Please review the code as i am geting the error where i have placed arrow mark on the code section.

Afer editing when i click on the update the below error is fired.

what i am doing wrong

Error:
Unable to cast object of type 'System.Web.UI.WebControls.DataGridLinkButton' to type 'System.Web.UI.WebControls.TextBox'.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;




public partial class _Default : System.Web.UI.Page 
{

    SqlConnection con1;
    SqlCommand cmd;
    SqlDataAdapter da;
    static DataTable dt=new DataTable();

    //SqlCommandBuilder cob;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

        con1=new SqlConnection(str);

        ds = new DataSet();

        cmd=new SqlCommand("select * from emp",con1);
        da=new SqlDataAdapter(cmd);
        con1.Open();

        da.Fill(ds,"emp");
        dt = ds.Tables["emp"];
        DataGrid1.DataSource=dt;
        DataGrid1.DataBind();
        con1.Close();
     }


    protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
    {

    }

    static string num;
    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
    {

        num=(e.Item.Cells[1].Text.ToString());
        DataGrid1.EditItemIndex = e.Item.ItemIndex;
        con1.Open();

        da.Fill(ds, "emp");
        dt = ds.Tables["emp"];
        DataGrid1.DataSource = dt;
        DataGrid1.DataBind();
        con1.Close();
    }


    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        TextBox t1, t2, t3,t4;

   >>>>>> t1 = (TextBox)e.Item.Cells[1].Controls[0];<<<<<<<<<<<<<<<
        t2 = (TextBox)e.Item.Cells[2].Controls[0];
        t3 = (TextBox)e.Item.Cells[3].Controls[0];
        t4 = (TextBox)e.Item.Cells[4].Controls[0];



        cmd = new SqlCommand("update emp set emp_name='"+t1.Text+"',city='"+t2.Text+"',salary='"+t3.Text+"',age='"+t4.Text+"' where emp_name="+num, con1);
        da = new SqlDataAdapter(cmd);
        con1.Open();

        cmd.ExecuteNonQuery();
        da.Fill(ds, "emp");
        DataGrid1.DataSource = ds.Tables[0];
        DataGrid1.DataBind();
        con1.Close();

    }
}

Aspx code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:DataGrid ID="DataGrid1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" ondeletecommand="DataGrid1_DeleteCommand" 
        oneditcommand="DataGrid1_EditCommand" 
        onselectedindexchanged="DataGrid1_SelectedIndexChanged" 
        onupdatecommand="DataGrid1_UpdateCommand">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditItemStyle BackColor="#999999" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
        <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update">
            </asp:EditCommandColumn>
            <asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>
        </Columns>
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    </asp:DataGrid>
    </form>
</body>
</html>

rino abraham,

Assuming that your error is occuring at the line with all the >>>>>>>>>>>>>>>>>>,
the first thought that comes to mind is that there are no columns being populated except for the command buttons, which are rendered as linkbuttons. That said, it is difficult to tell from what you posted. How many columns are in "emp"? Are they all showing up?

The fact that this line

t1 = (TextBox)e.Item.Cells[1].Controls[0];

is creating the error says to me that the object in cells[1] (which is the 2nd cell, if you were not aware) is of type linkButton, which would make me think that it one of the command buttons.

I would suggest debugging and seeing what is actually getting bound to your grid.

Hope that helps.

hi....For editing grid view ..try the below link
[snipped blog spam]

Hi ,
I appreciate you relplies.

There are 4 columns in Emp table and all are showing up in Data Grid as i Click on EDIT and change the row data and click on the UPDATE this Error is popped up..

ThankYou for help m still having problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.