how to insert the records using sqlBulkCopy(bulk Insert)

This Example will help you to insert the records in a datatbse using BulkCopy.
If you have huge amount of data to be entered at a time then this bulk copy concept is very easy and faster to insert the records...

Step 1: drag all the textboxes like Id,Name,Address etc. into your form.
Step 2: drag a button and gridview into your form..
Step 3:Dim _dtUserdtls As New DataTable 'public declaration
Step 4: in page load write the below code
'adding the columns dynamically
_dtUserdtls.Columns.Add("Id")
_dtUserdtls.Columns.Add("Name")
_dtUserdtls.Columns.Add("Address")
_dtUserdtls.Columns.Add("Salary")
_dtUserdtls.Columns.Add("Email")
_dtUserdtls.Columns.Add("DOB")
_dtUserdtls.Columns.Add("State")
_dtUserdtls.Columns.Add("Country")
Step 5: write the below code in button click event.
Dim dr As DataRow
dr = _dtUserdtls.NewRow()
dr("Id") = txtId.Text.ToString()
dr("Name") = txtName.Text.ToString()
dr("Address") = txtAddress.Text.ToString()
dr("Salary") = txtSalary.Text.ToString()
dr("Email") = txtEmail.Text.ToString()
dr("DOB") = dtpDOB.Text
dr("State") = cmbStatus.Text
dr("Country") = cmbCountry.Text
DgvDisplay.DataSource = _dtUserdtls
Step 6 :
Dim bcp As New SqlBulkCopy(DataLayer.ConnectionString(), SqlBulkCopyOptions.UseInternalTransaction)
bcp.DestinationTableName = "tb_UserMaster" 'table name has to be insert
'all the below columns mapping should match with source column(datatable column name) and destination column(table column name)
bcp.ColumnMappings.Add("Id", "Id")
bcp.ColumnMappings.Add("Name", "Name")
bcp.ColumnMappings.Add("Address", "Address")
bcp.ColumnMappings.Add("Salary", "Salary")
bcp.ColumnMappings.Add("Email", "Email")
bcp.ColumnMappings.Add("DOB", "DOB")
bcp.ColumnMappings.Add("State", "State")
bcp.ColumnMappings.Add("Country", "Country")
bcp.WriteToServer(_dtUserdtls)

Note:if you want to insert all the gridview rows into table then assign all the rows ivalues into datatable using datasource property and use this datatable for the above code
_dtUserdtls = Gridview1.datasource

hope this code will help you....
any doubts plz free to ask..

Code to get some important folder path and OS Version

Below code will help to get the important folder path and OS Version
VB.NET Code
Dim PersonalDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim CookiesDir As String = Environment.GetFolderPath(Environment.SpecialFolder.Cookies)
'Path of the Exe
Dim Location As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim os As OperatingSystem
os = Environment.OSVersion
MessageBox.Show(os.Version.ToString()) 'OS Version
MessageBox.Show(os.Platform.ToString()) 'OS Platform

C#.NET Code
string PersonalDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string CookiesDir = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
//Path of the Exe
string Location = System.Reflection.Assembly.GetExecutingAssembly().Location;
OperatingSystem os = default(OperatingSystem);
os = Environment.OSVersion;
MessageBox.Show(os.Version.ToString()); //OS Version
MessageBox.Show(os.Platform.ToString()); //OS Platform

Add/Remove Columns of a DataTable Dynamically/RunTime

if u want to add some of the columns in ur existing datatable in runtime then the below code will be very usefull....
let ur datatable columns will be (ID,Name) before adding the columns into the datattable(dttable)
and now u want add two more columns into the datatable(dttable) then below will the code for this
VB.NET Code
Dim CreatedDate As DataColumn
Dim Address As DataColumn

CreatedDate = New DataColumn("CreatedDate", System.Type.GetType("System.DateTime"))
'if u want to set the same values for all the rows then write the below code other 'wise just remove the below line
CreatedDate.DefaultValue = Now.Date 'set the default value for all the rows
If dttable.Columns.Contains("CreatedDate") = True Then
dttable.Columns.RemoveAt("CreatedDate")
dtTable.Columns.Add(CreatedDate)
Else
dtTable.Columns.Add(CreatedDate)
End If

Address = New DataColumn("Address", System.Type.GetType("System.String"))
Address.DefaultValue = "India" 'set the default value for all the rows
If dtTable.Columns.Contains("Address") = True Then
dtTable.Columns.Remove("Address")
dtTable.Columns.Add(Address)
Else
dtTable.Columns.Add(Address)
End If
Now ur datatable(dttable) columns will be (ID,Name,CreatedDate,Address)

C#.NET Code
DataColumn CreatedDate = null;
DataColumn Address = null;

CreatedDate = new DataColumn("CreatedDate", System.Type.GetType("System.DateTime"));
CreatedDate.DefaultValue = DateAndTime.Now.Date;
if (dttable.Columns.Contains("CreatedDate") == true) {
dttable.Columns.RemoveAt("CreatedDate");
dtTable.Columns.Add(CreatedDate);
} else {
dtTable.Columns.Add(CreatedDate);
}

Address = new DataColumn("Address", System.Type.GetType("System.String"));
//if u want to set the same values for all the rows then write the below code other //wise just remove the below line
Address.DefaultValue = "India"; 'set the default value for all the rows
if (dtTable.Columns.Contains("Address") == true) {
dtTable.Columns.Remove("Address");
dtTable.Columns.Add(Address);
} else {
dtTable.Columns.Add(Address);
}

Code to Download the Files from Internet

Below Code will help you download files from internet.
it's very simple code just follow the few steps.
Step 1:Drag two textboxes a two button to ur form.
Step 2 : Just enter the Download Link in Download Location textbox
Step 3 : Just enter the path where u want to save the file.
Note:Download Link path and save as path should be correct.
Step 4 :Just Click the save as Button
Refer the Image Below
VB.NET Code
Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
Try
If txtLocation.Text.Trim = String.Empty Then
MessageBox.Show("Please Enter the Location from where you want to download", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtLocation.Focus()
Exit Sub
ElseIf txtSaveas.Text.Trim.Trim = String.Empty Then
MessageBox.Show("Please Enter the Location to where you want to Save teh File", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSaveas.Focus()
Exit Sub
Else
My.Computer.Network.DownloadFile(txtLocation.Text, txtSaveas.Text & "\Santosh.jpg")
End If
Catch ex As Exception
MessageBox.Show("Please Enter the Correct Path", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSaveas.Focus()
End Try
End Sub

Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
C#.NET Code
private void btnSaveAs_Click(System.Object sender, System.EventArgs e)
{
try {
if (txtLocation.Text.Trim == string.Empty) {
MessageBox.Show("Please Enter the Location from where you want to download", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtLocation.Focus();
return;
} else if (txtSaveas.Text.Trim.Trim == string.Empty) {
MessageBox.Show("Please Enter the Location to where you want to Save teh File", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSaveas.Focus();
return;
} else {
My.Computer.Network.DownloadFile(txtLocation.Text, txtSaveas.Text + "\\Santosh.jpg");
}
} catch (Exception ex) {
MessageBox.Show("Please Enter the Correct Path", "San Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtSaveas.Focus();
}
}

private void btnClose_Click(System.Object sender, System.EventArgs e)
{
this.Close();
}

Generate the Password Randomly

Just follow the below code tp generate the password character randomly....
Step:1 just drag a text box into the form
Step:2 Just Drag a Button into the form

Step3: write the below method
VB.NET Code
Private Function GenerateRandomNo()
Dim s As String="kkdskkds87430900403kdsjj88ds88899d9sefghijkmnopqrstuvwxyzABCDEFrer"
Dim rand As New Random
Dim count As Integer = s.Length
Dim arr As Char() = New Char(6) {} 'defined the array
For i As Integer = 0 To 6 'array length
arr(i) = s(s.Length * rand.NextDouble)
Next
Return New String(arr)
End Function

Step 4:click the button
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
TextBox1.Text = GenerateRandomNo()
End Sub

C#.Net Code
public class frmRandomPassGen
{
//generate the password Randomly
private object GenerateRandomNo()
{
//just define some
string s = "hdaskkdskkds87430900499d9sefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRST";
Random rand = new Random();
int count = s.Length;
char[] arr = new char[7];
//defined the array
//array length
for (int i = 0; i <= 6; i++) {
arr[i] = s[s.Length * rand.NextDouble()];
}
return new string(arr);
}

private void btnGenerate_Click(System.Object sender, System.EventArgs e)
{
TextBox1.Text = GenerateRandomNo();
}
}