Click here to Skip to main content
15,897,093 members

searching in database with parameters and textbox search

jangojan asked:

Open original thread
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

C#
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)
        {
            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();
            }
        }

        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)
       {

       }
    }
}
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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