Sumathi

Sumathi

  • 1.5k
  • 235
  • 1.4k

Editing and deleting in Gridview

Mar 27 2024 7:58 AM

I have added a couple new colums to the Gridview such as:

<Columns>
<asp:BoundField DataField="regno" HeaderText="RegNo" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="class" HeaderText="Class" />
<asp:BoundField DataField="mob" HeaderText="Contact" />
<asp:BoundField DataField="dob" HeaderText="DOB" />
<asp:BoundField DataField="gen" HeaderText="Gender" />
<asp:BoundField DataField="address" HeaderText="Address" />
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>

Here's the c# code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int regno = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblREGNO = (Label)row.FindControl("lblREGNO");
TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textc = (TextBox)row.Cells[1].Controls[0];
TextBox textmob = (TextBox)row.Cells[2].Controls[0];
TextBox textdob = (TextBox)row.Cells[3].Controls[0];
TextBox textgen = (TextBox)row.Cells[4].Controls[0];
TextBox textadd = (TextBox)row.Cells[5].Controls[0]; 
GridView1.EditIndex = -1; cn.Open();
SqlCommand cmd = new SqlCommand("update student set name='" + textName.Text + "',address='" + textadd.Text + "',class='" + textc.Text + "',gender='" + textgen.Text + "',contact='" + textmob.Text + "',dob='" + textdob.Text + "'where regno='" + regno + "'", cn); cmd.ExecuteNonQuery();
cn.Close();
gvbind();
}

However, i'm getting an error that says: A field or property with the name 'mob' was not found on the selected data source. According to the original code, there were only 3 columns in the Gridview, I have added 4 more columns, but am getting an error saying the column is not found. How do i add new columns to the gridview, and be able to edit and delete them accoring to the above code? It is a program to edit and delete student details in Gridview.


Answers (1)