In this article I will explain with an example, how to populate (bind)
GridView using LocalDB in
ASP.Net.
Adding LocalDB in Project
1. Open Solution Explorer and click on Add after that right click on that and click Add New Item.
2. After clicking on Add New Item now click on SQL Server Database and set the name AjaxSamples.
3. In the next dialog window click on Yes button.
4. Here you can see LocalDB which name is AjaxSamples.
5. Now right click on Tables and click on New Query.
6. After clicking on New Query, write the following query.
CREATE TABLE [dbo].[Customers](
[CustomerId] [int]IDENTITY(1,1) NOT NULL,
[Name] [varchar](100) NOT NULL,
[Country] [varchar](50) NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
(
[CustomerId]ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT INTO Customers
SELECT 'John Hammond', 'United States'
UNION ALL
SELECT 'Mudassar Khan', 'India'
UNION ALL
SELECT 'Suzanne Mathews', 'France'
UNION ALL
SELECT 'Robert Schidner', 'Russia'
7. Then, execute the query.
8. Finally, after executing the query the Table has been created along with the records.
HTML Markup
The HTML Markup consists of:
GridView – For displaying data.
The GridView consists of three BoundField columns.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Binding GridView using (LocalDB) in ASP.Net
Inside the Page_Load event handler, the records are fetched from the Customers Table of LocalDB database.
Finally, the DataTable is assigned to the DataSource property of GridView and DataBind method is called.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string sql = "SELECT CustomerId, Name, Country FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End If
End Sub
Screenshot
Downloads