Reputation: 3
I have created a Label control and gridview. Label shows the data but In Gridview data from database does not populate. Following is my code. No error is received while doing this
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("Select BATSMAN_NAME from RUNS_STATS", con);
con.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
Label1.Text = dr.GetString(dr.GetOrdinal("BATSMAN_NAME"));
}
GridView2.DataSource = dr;
GridView2.DataBind();
con.Close();
}
}
}
Upvotes: 0
Views: 767
Reputation:
Try disconnected architecture. Incase the following code doesn't work, check if the Query is appropriate.
SqlConnection conobj=new SqlConnection;
SqlCommand cmdobj=new SqlCommand(""Select BATSMAN_NAME from RUNS_STATS", conobj);
SQlDataAdapter sdaobj=new SqlDataAdapter(cmdobj);
DataTable dtobj=new DataTable();
sdaobj.Fill(dtobj);
GridView2.DataSource=dtobj;
GridView2.DataBind();
Upvotes: 0
Reputation: 168
Use SqlDataAdapter & dataset to fill gridview
SqlCommand cmd = new SqlCommand("Select BATSMAN_NAME from RUNS_STATS", con);
SqlDataAdapter daGrid = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
daGrid.Fill(ds);
GridView2.DataSource = ds.Tables[0];
GridView2.DataBind();
Upvotes: 1