This article will show you how you can bind a gridview
control in asp.net using c#.net by datatable,SqlDataAdapter,SqlConnection
without adding bind column.
Some of my previous articles are as follows: Save
Or Insert TextBox Value into Sql DataBase Table In Asp.Net Using C#.Net, AjaxControlToolkit
CalendarExtender to Display Calendar in Asp.net on Textbox Click, Search
GridView Record on Button Click By Using C#.Net in Asp.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, User
Registration Form With ToolTip Message and Validation Using jQuery, C#.Net in
Asp..Net.
So for this article first I have created a sql table with
some data.
Here is the table script
/****** Object:
Table [dbo].[UserDetail]
Script Date: 11/05/2014 23:17:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[UserDetail](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Address]
[varchar](100)
NULL,
CONSTRAINT [PK_UserDetail] PRIMARY
KEY CLUSTERED
(
[Id] 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
|
Now I have created an asp.net application and add the below
code into your page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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>GridView
Bind By Sql Table Using DataTable,SqlDataAdapter,SqlConnection In Asp.Net
Using C#
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<b>USER DETAIL</b>
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
After this we will add out connection string into web.config
file.
<connectionStrings>
<add name="con" connectionString="Data Source=DELL-PC;Initial Catalog=Demo;Integrated
Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
|
Now add the below code into .cs page.
using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
SqlConnection
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
}
}
|
In above code I have used sql connection, datatable and sql
dataadaptor. After this I have fill the datatable with data. And check whether we
are having any data in data table or not if data count is greater than 0 then
gridview will bind.
Now we have done run the application and check the output.
0 comments:
Please let me know your view