Sunday, July 1, 2012

How to Bind data to gridview in asp.net

Title: How to bind data to grid view in asp.net using c#.net

Description:
This is the basic concept of the data binding to data source controls in asp.net.Now i would like to explain and given example on bind data to drop down list in grid view,In this post i will explain how to bind the data to grid view example in asp.net.Here the stored procedure has been used to get the data from sql database.In the same way we can see how to call a stored procedure in this example

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Bind data to gridview in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grvdoc" AllowSorting="True" Runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>

Code Behind:

protected void Page_Load(Object Sender, EventArgs E) 
{
SqlConnection con=new SqlConnection("Data Source=Test;Initial Catalog=binddata;Integrated Security=true")
SqlCommand cmd = new SqlCommand("fiilldocgridview", con);
cmd.CommandType = CommandType.StoredProcedure;con.Open();
SqlDataAdapter ad = new SqlDataAdapter(cmd);DataSet ds = new DataSet();
try
{
da = new SqlDataAdapter(cmd);
da.Fill(ds, "DoctorsTable");
grvdoc.DataSource = ds;
grvdoc.DataBind();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
}
}
filldocgridview Stored Procedure:

CREATE PROCEDURE [dbo].[fiilldocgridview]
AS
SELECT * FROM  binddata
Post a Comment

Bel