locked
search more than one field RRS feed

  • Question

  • c # from parameter with, I want to search in more than one textbox.
    Thanks;
    • Moved by Bob Beauchemin Sunday, September 21, 2014 7:07 PM Moved to the forum for client-side ADO.NET for best results
    Sunday, September 21, 2014 4:16 PM

Answers

  • Hello,

    >>in this address but not with linq?

    So it seems that you need a dynamic query, from your provided link, in ADO.NET, it should be similar with below code:

    string connectionString = @"Server=(localdb)\Projects;Database=DFDB;Trusted_Connection=True;";
    
                SqlConnection connection = new SqlConnection(connectionString);
    
                string sql = string.Empty;
    
                string odName = "7";
    
                string oId = string.Empty;
    
                try
    
                {
    
                    SqlCommand cmd = new SqlCommand();
    
                    sql = "select * from [OrderDetail] where 1=1 ";
    
                    if (oId != string.Empty)
    
                    {
    
                        sql += "and OrderID = @OrderID";
    
                        cmd.Parameters.Add("@OrderID", oId);
    
                    }
    
                    if (odName != string.Empty)
    
                    {
    
                        sql += "and OrderDetailName = @ODName";
    
                        cmd.Parameters.Add("@ODName", odName);
    
                    }
    
                    connection.Open();
    
                    cmd.CommandText = sql;
    
                    cmd.Connection = connection;
    
    
    
                    DataSet ds = new DataSet();
    
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                    da.Fill(ds);
    
                }
    
                catch (Exception)
    
                {
    
                }
    
                finally
    
                {
    
                    connection.Close();
    
                }
    

    You could replace my clause with yours.

    If I misunderstand, please let me know.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kenan YILMAZ Monday, September 22, 2014 11:27 AM
    • Unmarked as answer by Kenan YILMAZ Monday, September 22, 2014 11:33 AM
    • Marked as answer by Kenan YILMAZ Monday, September 22, 2014 11:33 AM
    Monday, September 22, 2014 8:32 AM

All replies

  • Hello Kenan,

    From your current description, it seems that you want to write a query with multiple filter fileds in where clause using ADO.NET.

    If I understand you correctly, you could check this blog written by Wriju Ghosh, it provides an example which searches database with two fileds. Although it uses the System.Data.OleDb, you could completely replace it with Syatem.Data.SqlClient if you are using SQL Server database.

    Replacing the sql statement with yours and add yourselves parameters with their values:

    String sSQL = "Your Command";
    
    cmd.Parameters.AddWithValue("@YoutParamterName", "YourParameterValue");
    

    If I do not understand you correctly, please feel free to let me know.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, September 22, 2014 5:46 AM
  • no problem write queries;
    four textbox has control on them
    I want to make the query by entering a value.

    as 1 and 4 or 2 and 4

    Monday, September 22, 2014 6:41 AM
  • no problem write queries;
    four textbox has control on them
    I want to make the query by entering a value.

    in this address but not with linq?

    Monday, September 22, 2014 6:44 AM
  • Hello,

    >>in this address but not with linq?

    So it seems that you need a dynamic query, from your provided link, in ADO.NET, it should be similar with below code:

    string connectionString = @"Server=(localdb)\Projects;Database=DFDB;Trusted_Connection=True;";
    
                SqlConnection connection = new SqlConnection(connectionString);
    
                string sql = string.Empty;
    
                string odName = "7";
    
                string oId = string.Empty;
    
                try
    
                {
    
                    SqlCommand cmd = new SqlCommand();
    
                    sql = "select * from [OrderDetail] where 1=1 ";
    
                    if (oId != string.Empty)
    
                    {
    
                        sql += "and OrderID = @OrderID";
    
                        cmd.Parameters.Add("@OrderID", oId);
    
                    }
    
                    if (odName != string.Empty)
    
                    {
    
                        sql += "and OrderDetailName = @ODName";
    
                        cmd.Parameters.Add("@ODName", odName);
    
                    }
    
                    connection.Open();
    
                    cmd.CommandText = sql;
    
                    cmd.Connection = connection;
    
    
    
                    DataSet ds = new DataSet();
    
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
    
                    da.Fill(ds);
    
                }
    
                catch (Exception)
    
                {
    
                }
    
                finally
    
                {
    
                    connection.Close();
    
                }
    

    You could replace my clause with yours.

    If I misunderstand, please let me know.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kenan YILMAZ Monday, September 22, 2014 11:27 AM
    • Unmarked as answer by Kenan YILMAZ Monday, September 22, 2014 11:33 AM
    • Marked as answer by Kenan YILMAZ Monday, September 22, 2014 11:33 AM
    Monday, September 22, 2014 8:32 AM