Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I'm trying to get my gridview to display on a button click but do not see where I am going wrong. On the button click there is nothing displaying on the page.

C#
protected void btnDisplay_Click(object sender, EventArgs e)
{
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\levels.mdb";
    DataSet ds;
    using (OleDbConnection myConnString = new OleDbConnection())
    {
        myConnString.ConnectionString = connString;
        using (OleDbCommand selectCommand = new OleDbCommand())
        {
            selectCommand.CommandText = "select * from tblTest";
            selectCommand.Connection = myConnString;
            myConnString.Open();
            using(OleDbDataAdapter da = new OleDbDataAdapter())
            {
                da.SelectCommand = selectCommand;
                ds = new DataSet();
                da.Fill(ds, "test");

                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }
}//end click event
Posted
Comments
idenizeni 21-Feb-14 16:00pm    
Questions: Are you sure the query returns results? My first guess would be your query isn't returning results, does tblTest have records in it? Are you sure the button event is fired? Is the GridView's Visible property set to false?

ds.Tables["test"];
or
ds.Tables[0];
 
Share this answer
 
It is happening because when you click the button the page is post back to the server and there you are with no data, as HTTP protocol is used.
So modify your code this way,
C#
protected void btnDisplay_Click(object sender, EventArgs e)
        {
if(!IsPostBack)
            string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\levels.mdb";
            DataSet ds;
            using (OleDbConnection myConnString = new OleDbConnection())
            {
                myConnString.ConnectionString = connString;
                using (OleDbCommand selectCommand = new OleDbCommand())
                {
                    selectCommand.CommandText = "select * from tblTest";
                    selectCommand.Connection = myConnString;
                    myConnString.Open();
                    using(OleDbDataAdapter da = new OleDbDataAdapter())
                    {
                        da.SelectCommand = selectCommand;
                        ds = new DataSet();
                        da.Fill(ds, "test");
 
                        GridView1.DataSource = ds;
                        GridView1.DataBind();
                    }                    
                }                
            }          
}     
        }//end click event
 
Share this answer
 
Hi,

You have coded like this,

selectCommand.CommandText = "select * from tblTest";

da.Fill(ds, "test");

Here you need give the actual name of the selected table. Like this

da.Fill(ds, "tblTest");

Please try this and update me,
 
Share this answer
 
C#
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=emp;Integrated Security=True");
    SqlDataAdapter sda;
    protected void Button1_Click(object sender, EventArgs e)
    {
        sda=new SqlDataAdapter("select * from tbl_emp",con);
        DataSet ds=new DataSet();
        sda.Fill(ds);
        GridView1.DataSource=ds.Tables[0];
        GridView1.DataBind();
     
    }
 
Share this answer
 
v3
Try this

I hope it i'll help you

protected void btnDisplay_Click(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\levels.mdb";
DataTable dt;
using (OleDbConnection myConnString = new OleDbConnection())
{
myConnString.ConnectionString = connString;
using (OleDbCommand selectCommand = new OleDbCommand())
{
selectCommand.CommandText = "select * from tblTest";
selectCommand.Connection = myConnString;
myConnString.Open();
using(OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = selectCommand;
ds = new DataSet();
da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}//end click event
 
Share this answer
 
Try this

protected void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(@"ConnectionString");
SqlDataAdapter dataAdapter;
DataSet dataSet=new DataSet();

dataAdapter=new SqlDataAdapter("select * from tbl_emp",sqlConnection);
dataAdapter.Fill(dataSet,'Employee');
GridView1.DataSource=dataSet.Tables['Employee'];
GridView1.DataBind();
}//end click event
 
Share this answer
 
Check your (ds.Tables[0].Rows.Count >0) or not and then
replace
C#
GridView1.DataSource = ds.Tables[0];
 GridView1.DataBind();

in place of

 GridView1.DataSource = ds;
 GridView1.DataBind();
 
Share this answer
 
v3
I think your is not return any result ...Chech your dataset in step by step compilation(f11)
 
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