This is a migrated thread and some comments may be shown as answers.

update radgrid in database

7 Answers 252 Views
Grid
This is a migrated thread and some comments may be shown as answers.
esmyy
Top achievements
Rank 1
esmyy asked on 14 Feb 2012, 10:31 AM
Hello
I made a project like this demo here : http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/editmodes/defaultcs.aspx
I can edit/update/delete the rows, but it's only updating in the actual session, not in the sqldatabase.
What do I have to add to my code, so it's updating directly in my database?

Thanks for your help,
esmyy

7 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 14 Feb 2012, 11:54 AM
Hi,

Just rework the code in the UpdateCommand event handler of the grid to work directly with the data base. You can keep on using the SqlCommand approach - just pass it an update statement:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx 
 
Regards,
Tsvetoslav
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Feb 2012, 11:55 AM
Hello,

protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {       
                           // existing code
                            try
                            {
                                DataRow[] changedRows = this.EmployeesData.Tables["Employees"].Select( "EmployeeID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["EmployeeID"] );
                                changedRows[0][column.UniqueName] = editorValue;
                                this.EmployeesData.Tables["Employees"].AcceptChanges();
                            }
 
// New code
try
{
   // perform your DB UPDATE operation
    Session["EmployeesData"] = null;
}
}



Thanks,
Jayesh Goyani
0
esmyy
Top achievements
Rank 1
answered on 15 Feb 2012, 03:19 PM
First of all thanks for your reply, it was really fast :)

I have an additional question:
I read somewhere, that if you use 'recordset' or something with 'cursortype' you can edit/update the datatable easier.
Does somebody know how do do that with my excisting code, or do I have to change everything?

Thanks in advance,
esmyy
0
esmyy
Top achievements
Rank 1
answered on 16 Feb 2012, 09:12 AM
Well, I changed almost my whole project, I'm using the EntityFramework now, according to this demo.
But now I have a little problem, in this demo the CustomerID is a string, what do I have to write in this lines, if my ID is an int?

var customerID = item.GetDataKeyValue("CustomerID").ToString();
Customers customer = DbContext.Customers.Where(p => p.CustomerID == customerID).FirstOrDefault();


Thanks,
esmyy
0
Shinu
Top achievements
Rank 2
answered on 16 Feb 2012, 10:45 AM
Hello Esmyy,

Eventhough the DataType of the field is integer, you can access the DataKeyValue as shown below.
C#:
string customerID = item.GetDataKeyValue("CustomerID").ToString();

-Shinu.
0
esmyy
Top achievements
Rank 1
answered on 16 Feb 2012, 10:49 AM
.. and what about the second line?
I doesn't work with a string and an integer.

Customers customer = DbContext.Customers.Where(p => p.CustomerID == customerID).FirstOrDefault();

0
esmyy
Top achievements
Rank 1
answered on 16 Feb 2012, 04:19 PM
I solved the problem, thanks to everybody, who tried to help me!

Regards,
esmyy
Tags
Grid
Asked by
esmyy
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Jayesh Goyani
Top achievements
Rank 2
esmyy
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or