using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace testingdatabase
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form1().Show();
            this.Close();
        }

        private void text_searchstring_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_search_Click(object sender, EventArgs e)
        {
            errorProvider1.Dispose();
            //if (.Text.Length == 0)
            if (text_searchstring.Text.Length == 0)
                errorProvider1.SetError(text_searchstring, "please fill name");

            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\jumbo\my documents\visual studio 2010\Projects\testingdatabase\testingdatabase\Database1.mdf;Integrated Security=True;User Instance=True";
                conn.Open();
                //SqlCommand cmd = new SqlCommand();
                //cmd.Connection = conn;
                //cmd = new SqlCommand("select * from Table where name like '%" + txtsearch.Text + "%'");
                //cmd = new SqlCommand("select * from Table1 where name like '%" + text_searchstring.Text + "%'");
                SqlDataAdapter adapter = new SqlDataAdapter("select * from Table1 where Name like '%" + text_searchstring.Text + "%'" '%" + comboBox1.SelectedItem.ToString + "%' , conn);
                DataTable dt = new DataTable();
                //SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Visible = true;
                //dataGridView1.DataSource = dt;
                //dataGridView1.Visible = true;
                conn.Close();
                conn.Dispose();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection myConnection_Company = new SqlConnection();
            myConnection_Company.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\jumbo\my documents\visual studio 2010\Projects\testingdatabase\testingdatabase\Database1.mdf;Integrated Security=True;User Instance=True";
            myConnection_Company.Open();
            SqlDataAdapter company = new SqlDataAdapter("select * from Table1 where Name like '" + comboBox1.SelectedItem.ToString() + "'", myConnection_Company);
            //DataTable dd = new DataTable();
            DataSet dd = new DataSet();
            company.Fill(dd);

                    text_searchstring.Text = "";

                myConnection_Company.Close();
                myConnection_Company.Dispose();
            } 

       private void groupBox1_Enter(object sender, EventArgs e)
       {

       }
    }
}

hi guys,
i have a search form, which function is to search from database and show results in gridview, in this there are two boxes one is textbox and second is combobox, now in this one has to enter something in textbox and then select appropriate parameters from combobox as combobox is having some parammeters like name RollNo etc, and then the press search button for results. now issue m facing is, i am not able to use parameters of combobox and textbox together, means the search button should be able to check both boxes and then give results accordingly

Try doing something like string parameter = textbox1.Text + comboBox.SelectedText.ToString(); at the top of the the button click method and then use that parameter variable as the parameter in the SQL statement.

Let me know how you get on.

Edited 3 Years Ago by ChrisHunter

hie chris thnx for reply i add the line mentinoed by you, but givees error, can you just put the code also and new line where to paste ..

thnx

Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement this is the error

I don't understand what you're asking. can you post the mouse click method as it is when you get that error for me to have a look at please?

Edited 3 Years Ago by ChrisHunter

 private void button1_search_Click(object sender, EventArgs e)
        {
            text_searchstring + comboBox1.SelectedItem.ToString();
            errorProvider1.Dispose();
            //if (.Text.Length == 0)
            if (text_searchstring.Text.Length == 0)
                errorProvider1.SetError(text_searchstring, "please fill name");

            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\jumbo\my documents\visual studio 2010\Projects\testingdatabase\testingdatabase\Database1.mdf;Integrated Security=True;User Instance=True";
                conn.Open();
                //SqlCommand cmd = new SqlCommand();
                //cmd.Connection = conn;
                //cmd = new SqlCommand("select * from Table where name like '%" + txtsearch.Text + "%'");
                //cmd = new SqlCommand("select * from Table1 where name like '%" + text_searchstring.Text + "%'");
                SqlDataAdapter adapter = new SqlDataAdapter("select * from Table1 where Name like '%" + text_searchstring.Text + "%'" , conn);
                DataTable dt = new DataTable();
                //SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.Visible = true;
                //dataGridView1.DataSource = dt;
                //dataGridView1.Visible = true;
                conn.Close();
                conn.Dispose();
            }
        }

i put the line which was told by you chris, but after that it gives the error, i think m doing something wrong, can you please have a look of this button code , and correct where i am going wrong ? thnx

You didn't, the bit you added at the top should look like this:

string parameter = textbox1.Text + comboBox.SelectedText.ToString();

NOT like this:

text_searchstring + comboBox1.SelectedItem.ToString();

and you SQL statement should look like this:

"select * from Table1 where Name like '%" + parameter + "%'"

NOT like this

"select * from Table1 where Name like '%" + text_searchstring.Text + "%'"

Have another go and let me know what happens.

Edited 3 Years Ago by ChrisHunter

hie chris
thnx for explaining me , i tries, no error but this time even if i select any parameter from combox it gives me no error and give results, means it doesnt check the parameters , and gives results only with the textbox input only, can you please suggest something again

Ohhhh do you want the user to select a the name of a field within Table1, from the comboBox, and then use what they have entered into the textBox as the search criteria?

If so try something like: "SELECT * FROM Table1 WHERE " + comboBox1.SelectedItem.ToString() + " LIKE '%" + textBox1.Text + "%'";

So if "Name" is selected from the comboBox and "James" is entered in the textBox then the statement above would select all records from Table that contain "James" their Name field.

Is this what you're looking for?

Edited 3 Years Ago by ChrisHunter

thank you soooooooooooooooooooooooooooo much chrish. :) :)

working like a charm now, but now can i add one more step into this, i mean a filter in search, suppose now if m searching james and selece name parameter from combobox, then in that case it should show me only the name column from the table and that having that record .. please

You're very welcome.

I don't quite understand you new request. At the moment you select everything from table1 if the name contains the value entered by the user (lets use "James" as an example again), you only want to return the Name field and NOT the entire record?

If that is the case, when you do "SELECT *" it means SELECT ALL columns (the entire row). If you want to select individual column you have to replace the * with the name of the column you want to select (which in your case is "Name".

Here are a few links that you might find useful:

http://www.w3schools.com/sql/sql_select.asp

http://beginner-sql-tutorial.com/sql.htm

If you get stuck try researching what you're trying to do and it might save you a lot of time as it's more than likely that someone has had the same problem in the past.

However if you can't find a solution feel free to ask but make sure you show what you've done so far as DaniWeb rules mean we can't do your work for you.

This article has been dead for over six months. Start a new discussion instead.