.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Insert datagridview records to database

Posted By:satyapriyanayak       Posted Date: October 07, 2013    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

We will know how to insert datagridview records to database.
 

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
        <add key="dsn" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\EMP.mdb" />
    </appSettings>
</configuration>


Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Insertdata_from_datagrid__dbcsharp
{
    public partial class Form1 : Form
    {
        string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
        OleDbCommand com;
        OleDbDataAdapter oledbda;
        DataSet ds;
        string str;
       

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_insert_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
          
                str = @"INSERT INTO student1(sid,sname,smarks,saddress) VALUES ('" + dataGridView1.Rows[i].Cells["sid"].Value + "', '" + dataGridView1.Rows[i].Cells["sname"].Value + "'," + dataGridView1.Rows[i].Cells["smarks"].Value + ",'" + dataGridView1.Rows[i].Cells["saddress"].Value + "');";
                try
                {
                    using (OleDbConnection con = new OleDbConnection(ConnectionString))
                    {
                        using (com = new OleDbCommand(str, con))
                        {
                            con.Open();
                            com.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            label1.Text = "Records inserted successfully";
        }

        private void bindgrid()
        {
            OleDbConnection con = new OleDbConnection(ConnectionString);
            con.Open();
            str = "select * from student";
            com = new OleDbCommand(str, con);
            oledbda = new OleDbDataAdapter(com);
            ds = new DataSet();
            oledbda.Fill(ds, "student");
            dataGridView1.DataMember = "student";
            dataGridView1.DataSource = ds;
            con.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bindgrid();
        }
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend