Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'. this error display when i update the record i want to update the record by datagridview by depend on the id .
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection(conctiioon);
int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
//TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textName = (TextBox)row.Cells[1].Controls[0];
TextBox textprice = (TextBox)row.Cells[2].Controls[0];//here the error display
GridView2.EditIndex = -1;
conn.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update items set name='" + textName.Text + "',price='" + textprice.Text + "' where id='" + userid + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating"
CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="id" HeaderText="S.No." />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Price") %>'
CssClass="stock">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<a href="#" id='<%# Eval("id") %>' class="updatebutton">
<img border="0" src="Images/update.png" alt="Delete" />
</a>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>