locked
dataAdapter parameter RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I am trying to add a parameter to an sql select query using dataAdapter.

     I can;t seem to make it work. 

    This is my code:

    using (System.Data.OleDb.OleDbConnection JETconn = new System.Data.OleDb.OleDbConnection(connString))
                        {
                            try
                            {
                                JETconn.Open();
                                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(recStrQuery, JETconn))
                                {
                                    dataAdapter.SelectCommand.Parameters.Add("@searchTextbox"OleDbType.VarWChar).Value = Session["searchVal"];
                                    dataAdapter.Fill(ds);
                                }
                                
     
                            }
                            catch (Exception ex)
                            {
                                //MessageBox.Show("Failed to connect to data source");
                                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "get_aDataset""alert(" + gh.EncodeJsString("Failed to connect to data source: " + ex.Message + "\n" + recStrQuery) + ");"true);
                            }
                        }

    The recStrQuery value is

    "Select * from tt where searchfield like '%@searchTextbox%'"
    

    Tuesday, August 28, 2018 11:28 AM

Answers

  • User632428103 posted

    Hello qsoft developer

    the problem comes to your select command replace this :

    "Select * from tt where searchfield like '%@searchTextbox%'"
    "Select * from tt where searchfield like '%' + @searchTextbox + '%' "
    

    try and tell us, here is it a little sample with Northwind Database as a select command with a parameter

    DataSet ds = new DataSet();
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString))
                    {
                        string myCommand = "select * from dbo.Orders where customerId like @search + '%'";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(myCommand, conn))
                        {
                            adapter.SelectCommand.Parameters.AddWithValue("@search", "vi");
                            adapter.Fill(ds);
                        }
                    }

    when you meet this problem, no data return get the command who is send to the database and execute it ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 28, 2018 1:30 PM

All replies

  • User632428103 posted

    Hello qsoft developer

    the problem comes to your select command replace this :

    "Select * from tt where searchfield like '%@searchTextbox%'"
    "Select * from tt where searchfield like '%' + @searchTextbox + '%' "
    

    try and tell us, here is it a little sample with Northwind Database as a select command with a parameter

    DataSet ds = new DataSet();
                    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString))
                    {
                        string myCommand = "select * from dbo.Orders where customerId like @search + '%'";
                        using (SqlDataAdapter adapter = new SqlDataAdapter(myCommand, conn))
                        {
                            adapter.SelectCommand.Parameters.AddWithValue("@search", "vi");
                            adapter.Fill(ds);
                        }
                    }

    when you meet this problem, no data return get the command who is send to the database and execute it ..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 28, 2018 1:30 PM
  • User-1171043462 posted

    Are you getting any error?

    Tuesday, August 28, 2018 1:43 PM
  • User1310055179 posted

    Thanks a lot! 

    It fixed my problem

    Wednesday, August 29, 2018 6:17 AM