Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am working in Grid and while inserting and updating I am getting a message

"Cannot find a cell bound to column name 'EmpName'" I dont kown Where I went wrong

What I have tried:

C#
protected void gvDetails_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        { 
            DataTable dt = new DataTable();
            
            try
            {

                cn.Open();
                SqlCommand cmd = new SqlCommand("Select * from gvdetails17", cn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);

                cn.Close();
                gvDetails.DataSource = ds;
              //  gvDetails.DataBind();
               
            }
            finally
            {
               
                cn.Close();
            }

        }

C#
protected void gvDetails_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
    {  
           
        GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;  
        string EmployeeName = (insertedItem["EmpName"].Controls[0] as TextBox).Text;  
        string Department = (insertedItem["Dep"].Controls[0] as TextBox).Text;  
        string Age = (insertedItem["Age"].Controls[0] as TextBox).Text;  
        string Sal = (insertedItem["Sal"].Controls[0] as TextBox).Text;  
       
         
        try  
        {  
             
               cn.Open();
               SqlCommand cmd = new SqlCommand("INSERT into  gvdetails17(EmpName,Dep,Age,Sal) values ('" + EmployeeName + "','" + Department + "','" + Age + "','" + Sal + "')", cn);
            int result = cmd.ExecuteNonQuery();   
                cn.Close();
        }  
        catch (Exception ex)  
        {  
            gvDetails.Controls.Add(new LiteralControl("Unable to insert Employee. Reason: " + ex.Message));  
            e.Canceled = true;  
        }    
        }
Posted
Updated 13-Mar-17 22:26pm

1 solution

Most probably, your GridView has a column which is bound to a column EmpName, but your query is not returning that column values. Your Gridview markup should exactly match with the columns returned from the query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900