How to bind my gridview data into the controls in edit mode and how to update

Posted by Kishore22 under ASP.NET on 9/20/2013 | Points: 10 | Views : 1718 | Status : [Member] | Replies : 6
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

GridView1.EditIndex = -1;
connect();
}
protected void GridView1_RowUpdating2(object sender, GridViewUpdateEventArgs e)

{

TextBox id = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TID");

TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

TextBox Lastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");

RadioButtonList Gender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

DropDownList Qual = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");

TextBox curemp1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");

TextBox caddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");

DropDownList ccity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp1");

TextBox paddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");

DropDownList pcity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");


SqlCommand cmd1 = new SqlCommand();

cmd1.Connection = con;

cmd1.CommandText = "UPDATE tab SET firstname ='" + TextBox1.Text + "',lastname ='" + TextBox2.Text + "',gender ='" + RadioButtonList1.SelectedValue + "',qual ='" + DropDownList1.Text + "', curemp='" + TextBox3.Text + "',caddress ='" + TextBox4.Text + "',ccity ='" + drp1.Text + "',paddress ='" + TextBox5.Text + "',tpcity ='" + DropDownList2.Text + "', WHERE id='" + TID.Text + "'";


cmd1.ExecuteNonQuery();

GridView1.EditIndex = -1;

connect();
con.Close();
}




Responses

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link once.. try to understand the process
http://stackoverflow.com/questions/12161973/edit-and-update-in-gridview

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kishore22 on: 9/20/2013 [Member] Starter | Points: 25

Up
0
Down
is is not working sir will u please give me a solution

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
You should use the TextBox names as FirstName.Text, Lastname.Text, Qual.SelectedValue.Tostring(), curemp1.Text and so on.........
cmd1.CommandText = "UPDATE tab SET firstname ='" + FirstName.Text + "',lastname ='" + LastName.Text

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Kishore,

Replace you cmd1.CommandText code by below one:-

cmd1.CommandText = "UPDATE tab SET firstname ='" + FirstName.Text + "',lastname ='" + Lastname.Text + "',gender ='" + Gender.SelectedValue + "',qual ='" + Qual.Text + "', curemp='" + curemp1.Text + "',caddress ='" + caddress1.Text + "',ccity ='" + Convert.ToString(ccity1.SelectedValue) + "',paddress ='" + paddress1.Text + "',tpcity ='" + Convert.ToString(pcity1.SelectedValue) + "', WHERE id='" + id.Text + "'";


Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/20/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
TextBox id = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TID"); 


TextBox FirstName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");

TextBox Lastname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");

RadioButtonList Gender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

DropDownList Qual = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");

TextBox curemp1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");

TextBox caddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");

DropDownList ccity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("drp1");

TextBox paddress1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");

DropDownList pcity1 = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2");


SqlCommand cmd1 = new SqlCommand();

cmd1.Connection = con;

cmd1.CommandText =
"UPDATE tab SET firstname ='" + FirstName.Text + "',lastname ='" + Lastname.Text + "',gender ='" + Gender.SelectedValue + "',qual ='" + Qual.SelectedValue.ToString()+ "', curemp='" + curemp1.Text + "',caddress ='" + caddress1.Text + "',ccity ='" + ccity1.SelectedValue.ToString() + "',paddress ='" + paddress1.Text + "',tpcity ='" + pcity1.SelectedValue.ToString()+ "', WHERE id='" + id.Text + "'";


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Kishore,

If you solve you issue, click on MARK IT AS ANSWER.

Happy Coding
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Kishore22, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response