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.........

protected

void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{


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

string firstname = ((TextBox

)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

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;


try

{

obj.SubmitChanges();

Response.Write(

"Updated Successfully");

}


catch (Exception ex)

{

Response.Write(ex.Message);

}

LoadGridData();

GridView1.EditIndex = -1;

}


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

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

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;
       GridView1.DataBind();
}

Regards
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)

{


if(!IsPostBack)

{

LoadGridData();

}

}


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

To enable updating, inserting, and deleting data

  1. Add a http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.aspx control to an ASP.NET Web page.

  2. Set the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.contexttypename.aspx property to the name of an object that derives from the http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx 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 http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx. For information about how to connect the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.aspx control to a database, see http://msdn.microsoft.com/en-us/library/bb547113.aspx.

  3. Set the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.tablename.aspx property to the name of the class that represents the database table that you want to update.

  4. Set one or more of the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.enableupdate.aspx, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.enabledelete.aspx, and http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.enableinsert.aspx properties of the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.aspx control to true.

  5. Add a data bound control, such as a http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx control, and set its http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.databoundcontrol.datasourceid.aspx property to the ID of the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.aspx control.

  6. Set the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.datakeynames.aspx 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 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.aspx control and a http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx control. The controls are configured to enable the user to display, update, insert, and delete data in a table named Products.


    <asp:LinqDataSource
      ContextTypeName="ExampleDataContext"
      TableName="Products"
      EnableUpdate="true"
      EnableInsert="true"
      EnableDelete="true"
      ID="LinqDataSource1"
      runat="server">
    </asp:LinqDataSource>
    <asp:DetailsView
      DataSourceID="LinqDataSource1"
      DataKeyNames="ProductID"
      AutoGenerateEditButton="true"
      AutoGenerateDeleteButton="true"
      AutoGenerateInsertButton="true"
      AllowPaging="true"
      ID="DetailsView1"
      runat="server">
    </asp:DetailsView>


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
http://msdn.microsoft.com/en-us/library/bb514963.aspx