locked
Why Will the query not show/display in my gridview1 ? RRS feed

  • Question

  • User-525182193 posted

    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";


    }
    }

    Thursday, November 29, 2012 11:57 AM

Answers

  • User-665789584 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2012 12:33 PM

All replies

  • User-1716253493 posted
    mySQLCommand1.ExecuteQuery ();
    Thursday, November 29, 2012 12:04 PM
  • User-525182193 posted

    Were will i put that code?....also imgetting a red line under .ExectueQuery();

    Thursday, November 29, 2012 12:19 PM
  • User-665789584 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2012 12:33 PM
  • User3866881 posted

    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.

     

    Friday, November 30, 2012 12:45 AM
  • User-1199946673 posted

    And you should notice that you cannot bind a DataReader directly to GridView

    Yes, you can!

    Saturday, December 1, 2012 7:33 PM
  • User3866881 posted

    Hello,

    Please check whether your DataReader can really read out data from db.

    @Hans_v:

    Sorry I've mixed the concepts……

    Sunday, December 2, 2012 1:54 AM