Hi SUJAYS,
HTML
<asp:GridView runat="server" AutoGenerateColumns="false" ID="gvCustomers">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
Connection.cs
using System.Configuration;
public class Connection
{
public string GetConnectionString()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
return constring;
}
}
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Connection connectioString = new Connection();
using (SqlConnection con = new SqlConnection(connectioString.GetConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
}
VB.Net
ConnectionVB.vb
Imports Microsoft.VisualBasic
Imports System.Configuration
Public Class ConnectionVB
Public Function GetConnectionString() As String
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Return constring
End Function
End Class
VB.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim connectioString As ConnectionVB = New ConnectionVB()
Using con As SqlConnection = New SqlConnection(connectioString.GetConnectionString())
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Screenshot