Saturday, September 24

GridView DataBinding in

 This is one of the best article for a learner who is trying to bind data in a GridView Control.

Page Source:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="BasicGridView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">

<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">


File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;

public partial class BasicGridView : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!this.IsPostBack)
string connectionString = ConfigurationManager.ConnectionStrings["ShibaConn"].ConnectionString;
string selectSQL = "SELECT ID, Name, Price FROM Students ";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

adapter.Fill(ds, "Products");

GridView1.DataSource = ds;

File: Web.config

<?xml version="1.0"?>
<add name="ShibaConn" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"/>

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

shibashish mohanty