Friday, January 23, 2015

How to perform DML operations on a GridView Directly

11:00 AM - By ajay desai 0

Explanation: -  The CommandBuilder class is used to to generate commands for insertion, updation and deletion of records in the database through an application. In the below given program, this class belongs to System.Data.SqlClient namespace.

Program: -

                                                                                                                                                              
using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace GridView { public partial class GridView : Form { SqlConnection con; SqlCommand cmd; SqlDataAdapter da; DataSet ds; SqlCommandBuilder cb; public GridView() { InitializeComponent(); } private void GridView_Load(object sender, EventArgs e) { con = new SqlConnection(); string cs = "Data Source=IFMRJPAL-PC\\TESTDB; Initial Catalog=LIB_MGMT; Integrated Security=True"; con.ConnectionString = cs; string q = "select bookid,bookname,authname,publication,edition from books"; cmd = new SqlCommand(q, con);//passing SQL query to the command object. da = new SqlDataAdapter(); da.SelectCommand = cmd;//this SelectCommand is used to retrieve data from database. ds = new DataSet(); da.Fill(ds, "books");//Fill command is used to fill the dataset table with records from the database table dataGridView1.DataSource = ds.Tables[0];//Binding a DataGridView with the DataTable of a DataSet. } private void sav_change_Click(object sender, EventArgs e) { SqlCommandBuilder cb = new SqlCommandBuilder(da); da.Update(ds, "books"); MessageBox.Show("changes are saved in the database"); } } }                                               

Output:-






Tags:
About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top