how to use itemcommand w/datalist & buttons w/out object reference error?
i'm in c# ASP.NET and I'm new to all this. trying to teach myself but I'm hung up on something that I suppose should be simple. I couldn't find the answer on here with searches because I guess I don't know the appropriate way to describe what I'm looking for. So i last resorted to bugging you guys for an answer.
Please be very basic, I'm quite new but eager.
I have a datalist returning X number of results from a database (MSSQL) - each result comes with some information, and then 2 textbox's and a button. i want them to be able to enter some information into each box, click a button, and then that is inserted back into my SQL database.
I want the text results from each textbox, along with the id (a sql value returned from the datalist's results) to go with it (so that my insert knows which results this is from)
so my page looks like
text 1 - TEXTBOX - TEXTBOX - BUTTON text 2 - etc etc
if the guy fills out text 2's 2 textboxes and clicks text2's button, i insert (textbox1.text, textbox2.text, "text 2") into my db
this is what i have in my code behind for the datalist so far:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Button Button2b = (Button)e.Item.FindControl("Button2");
TextBox TextBox2b = (TextBox)e.Item.FindControl("TextBox2");
TextBox TextBox3b = (TextBox)e.Item.FindControl("TextBox3");
SqlDataSource commentinsert = new SqlDataSource();
commentinsert.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
commentinsert.InsertCommandType = SqlDataSourceCommandType.Text;
commentinsert.InsertCommand = "INSERT INTO ocomments (cuser, date, ip, blogid, text) VALUES (@cuser, @date, @ip, @blogid, @text)";
commentinsert.InsertParameters.Add("cuser", TextBox2b.Text);
commentinsert.InsertParameters.Add("date", DateTime.Now.ToString());
commentinsert.InsertParameters.Add("ip", Request.UserHostAddress.ToString());
commentinsert.InsertParameters.Add("blogid", "16");
commentinsert.InsertParameters.Add("text", TextBox3b.Text);
commentinsert.Insert();
}
here is the relevant datalist in my aspx
<asp:DataList ID="DataList1" runat="server" DataKeyField="id"
DataSourceID="SqlDataSource1" style="width:700px;" onitemdatabound="DataList1_ItemDataBound"
onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("subject") %>' style="font-size:25pt;font-family:Tahoma;"></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("auser", " - {0}") %>' style="font-size:15pt;color:#74daf8;font-family:Tahoma;"></asp:Label>
<br />
<hr width="80%" size="1" NOSHADE color="#343a68" />
<asp:Label style="font-size:14pt;font-family:Tahoma;font-weight:normal;" ID="Label3" runat="server" Text='<%# Eval("body") %>' />
<asp:HyperLink ID="HyperLink5" runat="server"
NavigateUrl='<%# Eval("id", "http://domain.com/?id={0}#{0}") %>'
Visible="False">edit</asp:HyperLink>
<br />
<span style="float:right;">
<asp:HyperLink ID="HyperLink7" runat="server" style="cursor:pointer;font-family:开发者_如何学运维Tahoma;
font-size:12pt;color:White;"># comments - add a comment</asp:HyperLink>
</span>
<br />
<asp:Panel ID="Panel2" runat="server" style="display:none;width:600px;
background-color:Black;margin: 10px 0 10px 10px;padding: 5px 0 10px 10px;">
your name:
<br />
<asp:TextBox ID="TextBox2" runat="server" Width="250px"></asp:TextBox>
<br />
your comment:
<br />
<asp:TextBox ID="TextBox3" runat="server" Width="550px" height="150px" TextMode="MultiLine"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server"
Text="Send Comment" Width="200px" Height="35px" Font-Bold="False" Font-Names="tahoma"
Font-Size="17pt" CommandArgument='<%# Eval("id") %>' />
</asp:Panel>
<hr width="30%" size="1" align="right" NOSHADE color="#696969" />
</ItemTemplate>
</asp:DataList>
and here is the error i get when i click the send comment button, even if i have text int he textboxes i'm trying to pull data from:
Server Error in '/' Application. Object reference not set to an instance of an object.
please help!
EDIT: tried the following code per user suggestion, but I'm receiving the exact same error.
Control panelControl = e.Item.FindControl("Panel2");
Button Button2b = panelControl.FindControl("Button2") as Button;
TextBox TextBox2b = panelControl.FindControl("TextBox2") as TextBox;
TextBox TextBox3b = panelControl.FindControl("TextBox3") as TextBox;
SqlDataSource commentinsert = new SqlDataSource();
commentinsert.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
commentinsert.InsertCommandType = SqlDataSourceCommandType.Text;
commentinsert.InsertCommand = "INSERT INTO ocomments (cuser, date, ip, blogid, text) VALUES (@cuser, @date, @ip, @blogid, @text)";
commentinsert.InsertParameters.Add("cuser", TextBox2b.Text);
commentinsert.InsertParameters.Add("date", DateTime.Now.ToString());
commentinsert.InsertParameters.Add("ip", Request.UserHostAddress.ToString());
commentinsert.InsertParameters.Add("blogid", Button2b.CommandArgument.ToString());
commentinsert.InsertParameters.Add("text", TextBox3b.Text);
commentinsert.Insert();
It is because your Button controls and Textbox controls are with in a Panel, FIndControl searched for Immediate children. Change your handler to:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Control panelControl = e.Item.FindControl("Panel2");
Button Button2b = panelControl.FindControl("Button2") as Button;
TextBox TextBox2b = panelControl.FindControl("TextBox2") as TextBox;
TextBox TextBox3b = panelControl.FindControl("TextBox3") as TextBox;
.
.
.
.
精彩评论