0

Here is the code that works:

protected void submitForMail(object sender, EventArgs e)
{
    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
    string cmdstr = "INSERT INTO EmailList(FirstName,LastName,EmailAddress) VALUES (@FirstName, @LastName, @EmailAddress)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);

    TextBox tFirstName = (TextBox)FormView1.FindControl("FirstName");
    TextBox tLastName = (TextBox)FormView1.FindControl("LastName");
    TextBox tEmail = (TextBox)FormView1.FindControl("EmailAddress");

    con.Open();
    com.Parameters.AddWithValue("@FirstName", tFirstName.Text);
    com.Parameters.AddWithValue("@LastName", tLastName.Text);
    com.Parameters.AddWithValue("@EmailAddress", tEmail.Text);
    com.ExecuteNonQuery();
    con.Close();
}

Here is the code that doesn't:

protected void UpdatePic(object sender, EventArgs e)
{
    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
    string cmdstr = "INSERT INTO BlogEntryItems(Picture) VALUES (@UpdatedPic)";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand com = new OleDbCommand(cmdstr, con);

    TextBox uPic = (TextBox)DataList1.FindControl("BEIPictureField");

    con.Open();
    com.Parameters.AddWithValue("@UpdatedPic", uPic.Text);
    com.ExecuteNonQuery();
    con.Close();
}

Here is the code containing the Datalist1 control:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/TravelJoansDB.mdb" 
    SelectCommand="SELECT * FROM Table2 INNER JOIN [BlogEntryItems] ON Table2.ID=BlogEntryItems.BlogID WHERE ID=@ID" >
    <SelectParameters>
        <asp:QueryStringParameter Name="ID" QueryStringField="ID" />
    </SelectParameters>
</asp:AccessDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="AccessDataSource1">
<ItemStyle />
<ItemTemplate>
    <table>
        <tr>
            <td>
                <br />
                <asp:Image ID="Image1" CssClass="placePicCenter" runat="server" 
                BorderWidth="1px"
                BorderColor="#EEEEEE"
                ImageUrl='<%# "PlaceImages/" + Eval("Picture") %>' /><br />
                <asp:TextBox ID="BEIPictureField" runat="server" Text='<%# Bind("Picture") %>' /><br />
                <asp:Button ID="UpdatePicButton" runat="server" Text="Update" OnClick="UpdatePic" />
                <br />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" CssClass="placeBodyStyle" runat="server" Text='<%# Eval("PicText1") %>' />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
</ItemTemplate>
</asp:DataList>

These blocks of code are for two different buttons on two different pages. The error I get when I run the second block of code is "Object reference not set to an instance of an object." Any help would be appreciated.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joseph
  • 609
  • 2
  • 12
  • 26
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 02 '13 at 02:37
  • I understand that it is saying it is not initialized, but why is the first section of code working that is for another button on another page in my site. – Joseph Oct 02 '13 at 02:49

1 Answers1

4

This line is the probable cause, it is failing to find the control BEIPictureField thus the "Object reference not set to an instance of an object." error

TextBox uPic = (TextBox)DataList1.FindControl("BEIPictureField");

EDIT 1

Try this:

TextBox uPic = (TextBox)DataList1.Items[1].FindControl("BEIPictureField");

you will have to rewrite your logic to find the control in each item not the DataList since it is the parent you will not find it there.

Ahsan
  • 2,488
  • 2
  • 22
  • 44
  • 2
    Also possible that `DataList1` is `null`. – Jay Oct 02 '13 at 02:32
  • Check my edited question. Why wouldn't it be able to find the control? – Joseph Oct 02 '13 at 02:37
  • @Joseph Please debug your code. i just gave a suggestion with the above example. this would only fetch item 1 in your datalist, if you have many items in the list you need to process them individually. – Ahsan Oct 02 '13 at 03:03