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

  • Question

  • 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 4:49 PM

Answers

  • Hello,

    A DataReader is used to read once thru the data collected when executing ExecuteReader of the command object. Your best option is to use option 2 from Enrique. Once you have executed the load method of the DataTable take a look at the data by placing a break-point on a line directly after the load method has executed. Place you mouse over a line which has myTable (as per Enrique's suggestion) and a small window will appear, click on the magnifier which invokes a data visualizer to see if there is data in the table.


    KSG

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:20 AM
    Tuesday, December 4, 2012 1:46 PM

All replies

  • Not sure but things you can try:

    1) Set GridView1.AutoGenerateColumns = true;

    2) Create a DataTable and use it as data source instead the datareader:

    DataTable myTable = new DataTable();

    myTable.Load(myDataReader);

    GridView1.DataSource = myTable;

    3) Have you debugged the code step by step ? what path it takes?


    -

    Friday, November 30, 2012 11:29 AM
  • Hello,

    A DataReader is used to read once thru the data collected when executing ExecuteReader of the command object. Your best option is to use option 2 from Enrique. Once you have executed the load method of the DataTable take a look at the data by placing a break-point on a line directly after the load method has executed. Place you mouse over a line which has myTable (as per Enrique's suggestion) and a small window will appear, click on the magnifier which invokes a data visualizer to see if there is data in the table.


    KSG

    • Marked as answer by Alexander Sun Friday, December 7, 2012 1:20 AM
    Tuesday, December 4, 2012 1:46 PM