aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Use SqlBulkCopy to Insert Gridview Data into Database in using C#, VB.NET

Apr 26, 2015

Here I will explain how to use sqlbulkcopy to insert
gridview data into database in in c#, or insert gridview data in datatable and insert in database in using c#, with example or insert bulk data from gridview to database in using sqlbulkcopy in .

In previous articles I explained import data from excel to database in, read xml file and bind data to gridview in, create datatable dynamically and bind to gridview in,
Delete multiple rows in gridview using checkbox in and many articles relating to gridview,, c#, and jQuery. Now I will explain how insert gridview data into database in using sqlbulkcopy in c#,

Before we implement this example first design one table EmployeeInfo in your database as shown below

Now create one new web application and open your aspx page and write the code like as shown below

<html xmlns="">
<head runat="server">
<title>Insert Gridview Data to Database in using SQLBulkCopy</title>
<form id="form1" runat="server">
<asp:GridView ID="gvDetails" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</div><br />
<asp:Button ID="btnInsert" runat="server" Text="Insert Gridview Data"
Font-Bold="true" onclick="btnInsert_Click" /><br />
<asp:Label ID="lblMsg" runat="server"/>

Now right click on your application à Select Add New Item à Select XML file à Give name as “sample.xml” and click OK

Once we add xml file open it and write code like as shown below

<?xml version="1.0" encoding="utf-8" ?>
<Designation>Team Leader</Designation>
<Designation>Software Developer</Designation>
<Designation>Business Analyst</Designation>

After completion of xml file now open aspx page codebehind behind file and add following namespaces

C# Code

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

After completion of adding namespaces you need to write the code like as shown below

DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
//Bind Data to Gridview
// This method is used to get xml node values and bind to gridview
protected void GetXMLData()
DataSet ds = new DataSet();
dt = ds.Tables[0];
gvDetails.DataSource = dt;
// Insert data in database using SqlBulkCopy
protected void btnInsert_Click(object sender, EventArgs e)
String strConnection = "Data Source=Suresh;Initial Catalog=MySampleDB;Integrated Security=True";
DataTable dt = new DataTable();
dt.Columns.Add("userid", typeof(int));
dt.Columns.Add("username", typeof(string));
dt.Columns.Add("firstname", typeof(string));
dt.Columns.Add("lastname", typeof(string));
dt.Columns.Add("designation", typeof(string));
foreach (GridViewRow gvrow in gvDetails.Rows)
int userid = int.Parse(gvrow.Cells[0].Text);
string firstname = gvrow.Cells[1].Text;
string lastname= gvrow.Cells[2].Text;
string username = gvrow.Cells[3].Text;
string designation = gvrow.Cells[4].Text;
dt.Rows.Add(userid,username,firstname, lastname, designation);
using (SqlConnection con = new SqlConnection(strConnection))
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "EmployeeInfo";
lblMsg.Text = "Details Inserted Successfully";
lblMsg.ForeColor = System.Drawing.Color.Green;


Imports System.Data
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
Partial Class VBCode
Inherits System.Web.UI.Page
Private dt As New DataTable()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Bind Data to Gridview
End If
End Sub
' This method is used to get xml node values and bind to gridview
Protected Sub GetXMLData()
Dim ds As New DataSet()
dt = ds.Tables(0)
gvDetails.DataSource = dt
End Sub
' Insert data in database using SqlBulkCopy
Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnection As [String] = "Data Source=Suresh;Initial Catalog=MySampleDB;Integrated Security=True"
Dim dt As New DataTable()
dt.Columns.Add("userid", GetType(Integer))
dt.Columns.Add("username", GetType(String))
dt.Columns.Add("firstname", GetType(String))
dt.Columns.Add("lastname", GetType(String))
dt.Columns.Add("designation", GetType(String))
For Each gvrow As GridViewRow In gvDetails.Rows
Dim userid As Integer = Integer.Parse(gvrow.Cells(0).Text)
Dim firstname As String = gvrow.Cells(1).Text
Dim lastname As String = gvrow.Cells(2).Text
Dim username As String = gvrow.Cells(3).Text
Dim designation As String = gvrow.Cells(4).Text
dt.Rows.Add(userid, username, firstname, lastname, designation)
Using con As New SqlConnection(strConnection)
Dim sqlBulk As New SqlBulkCopy(strConnection)
'Give your Destination table name
sqlBulk.DestinationTableName = "EmployeeInfo"
End Using
lblMsg.Text = "Details Inserted Successfully"
lblMsg.ForeColor = System.Drawing.Color.Green
End Sub
End Class


Use SqlBulkCopy to Insert Gridview Data into Database in using C#, VB.NET

Download Sample Code Attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Anonymous said...

Excellent ......those who are good in C#, it excellent for them to understand step by step...great keep it up....if possible give those questions which will ask for system testing in interview....

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.