ASP.NET - updating problem using LINQ - Asked By aman on 25-Oct-11 01:25 AM

hi all,

i am using vs 2010 and sql server

i have gridview on my web form and my requirement is make the grid view updateable, deleteable using linq.....

below i have written code for the same but value is taken by the code is the old data only.......

please have a look and help me.........


void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)


string uniq=((TextBox)GridView1.Rows[e.RowIndex].Cells[9].Controls[0]).Text;

string firstname = ((TextBox


string middlename = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

string lastname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;

string department = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

string designation = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text;

string mobnumber = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text;

string dob = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text;

DataClasses1DataContext obj = new DataClasses1DataContext();

var update = obj.Employees.Single(s => s.Emp_Code == uniq);

update.Emp_FirstName = firstname;

update.Emp_MiddleName = middlename;

update.Emp_LastName = lastname;

update.Department = department;

update.Designation = designation;

update.MobileNumber = mobnumber;

update.DOB = dob;





"Updated Successfully");


catch (Exception ex)





GridView1.EditIndex = -1;


the above code is updating but the old value only.......

TSN ... replied to aman on 25-Oct-11 01:39 AM

check if the textboxes in the gridview are READONLY="true", if they are set to Readonly only please remove that it will solve your problem...

or check whether you are binding the gridview in  (!ISPostBack) condition are not...

these are the possibilities i can give you , even if the problem is present send the whole CS file code with designer...

hope this helps you...
Sri K replied to aman on 25-Oct-11 01:40 AM
Could you please put the breakpoint and Check it once the value from the Textboxes?If any probelm,ask me.
Riley K replied to aman on 25-Oct-11 01:42 AM

Most likely reason is, you are always binding the data to the GridView without checking the IsPostBack property.
When you are clicking the "Update" button, the Page_Load() event gets invoked before the RowUpdating() event method is invoked. So, the GridView is getting bound again with the old value (Because you might not checked the IsPostBack flag) and hence, inside the RowUpdating() event method you are getting the old value.
If my assumption is correct, to solve this, change the Data binding method of the GridView as follows:

if (!IsPostBack) //Bind GridView only when IsPostBack is false
       GridView1.DataSource = //Assign data here;

Jitendra Faye replied to aman on 25-Oct-11 01:52 AM
Check this-

Don't call your LoadGridData()  Page_Load() event.

Call it like this-

protected void Page_Load(object sender, EventArgs e)







Try this and let me know.
Devil Scorpio replied to aman on 27-Oct-11 03:08 PM

To enable updating, inserting, and deleting data

  1. Add a control to an ASP.NET Web page.

  2. Set the property to the name of an object that derives from the class.

    When you use the Object Relationship Designer in Visual Studio 2010 to create classes that represent SQL database tables, the generated classes automatically derive from For information about how to connect the control to a database, see

  3. Set the property to the name of the class that represents the database table that you want to update.

  4. Set one or more of the,, and properties of the control to true.

  5. Add a data bound control, such as a control, and set its property to the ID of the control.

  6. Set the property of the data control to the name of the table's primary key column or columns.

  7. Provide a way in the data-bound control for the user to switch to update, delete, or edit mode.

    For example, you can add buttons to the data-bound control or enable the control to automatically create those buttons.

    The following example shows the markup for part of an ASP.NET Web page that contains a control and a control. The controls are configured to enable the user to display, update, insert, and delete data in a table named Products.


protected void LinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    Product product = (Product)e.NewObject;
    product.DateModified = DateTime.Now;

Please refer the website for more info and code