Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

How to bind a grid view.
My code is as follows.
But it is not displaying a grid.

Plz help
C#
protected void Page_Load(object sender, EventArgs e)
        {            
            //string connection="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True";
            string connection = "Data Source=path;Initial Catalog=commonlogin;User ID=sa;Password=; ";

            SqlConnection conn = new SqlConnection(connection);
            conn.Open();
            SqlCommand com = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);

            SqlDataReader reader = com.ExecuteReader();
            
            GridView1.DataSource = reader;
            GridView1.DataBind();
        }

-------------------------
Plz suggest me correct code
Posted
Updated 14-Jun-12 5:38am
v3

try this :
protected void Page_Load(object sender, EventArgs e)
{

//string connection="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True";
string connection = "Data Source=path;Initial Catalog=commonlogin;User ID=sa;Password=; ";
 

SqlConnection conn = new SqlConnection(connection);
conn.Open();
SqlDataAdapter ad= new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
Dataset ds=new Dataset();
ad.fill(ds);

if(ds.tables[0].rows.count!=0)
{
          Gridview1.Datasource=ds;
          Gridview1.Databind();
}
Hope this will help you or if not Please the Post error which is generated.
Enjoy.
 
Share this answer
 
Comments
Nikil0012 14-Jun-12 6:31am    
I get this error: A field or property with the name 'RequestID' was not found on the selected data source.
On Gridview1.datbind();

Plz help
Nikil0012 14-Jun-12 6:35am    
It doesn't display the grid view plz help, i have no data in grid view, and when i insert it it gives above error RequestID
[no name] 14-Jun-12 8:25am    
What does your grid contain?
I mean are you autogenerating columns or have you specified the columns to be displayed.
Please try AutoGenerateColumns="true" in gridview, this will enable you to check if the gridview is bound properly or not.
Next you can specify the columns you need to display along with the headers.

Hope this helps you out.
happy coding!
Cheers
C#
SqlCommand com = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);

 SqlDataAdapter adap = new SqlDataAdapter(com );
            DataTable dt = new DataTable();
            adap.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();


you can see an easy example in this link


C# SqlDataAdapter
 
Share this answer
 
What does your grid contain?
I mean are you autogenerating columns or have you specified the columns to be displayed.
You might be trying to get a column that the query is not returning
Please try AutoGenerateColumns="true" in gridview, this will enable you to check if the gridview is bound properly or not.
Next you can specify the columns you need to display along with the headers.

Hope this helps you out.
happy coding!
Cheers
 
Share this answer
 
 
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