简体   繁体   English

为什么其中一个代码段有效,而另一部分无效?

[英]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: 这是包含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>

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." 此行是可能的原因,它找不到控件BEIPictureField,因此“对象引用未设置为对象的实例”。 error 错误

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

EDIT 1 编辑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. 您将不得不重写逻辑以在每个项目中而不是在DataList中找到控件,因为它是父级,您将无法在其中找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么泛型转换在此部分代码上不起作用? - Why Generic Casting is not working on this section of code? 为什么一个适配器填充有效,而另一个适配器不起作用? - why is one adapter fill working and not the other? 代码混淆 - 为什么一个工作,而另一个工作? - Code confusion - why does one work, but not the other? Unity C#代码可以在一个脚本中工作,而不能在其他脚本中工作吗? - Unity c# Code working in one script but not in other? 为什么相同的代码会在一个应用程序中崩溃而在另一个应用程序中工作呢? - Why would the same exact code crash in one app, and work in the other? 为什么一个代码片段可以编译,而另一个则不能? - Why does one code snippet compile, but the other does not? 将代码段一次锁定到一个入口 - Lock code section to one entrance at a time 段落破坏,一页到另一页,在Crystal Reports的详细部分 - Paragraph breakage, one page to the other page, in detail section of Crystal Reports SqlDependency正在使用一个表,但不能使用其他表 - SqlDependency is working with one but not with other table 应用程序在一个链接上工作,但不在另一链接上 - Application working on one link but not on other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM