简体   繁体   中英

Why is one section of code working, but the other not?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM