Reputation: 41
I am building a web form. I have one search field and a search button. I am connecting to MS Access database to retrieve and display the result on the grid view. But my grid view is not appearing on the web page.
Can anyone please help me to find out where I am wrong?
Here is my aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Smita\\Desktop\\Project.accdb");
DataTable dt = new DataTable() ;
if (txtMerchant.Text.Length > 0)
{
con.Open();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
DBAdapter.SelectCommand = new OleDbCommand("select * from Test where Merchant ID like '" + txtMerchant.Text + "%'", con);
DBAdapter.Fill(dt);
GridView1.DataSource = dt;
}
Upvotes: 0
Views: 42
Reputation: 1318
You have to call the DataBind, binding method first after you assign the data source. Like that:
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
Upvotes: 1
Reputation:
GridView1.DataSource = dt; //Assigned a blank table.
"dt" Doesn't seem to point to anything.
Upvotes: 0