为什么一段代码工作,但另一段没有?

时间:2013-10-02 02:27:27

标签: c# asp.net nullreferenceexception

以下是有效的代码:

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();
}

以下代码不是:

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();
}

以下是包含Datalist1控件的代码:

<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>

这些代码块用于两个不同页面上的两个不同按钮。我运行第二个代码块时得到的错误是“对象引用未设置为对象的实例”。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

这一行是可能的原因,它无法找到控件BEIPictureField因此“对象引用未设置为对象的实例”。错误

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

编辑1

试试这个:

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

你必须重写你的逻辑才能找到每个项目中的控件而不是DataList,因为它是你在那里找不到的父项。