Whenever we create an asp.net application on that we always
come in a situation where we needed to display the data in the tabular form. So
asp.net has provided so many controls like GridView, DataGrid for representing
the data in tabular format.
So in this article i will show you how you can bind a grid view using C#.Net and VB.Net in asp.net.
So in this article i will show you how you can bind a grid view using C#.Net and VB.Net in asp.net.
In my previous article I have shown your How
to style input and submit button with CSS in Asp.net, Free
jQuery Spell Checker Plugins For Asp.Net. Have a look of some of my windows
application article like Email
Validation in Windows Application C#.Net and VB.Net | Validating Email ID in
TextBox in C# .Net, Displaying
More than One Month in the Windows Forms MonthCalendar Control Using C#.Net,
How
to get the selected date of a MonthCalendar control in C#.
Now for this article first we will create table in our data
base.
Now add the Access
data base in your App_Data folder.
Now we will create a new asp.net application and add a .aspx
form . Now in this form we will a GridView Control.
Now from Edit template option we will add Bind column in our
grid view.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Binding
Gridview By Access DataBase Using C#.Net in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Binding
Gridview By Access DataBase Using C#.Net in Asp.Net</h3>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
EmptyDataText="There are no data records to display." BorderStyle="Solid">
<Columns>
<asp:BoundField DataField="author_name" HeaderText="NAME" />
<asp:BoundField DataField="publisher_name" HeaderText="PUBLISHER NAME"
/>
<asp:BoundField DataField="publication_year" HeaderText="PUBLISH
YEAR" />
<asp:BoundField DataField="retail_price" HeaderText="PRICE" />
</Columns>
<HeaderStyle BackColor="#66CCFF" />
</asp:GridView>
</form>
</body>
</html>
|
Now in your .cs page add the below mention code.
C#.Net
C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace ProjectDemo_Asp.et
{
public partial class Default : System.Web.UI.Page
{
public
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
protected
void Page_Load(object
sender, EventArgs e)
{
DataTable
_objdt = new DataTable();
_objdt = GetDataFromDataBase();
if
(_objdt.Rows.Count > 0)
{
GridView1.DataSource =
_objdt;
GridView1.DataBind();
}
}
/// <summary>
/// Function for binding retribing the data from database
/// </summary>
/// <returns></returns>
public
DataTable GetDataFromDataBase()
{
DataTable
_objdt = new DataTable();
string
querystring = "select * from Books;";
OleDbConnection
_objcon=new OleDbConnection(connectionstring);
OleDbDataAdapter
_objda = new OleDbDataAdapter(querystring,
_objcon);
_objcon.Open();
_objda.Fill(_objdt);
return
_objdt;
}
}
}
|
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.OleDb
Imports System.Data
Namespace ProjectDemo_Asp.et
Partial Public Class [Default]
Inherits
System.Web.UI.Page
Public
connectionstring As String
= "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\bookstore.mdb;Persist Security Info=False;"
Protected
Sub Page_Load(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim
_objdt As New
DataTable()
_objdt = GetDataFromDataBase()
If
_objdt.Rows.Count > 0 Then
GridView1.DataSource = _objdt
GridView1.DataBind()
End
If
End Sub
'''
<summary>
'''
Function for binding retribing the data from database
'''
</summary>
'''
<returns></returns>
Public
Function GetDataFromDataBase() As DataTable
Dim
_objdt As New
DataTable()
Dim
querystring As String
= "select * from Books;"
Dim
_objcon As New
OleDbConnection(connectionstring)
Dim
_objda As New
OleDbDataAdapter(querystring, _objcon)
_objcon.Open()
_objda.Fill(_objdt)
Return
_objdt
End Function
End Class
End Namespace
|
Now we are ready to view the result.
DOWNLOAD
0 comments:
Please let me know your view