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.
None
0 Points
2 Posts
Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 11:57 AM|ret123|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";
}
}
All-Star
52823 Points
15770 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 12:04 PM|oned_gk|LINK
Suwandi - Non Graduate Programmer
None
0 Points
2 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 12:19 PM|ret123|LINK
Were will i put that code?....also imgetting a red line under .ExectueQuery();
Member
677 Points
240 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 29, 2012 12:33 PM|msmk|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.
All-Star
94130 Points
18109 Posts
Re: Why Will the query not show/display in my gridview1 ?
Nov 30, 2012 12:45 AM|Decker Dong - MSFT|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.
All-Star
25756 Points
7014 Posts
Re: Why Will the query not show/display in my gridview1 ?
Dec 01, 2012 07:33 PM|hans_v|LINK
Yes, you can!
All-Star
94130 Points
18109 Posts
Re: Why Will the query not show/display in my gridview1 ?
Dec 02, 2012 01:54 AM|Decker Dong - MSFT|LINK
Hello,
Please check whether your DataReader can really read out data from db.
@Hans_v:
Sorry I've mixed the concepts……