AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 Problem in updating a row in gridview
Posted by Pallavi Dudeja on  4/7/2009 2:23:24 AM Category: C#
Add to my favorites
Email to friend
I m working on a Web application in which i've made a page to maintain the records and for that i've used a gridview. I've given the code for updating the gridview but when i click on the update linkbutton after editing the data in the gridview, the page get refreshed and i get the same old values as before.
Kindly help me.....
Here's the code:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string uid = (string)Session["ID"];
string abc = System.Configuration.ConfigurationSettings.AppSettings["abc"];
SqlConnection cn1 = new SqlConnection(abc);
//int id = (int)GridView1.DataKeys[(int)e.RowIndex].Value;
DropDownList ddl;
ddl= (DropDownList)(GridView1 .Rows[e.RowIndex].Cells[11].FindControl("DropDownList2"));
String strQuery="Update Contacts set FirstName=@FirstName,LastName=@LastName,Address=@Address,City=@City,State=@State,Phone=@Phone,Mobile=@Mobile,EmailId=@EmailID,Dob=@Dob,Note=@Note,Category=@Category Where CID=@CID and UserID=" + uid ;
SqlCommand scd = new SqlCommand(strQuery, cn1);
scd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
scd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
scd.Parameters.Add("@Address", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
scd.Parameters.Add("@City", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
scd.Parameters.Add("@State", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
scd.Parameters.Add("@Phone", SqlDbType.BigInt).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
scd.Parameters.Add("@Mobile", SqlDbType.BigInt).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
scd.Parameters.Add("@EmailId", SqlDbType.Text).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
scd.Parameters.Add("@Dob", SqlDbType.Text).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
scd.Parameters.Add("@Note", SqlDbType.NText).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[10].Controls[0]).Text;
scd.Parameters.Add("@Category", SqlDbType.Text).Value = ddl.SelectedValue;
scd.Parameters.Add("@CID", SqlDbType.Int).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
cn1.Open();
scd.ExecuteNonQuery();
cn1.Close();
GridView1.EditIndex = -1;
BindGrid();
}

Post Your Reply Post New Question

 Viewer's Reply
Check your values passed in WHERE Condition
Answered By Moderator1 on 4/7/2009 5:35:57 AM
Hi,

You have to check the Values you passed in Where Condition is passing the correct value. Also I noticed you have used Session to get the Uid. Also check the session, in the place where you assign it. There might be the problem. Else your code looks ok.
 
Checked the values
Answered By Pallavi Dudeja on 4/7/2009 5:44:16 AM
Thanx for replying.........
I've checked all the values and tried it a lot but it's not working.
Hope You would help me.....
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Oracle Magazine
Speech Technology Magazine
Website Magazine