Hi PRA,
Check this example. Now please take its reference and correct your code.
Using the below article i have created the example.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
BindCustomerId();
}
private void BindCustomerId()
{
DataTable dt = GetData();
dgCustomerId.AutoGenerateColumns = false;
dgCustomerId.ColumnCount = 1;
dgCustomerId.Columns[0].Name = "CustomerId";
dgCustomerId.Columns[0].HeaderText = "Customer Id";
dgCustomerId.Columns[0].DataPropertyName = "CustomerID";
dgCustomerId.DataSource = dt;
}
private void BindCustomerDetails()
{
DataTable dt = GetData();
dgCustomerDetails.AutoGenerateColumns = false;
dgCustomerDetails.ColumnCount = 2;
dgCustomerDetails.Columns[0].HeaderText = "Contact Name";
dgCustomerDetails.Columns[0].Name = "ContactName";
dgCustomerDetails.Columns[0].DataPropertyName = "ContactName";
dgCustomerDetails.Columns[1].Name = "Country";
dgCustomerDetails.Columns[1].HeaderText = "Country";
dgCustomerDetails.Columns[1].DataPropertyName = "Country";
dgCustomerDetails.DataSource = dt;
}
private DataTable GetData()
{
string constring = @"Data Source=.;Initial Catalog=Northwind;User id = sa;password=pass@123";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 CustomerId,ContactName,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
private void btnShow_Click(object sender, EventArgs e)
{
BindCustomerDetails();
}
VB.Net
Public Sub New()
InitializeComponent()
BindCustomerId()
End Sub
Private Sub BindCustomerId()
Dim dt As DataTable = GetData()
dgCustomerId.AutoGenerateColumns = False
dgCustomerId.ColumnCount = 1
dgCustomerId.Columns(0).Name = "CustomerId"
dgCustomerId.Columns(0).HeaderText = "Customer Id"
dgCustomerId.Columns(0).DataPropertyName = "CustomerID"
dgCustomerId.DataSource = dt
End Sub
Private Sub BindCustomerDetails()
Dim dt As DataTable = GetData()
dgCustomerDetails.AutoGenerateColumns = False
dgCustomerDetails.ColumnCount = 2
dgCustomerDetails.Columns(0).HeaderText = "Contact Name"
dgCustomerDetails.Columns(0).Name = "ContactName"
dgCustomerDetails.Columns(0).DataPropertyName = "ContactName"
dgCustomerDetails.Columns(1).Name = "Country"
dgCustomerDetails.Columns(1).HeaderText = "Country"
dgCustomerDetails.Columns(1).DataPropertyName = "Country"
dgCustomerDetails.DataSource = dt
End Sub
Private Function GetData() As DataTable
Dim constring As String = "Data Source=.;Initial Catalog=Northwind;uid= sa;password=pass@123"
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 CustomerId,ContactName,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Private Sub button1_Click(sender As System.Object, e As System.EventArgs) Handles button1.Click
BindCustomerDetails()
End Sub
Screenshot