SIGN UP MEMBER LOGIN:    
ARTICLE

Automatic Generated DataGrid Commands

Posted by Levent Camlibel Articles | ADO.NET in C# August 15, 2001
This article illustrates how to update, insert, and delete in the windows DataGrid control by using automatic generated commands.
Reader Level:
Download Files:
 

Description 

This article illustrates how to update, insert, and delete in the windows DataGrid control by using automatic generated commands. There are two main functions which handle retrieving  and manipulating data. To manipulate data in DataGrid, you have to create commands for them.

MSDN documentation states that If your DataTable maps to or is generated from a single database table, you can take advantage of the CommandBuilder object to automatically generate the DeleteCommand, InsertCommand, and UpdateCommand of the DataAdapter. This simplifies and reduces the code required to perform INSERT, UPDATE, and DELETE operations.The SelectCommand must also return at least one primary key or unique column. If none are present, an InvalidOperation exception is generated, and the commands are not generated.

The code is self explanatory, if you just review the code line by line, you will not have any problem to understand how it is done.

Source Code:

private void GridBind()
{
///connection string which will be different in your computer
StringConnStr="server=(local)\\NetSDK;uid=northwind;pwd=northwind;database=northwind";
SqlConnection myConnection= new SqlConnection(ConnStr);
///instantiate SqlDataAdapter to create DataSet
CustomerAdapter = new SqlDataAdapter("select * from customers",myConnection);
/// the following is for creating automatic command builder
CustomerCmdBuilder = new SqlCommandBuilder(CustomerAdapter);
///fill the dataset
CustomerAdapter.Fill(CustomerDataSet, "Customers");
///set the dataset as a datasource for windows datagrid
dataGrid1.SetDataBinding(CustomerDataSet,"Customers");
}
private void btnSave_Click(object sender, System.EventArgs e)
{
try
{
///the following statement
///inserts
///updates
///deletes for you
/// Without the SqlCommandBuilder, this line would fail.
CustomerAdapter.Update(CustomerDataSet,"Customers");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor