Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
anyone please give idea How can i reduce search code......


C#
private void cmdsearch_Click(object sender, EventArgs e)
        {
           
            
            if (comboBox1.Text=="Name")
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where name='" + textBox3.Text + "'", conn);

                DataTable tbl = new DataTable();
                adp.Fill(tbl);
                dataGridView1.DataSource = tbl;
              
            }
            else if (comboBox1.Text == "ID")
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id='" + int.Parse(textBox3.Text) + "'", conn);
                DataTable tbl = new DataTable();
                adp.Fill(tbl);
                dataGridView1.DataSource = tbl;
              
            }
else if (comboBox1.Text == "Contactno")
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where contactno='" + textBox3.Text + "'", conn);
                DataTable tbl = new DataTable();
                adp.Fill(tbl);
                dataGridView1.DataSource = tbl;
              
            }
else if (comboBox1.Text == "Post")
            {
                SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where post='" + textBox3.Text+ "'", conn);
                DataTable tbl = new DataTable();
                adp.Fill(tbl);
                dataGridView1.DataSource = tbl;
              
            }
Posted
Updated 24-Mar-14 1:45am
v2

Should be like this:

C#
private void cmdsearch_Click(object sender, EventArgs e)
{
SqlDataAdapter adp = new SqlDataAdapter(string.Format("select * from test1 where {0}='{1}'", comboBox1.Text, textBox3.Text), conn);
//
DataTable tbl = new DataTable();
adp.Fill(tbl);
dataGridView1.DataSource = tbl;
}
 
Share this answer
 
v2
Comments
manish7664 24-Mar-14 8:15am    
where {0}='{1}'" could you please explain..
Raul Iloc 24-Mar-14 8:19am    
In my code there is a string.Format() used to create the SQL, and this format method has two parameters placeholders {0} and {1}; the value of comboBox1.Text and textBox3.Text will replace these two placeholders in the string.
Raul Iloc 24-Mar-14 8:30am    
So did you try my solution?
Raul Iloc 24-Mar-14 8:40am    
The code from my solution is from your posting.
It seems that for you is not cleat the string.Format(), so please have a look in Help/MSDN/Google for some example with string.Format() and should be more clear for you.
manish7664 26-Mar-14 9:35am    
Mr Raul . i need your help.
i have two form.in first form have two textbox and one find button and second form have one datagrid.
i want when i click form 1 find button then form 2 datagrid open and if i click any perticular datagrid records cell then selected record show in form 1 textbox
i use below code but not working .

// Form 1

private void cmdfind_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();

}
//Form 2

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
Form2 form2 = new Form2();
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
form2.textBox1.Text = row.Cells[0].Value.ToString();
form2.textBox2.Text = row.Cells[1].Value.ToString();
}
First off, Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Then it's pretty simple:
C#
private void cmdsearch_Click(object sender, EventArgs e)
    {
    switch (comboBox1.Text.ToLower())
        {
        case "name":
        case "id":
        case "contactno":
        case "post":
            SqlDataAdapter adp = new SqlDataAdapter("SELECT * FROM test1 WHERE " + comboBox1.Text + "=@PAR", conn);
            adp.SelectCommand.Parameters.AddWithValue("@PAR", textBox3.Text);
            DataTable tbl = new DataTable();
            adp.Fill(tbl);
            dataGridView1.DataSource = tbl;
            break;
        }
    }
 
Share this answer
 
v2
Comments
manish7664 24-Mar-14 9:24am    
Thanks but it's not working
OriginalGriff 24-Mar-14 9:47am    
That's a helpful error message!
What is it doing that you didn't expect, or not doing that you did?
What did you do to get those results?
OriginalGriff 24-Mar-14 9:48am    
Hang on, hang on... try the modified version - I used textBox3 twice! :laugh:
declare string variable
string sql = string.empty;

put string inside the if block and assign the sql statement

Move the following lines outside.

C#
SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
DataTable tbl = new DataTable();
adp.Fill(tbl);
dataGridView1.DataSource = tbl;
 
Share this answer
 
Comments
manish7664 24-Mar-14 8:29am    
Mr Arun Thanks for reply. but if i used sqldataadpter adp= new data... outside
then one error coming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900