HI
i am have a grid view with edit,update,delete button edit and delete button is wrking,updated button is nt wrking....in my table i dnt have id columns,here showing negative error on [string id = GridView1.DataKeys[e.RowIndex].Value.ToString();] this line...
hw to clear the error
hw to wrk?
my query:
OnRowCancelingEdit="GridView1_RowCancelingEdit" CellPadding="4" ForeColor="#333333" OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Height="199px" Width="516px">
public partial class As : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"data source=VAAYUSEVA_SVR;Initial Catalog=cppsch;User ID=sa;Password=sa");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select [USER NAME],HTSCNO,ADDRESS1,ADDRESS2 from TBL_CPPMASTER", con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//find student id of edit row
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
// string un = Session["Uname"].ToString();
TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Name");
TextBox branch = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Branch");
TextBox city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_City");
SqlCommand cmd = new SqlCommand("update TBL_CPPMASTER set HTSCNO='" + name.Text + "', ADDRESS1='"+branch.Text+"', ADDRESS2='"+city.Text+"' where [COMPANYNAME]= id", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindGridView();
}
// cancel row edit event
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGridView();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(e.Row.FindControl("lblRowNumber") as Label).Text = (e.Row.RowIndex + 1).ToString();
}
}
}
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

ali tuncerPosted Jun 14, 2016, 3:01 AM
you are missing DataKeyNames attribute in your grid..