connectseekers.com

Wednesday, 5 November 2014

GridView Bind By Sql Table Using DataTable,SqlDataAdapter,SqlConnection In Asp.Net Using C#

11/05/2014 - By Pranav Singh 0

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.



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.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top