ExecuteNonQuery is used when no data is being returned, for example a delete, update or insert statement.
If data will be returned, use ExecuteReader. For example in your case where the select statement will return data. you already have this statement on the next line.
If only one cell is returned (ie. one 'peice' of data) you can use ExecuteScalar.
First I'd like to say that I don't see anything obvious wrong with your codes……So please make sure that you GridView supports DataReader:
And you should notice that you cannot bind a DataReader directly to GridView, and you have to bind a DataTable to that.
If you wanna increase the speed, I think you can just try this by drag and drop a SqlDataReader onto the Web page, and then set its
DataSourceMode = DataReader.
ret123
0 Points
2 Posts
Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 03:57 PM|LINK
I changed the txtserachbox.text to SQL Ran so i know it has ran but the result still wont apper in the grid view
*******
protected void btnsearch_Click(object sender, EventArgs e)
{
string conn_string = @"Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=C:\Users\s00101157\Desktop\House100DataBase.accdb;
Persist Security Info=False;";
using (OleDbConnection connection1 = new OleDbConnection(conn_string))
{
string query1 = "select * from song_tbl where sTitle = @searchQuary ";
OleDbCommand mySQLCommand1 = new OleDbCommand(query1, connection1);
OleDbDataReader myDataReader;
mySQLCommand1.Parameters.AddWithValue("@searchQuary", txtSearchbar.Text);
connection1.Open();
mySQLCommand1.ExecuteNonQuery();
myDataReader = mySQLCommand1.ExecuteReader();
if (myDataReader != null)
{
txtSearchbar.Text = "SQL Ran";
GridView1.DataSource = myDataReader;
GridView1.DataBind();
}
else
{
txtSearchbar.Text = "No Matchfound";
}
}
oned_gk
All-Star
31007 Points
6347 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 04:04 PM|LINK
ret123
0 Points
2 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 04:19 PM|LINK
Were will i put that code?....also imgetting a red line under .ExectueQuery();
msmk
Participant
776 Points
158 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 04:33 PM|LINK
Remove mySQLCommand1.ExecuteNonQuery();
ExecuteNonQuery is used when no data is being returned, for example a delete, update or insert statement.
If data will be returned, use ExecuteReader. For example in your case where the select statement will return data. you already have this statement on the next line.
If only one cell is returned (ie. one 'peice' of data) you can use ExecuteScalar.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 30, 2012 04:45 AM|LINK
Hello ret123,
First I'd like to say that I don't see anything obvious wrong with your codes……So please make sure that you GridView supports DataReader:
And you should notice that you cannot bind a DataReader directly to GridView, and you have to bind a DataTable to that.
If you wanna increase the speed, I think you can just try this by drag and drop a SqlDataReader onto the Web page, and then set its DataSourceMode = DataReader.
hans_v
All-Star
35986 Points
6550 Posts
Re: Why Will the query not show/display in my gridview1 ?
Dec 01, 2012 11:33 PM|LINK
Yes, you can!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Why Will the query not show/display in my gridview1 ?
Dec 02, 2012 05:54 AM|LINK
Hello,
Please check whether your DataReader can really read out data from db.
@Hans_v:
Sorry I've mixed the concepts……