C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Update the Database


Posted Date: 16 Oct 2009    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: LaljiMember Level: Diamond    
Rating: 1 out of 5Points: 5



To update the database after inserting/updating/deleting a record from the grid
you need to create server-side methods and use them together with the server-side
events of the ASP.NET Grid:

* OnInsertCommand
* OnUpdateCommand
* OnDeleteCommand
* OnRebind
OnInsertCommand="InsertRecord"
OnUpdateCommand="UpdateRecord"
OnDeleteCommand="DeleteRecord"
OnRebind="RebindGrid">


Each method must have two arguments. The first argument is of type Object and
represents the Grid object which triggered the event. The second argument is
of type GridRecordEventArgs and contains the data that must be stored in
the database.



void DeleteRecord(object sender, GridRecordEventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath("Northwind.mdb"));
myConn.Open();

OleDbCommand myComm = new OleDbCommand("DELETE FROM Orders WHERE OrderID = @OrderID",
myConn);

myComm.Parameters.Add("@OrderID", OleDbType.Integer).Value = e.Record["OrderID"];

myComm.ExecuteNonQuery();
myConn.Close();
}

void UpdateRecord(object sender, GridRecordEventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath("Northwind.mdb"));
myConn.Open();

OleDbCommand myComm = new OleDbCommand("UPDATE Orders SET ShipName = @ShipName,
ShipCity = @ShipCity, ShipCountry = @ShipCountry, Deleted = @Deleted WHERE
OrderID = @OrderID", myConn);

myComm.Parameters.Add("@ShipName", OleDbType.VarChar).Value = e.Record["ShipName"];
myComm.Parameters.Add("@ShipCity", OleDbType.VarChar).Value = e.Record["ShipCity"];
myComm.Parameters.Add("@ShipCountry", OleDbType.VarChar).Value = e.Record["ShipCountry"];
myComm.Parameters.Add("@Deleted", OleDbType.Boolean).Value = e.Record["Deleted"];
myComm.Parameters.Add("@OrderID", OleDbType.Integer).Value = e.Record["OrderID"];

myComm.ExecuteNonQuery();
myConn.Close();
}

void InsertRecord(object sender, GridRecordEventArgs e)
{
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath("Northwind.mdb"));
myConn.Open();

OleDbCommand myComm = new OleDbCommand("INSERT INTO Orders (ShipName, ShipCity,
ShipCountry, Deleted) VALUES(@ShipName, @ShipCity, @ShipCountry, @Deleted)", myConn);

myComm.Parameters.Add("@ShipName", OleDbType.VarChar).Value = e.Record["ShipName"];
myComm.Parameters.Add("@ShipCity", OleDbType.VarChar).Value = e.Record["ShipCity"];
myComm.Parameters.Add("@ShipCountry", OleDbType.VarChar).Value = e.Record["ShipCountry"];
myComm.Parameters.Add("@Deleted", OleDbType.Boolean).Value = e.Record["Deleted"];

myComm.ExecuteNonQuery();
myConn.Close();
}

void RebindGrid(object sender, EventArgs e)
{
// re-create the grid from the updated database
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Update the Database  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: VB.Net Programming Delivers Reliable OOP Features
Previous Resource: Aspx page at server side
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use