Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>



        <asp:GridView ID="GridView1" runat="server">

        </asp:GridView>



    </div>
    </form>
</body>
</html>




c#




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


public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{
string strSQLconnection = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");

SqlConnection sqlConnection = new SqlConnection(strSQLconnection);

SqlCommand sqlCommand = new SqlCommand("select * from persons", sqlConnection);

sqlConnection.Open();



SqlDataReader reader = sqlCommand.ExecuteReader();



GridView1.DataSource = reader;

GridView1.DataBind();

}


}
Posted
Updated 6-Nov-19 17:00pm
Comments
Abhijit Parab 24-Sep-12 3:11am    
What you want? what is the problem you are facing?
Rickin Kane 24-Sep-12 3:54am    
he is showing his gridview databind capability :)
Samer Aburabie 24-Sep-12 4:47am    
lo0o0ol .. I think he is :)
Rickin Kane 24-Sep-12 3:55am    
let me complete your question , i am trying to bind the record from Person table , but when my page Load finish , my gridview is empty , where i am going wrong
Tejas Vaishnav 24-Sep-12 5:49am    
One thing before asking such question please try to search on google to figure out your problem. and make some sense to ask question in proper way so other can understand what you want to ask.

This tutorial helped me to create gridview

Asp.Net Gridview tutorial

Eric
 
Share this answer
 
Try this code if it will solve your problem
or refer this link GridView Databinging demo[^]

C#
string strSQLconnection = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
SqlCommand sqlCommand = new SqlCommand("select * from persons", sqlConnection);
sqlConnection.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCommand);
DataTable dt = new DataTable();
sqlDa.Fill(dt);
sqlConnection.Close();
GridView1.DataSource = dt;
GridView1.DataBind();


OR
C#
string strSQLconnection = (@"Data Source=.;Initial Catalog=new;Integrated Security=True");
SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
sqlConnection.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter();
sqlDa.SelectCommand.Connection = sqlConnection;
sqlDa.SelectCommand.CommandType = CommandType.Text;
sqlDa.SelectCommand.CommandText = "YOUR SELECT QUERY";
DataTable dt = new DataTable();
sqlDa.Fill(dt);
sqlConnection.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900