fetching the data into gridview in asp.net [Resolved]

Posted by Sumasri under ASP.NET on 7/24/2012 | Points: 10 | Views : 3389 | Status : [Member] | Replies : 5
hii
Im a beginner in asp.net. Can u pls help me that how to fetch the data into gridview from sql in asp.net. Pls reply me asap.

Thanks & Regards
Suma

sumasri


Responses

Posted by: Ranjeet_8 on: 7/25/2012 [Member] [MVP] Gold | Points: 50

Up
0
Down

Resolved
using System.Data.SqlClient;

using System.Data;


SqlConnection con = new SqlConnection("Server=YourServerName;Initial Catalog=DatabaseName;User=UserName;password=YourPassword");

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
try
{
con.Open();

SqlDataAdapter da = new SqlDataAdapter("select * from Table_Name", con);
DataTable ds = new DataTable();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
finally
{
con.Close();
}
}



Sumasri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/25/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

can you please tell something more about it ,It is not clear

R D Patel

Sumasri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 7/25/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
hi... Sumasri
try this above code to fetch the data in gridview from sql.
if code will help u then Mark As Answer.

Sumasri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupentiwari on: 7/25/2012 [Member] Starter | Points: 25

Up
0
Down
Hi refer this

http://www.dotnetfunda.com/articles/article1594-how-to-populate-gridview-from-code-behind.aspx

definitely it will help you

Thanks n Regards
Bhupendra Tiwari

Sumasri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sumasri on: 7/25/2012 [Member] Starter | Points: 25

Up
0
Down
thank u ranjit.

sumasri

Sumasri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response