8

I am getting this error: Unable to cast object of type ‘System.Web.UI.LiteralControl’ to type ‘System.Web.Controls.TextBox’

I am feeding my Text input box from a querystring in the ASPX page and here is the code:

<EditItemTemplate>
                        <asp:TextBox ID="GV_Post_ID" runat="server" text='<%# Request.QueryString["Post_ID"] %>'></asp:TextBox>
                    </EditItemTemplate>

But when I run it, it stops here:

cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = ((TextBox)GV_InlineEditing.Rows[0].Cells[2].Controls[0]).Text;

and I get the error above. Here is the code behind:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DSRConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO RCA_Events(Post_ID, Date, Description) VALUES(@Post_ID, @Date, @Description)";
            cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = ((TextBox)GV_InlineEditing.Rows[0].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@Date", SqlDbType.VarChar).Value = ((TextBox)GV_InlineEditing.Rows[0].Cells[3].Controls[0]).Text;
            cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = ((TextBox)GV_InlineEditing.Rows[0].Cells[4].Controls[0]).Text;

Please note if I remove the querystring from the ASPX page and then I insert the value manually then it works. Pls. help. thanks

2 Answers 2

11

The problem is here:

(TextBox)GV_InlineEditing.Rows[0].Cells[2].Controls[0]

The first control in that cell isn't the TextBox you think it is. Let's assume GV_InlineEditing.Rows[0] is safely getting you the row you need. Do something like this:

TextBox myTextBox = GV_InlineEditing.Rows[0].FindControl("GV_Post_ID") as TextBox;
cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = myTextBox.Text;

That code can be even more safe like this:

TextBox myTextBox = GV_InlineEditing.Rows[0].FindControl("GV_Post_ID") as TextBox;
if (myTextBox != null)
{
    cmd.Parameters.Add("@Post_ID", SqlDbType.VarChar).Value = myTextBox.Text;
}
else
{
    // Do something here.  Default value for the post id?
}
Sign up to request clarification or add additional context in comments.

Comments

7

The problem is when you are using Gridviews and you convert Databound fields into ItemTemplates, and you add for example a textbox to the EditItemTemplate and you then add its own ID to that textbox, for example txtProduct, the function .controls[0] won't find it, it doesnt know the ID of your textbox, so in this case you will have to provide the ID of the TextBox that you want to target. So instead of using .Controls[0] you should use .FindControl("txtProduct"). In your case instead of:

(TextBox)GV_InlineEditing.Rows[0].Cells[2].Controls[0].Text;

you should do this:

(TextBox)GV_InlineEditing.Rows[0].Cells[2].FindControl("GV_Post_ID").Text;

1 Comment

I stumbled upon this and found this to explain what I couldn't find anywhere. I cant thank you enough!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.