G
Guest
Inside my .aspx file, I have a textbox populated with data from a dataset
like this:
<asp:TextBox text='<%# DataBinder.Eval(Container.DataItem, "Comment")%>'
id="CommentText" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:datalist>
<asp:button id="EditCommentButton" Text="Edit Comment"
Runat="server"></asp:button
In my .aspx.cs file, I have a method:
private void EditCommentButton_Click(object sender, System.EventArgs e)
{
string editedComment = CommentText.Text.ToString();
// then here do an UPDATE
SqlConnection dataConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = "UPDATE table SET Comment = '" + editedComment +
"' ";
dataCommand.CommandText += "WHERE blah blah blah";
}
My codes is able to populate the textbox with all the comments in the html
page, but I cannot edit the text when firing on the "EditCommentButton_Click"
method.
I would get an error: "Object reference not set to an instance of an object".
Is this line incorrect? --> string editedComment =
CommentText.Text.ToString( )
I know the SQL Update works fine because if I do:
string editedComment = "HELLO WORLD";
then the textbox would be updated. So I guess something is wrong with
"CommentText.Text.ToString( )" and it is not casting to a string or something.
How should I change my codes so I can edit a populated textbox field?
like this:
<asp:TextBox text='<%# DataBinder.Eval(Container.DataItem, "Comment")%>'
id="CommentText" runat="server" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:datalist>
<asp:button id="EditCommentButton" Text="Edit Comment"
Runat="server"></asp:button
In my .aspx.cs file, I have a method:
private void EditCommentButton_Click(object sender, System.EventArgs e)
{
string editedComment = CommentText.Text.ToString();
// then here do an UPDATE
SqlConnection dataConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand dataCommand = new SqlCommand();
dataCommand.CommandText = "UPDATE table SET Comment = '" + editedComment +
"' ";
dataCommand.CommandText += "WHERE blah blah blah";
}
My codes is able to populate the textbox with all the comments in the html
page, but I cannot edit the text when firing on the "EditCommentButton_Click"
method.
I would get an error: "Object reference not set to an instance of an object".
Is this line incorrect? --> string editedComment =
CommentText.Text.ToString( )
I know the SQL Update works fine because if I do:
string editedComment = "HELLO WORLD";
then the textbox would be updated. So I guess something is wrong with
"CommentText.Text.ToString( )" and it is not casting to a string or something.
How should I change my codes so I can edit a populated textbox field?