I am using a datagridview for the output of the results and I have made 4 combobox each have its condition, asceding and descending, starts and contains with, search from table..

I am writing this with lots of code, is there any way to make this short?

string bncw = "select * from Book where Book_Title LIKE '%' + @searchinput + '%'";
            string bncwa = "select * from Book where Book_Title LIKE '%' + @searchinput + '%'";
            string bnbw = "select * from Book where Book_Title LIKE ' +@searchinput +'%'";

            if (cb2.SelectedItem.ToString() == "Book")
            {
                cmd = new SqlCommand(bncw, con);
                cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];

                if (cb1.SelectedItem.ToString() == "Name contains with")
                {
                    cmd = new SqlCommand(bncw, con);
                    cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];

                    if (cb3.SelectedItem.ToString() == "Ascending Order")
                    {

                    }
                }
            }
            else if (cb1.SelectedItem.ToString() == "Name begins with")
            {
                cmd = new SqlCommand(bnbw, con);
                cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }

I have a wildcard query here too so this form for searching will contain lots of code.

You need to make your query dynamic based on the selection of your combo boxes.. take a look in below code it may help you...

string strQuery = "";
string strOrderBy = "";
string strWhere = "";

strQuery = "Select * from " + searchfromtablecombo.SelectedItem.Text;
strWhere = " Where filedname = conditin"
strOrderBy = "Order By col1 " + ascdesccombo.SeletedItem.Text

strQuery += strWhere += strOrderBy
objAdpt = new SqlDataAdapter(strQuery, your connection);
DataTable objDt = new DataTable();
objAdpt.Fill(objDt);
GridView1.DataSource = objDt;
GridView1.DataBind();

try by this and let us know...Also if possible give the option your combo boxes having...

I am using a datagridview for the output of the results and I have made 4 combobox each have its condition, asceding and descending, starts and contains with, search from table..

I am writing this with lots of code, is there any way to make this short?

string bncw = "select * from Book where Book_Title LIKE '%' + @searchinput + '%'";
            string bncwa = "select * from Book where Book_Title LIKE '%' + @searchinput + '%'";
            string bnbw = "select * from Book where Book_Title LIKE ' +@searchinput +'%'";

            if (cb2.SelectedItem.ToString() == "Book")
            {
                cmd = new SqlCommand(bncw, con);
                cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];

                if (cb1.SelectedItem.ToString() == "Name contains with")
                {
                    cmd = new SqlCommand(bncw, con);
                    cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];

                    if (cb3.SelectedItem.ToString() == "Ascending Order")
                    {

                    }
                }
            }
            else if (cb1.SelectedItem.ToString() == "Name begins with")
            {
                cmd = new SqlCommand(bnbw, con);
                cmd.Parameters.Add("@searchinput", SqlDbType.NVarChar).Value = inputsearch.Text;
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.