i am using this code for searching against diffrent parameters. but when i enter wrong record error messag is shown "not found" when i enter valid record it populated to datagridview and agin error messag shown not found i whant to remove it. but when i remove it from current place then on wrong entry no error is shown help me plz.
if (comboBox1.SelectedItem.ToString() == "Acc_No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Account_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Name")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Name= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "CNIC")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where CNIC_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Contact No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Phone_No= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Registration No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Registration_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Frame No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Frame_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Engine No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Engine_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
else
{
MessageBox.Show("No record found");
my.Close();
}
Loading

Iftikar HussainRank #423 · 5.9K reputationPosted Aug 26, 2013, 10:34 PM EDT
Regards,
Iftikar
Nitesh KejriwalRank #253 · 9.9K reputationPosted Aug 26, 2013, 7:59 PM EDT
Since you are using a combobox, i understand you are filtering only based on 1 data field. You should use if..elseif..condition in this case.. Try the code below-
if (comboBox1.SelectedItem.ToString() == "Acc_No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Account_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "Name")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Name= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "CNIC")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where CNIC_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "Contact No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Phone_No= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "Registration No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Registration_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "Frame No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Frame_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
elseif (comboBox1.SelectedItem.ToString() == "Engine No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Engine_no= '" + textBox1.Text + "'", my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
else
{
MessageBox.Show("No record found");
my.Close();
}
ta mu198 reputationPosted Aug 26, 2013, 8:18 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 26, 2013, 8:19 PM EDT
If you have more than 3 if condition that better use switch statement for performance improvement. And also you can optimize your code as like below
string sql = "";
switch (comboBox1.SelectedItem.ToString())
{
case "Acc_No":
sql = string.Format("Select * from addcustomer where Account_no= '{0}", textBox1.Text);
break;
case "Name":
sql = string.Format("Select * from addcustomer where Name= '{0}", textBox1.Text);
break;
case "CNIC":
sql = string.Format("Select * from addcustomer where CNIC_no= '{0}", textBox1.Text);
break;
case "Contact No":
sql = string.Format("Select * from addcustomer where Phone_No= '{0}", textBox1.Text);
break;
case "Registration No":
sql = string.Format("Select * from addcustomer where Registration_no= '{0}", textBox1.Text);
break;
case "Frame No":
sql = string.Format("Select * from addcustomer where Frame_no= '{0}", textBox1.Text);
break;
case "Engine No":
sql = string.Format("Select * from addcustomer where Engine_no= '{0}", textBox1.Text);
break;
}
if (sql != "")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, my);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if(dt.Rows.Count==0)
{
MessageBox.Show("No record found");
}
Regards,
Iftikar
ta mu198 reputationPosted Aug 26, 2013, 8:48 PM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 26, 2013, 8:51 PM EDT
Regards,
Iftikar
ta mu198 reputationPosted Aug 26, 2013, 8:56 PM EDT
{
public search_customer()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
SqlConnection my = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\heaven_prince2009\Desktop\final pr latest\final pr\Database1.mdf;Integrated Security=True;User Instance=True");
DataTable dt = new DataTable();
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
string sql = "";
switch (comboBox1.SelectedItem.ToString())
{
case "Acc_No":
sql = string.Format("Select * from addcustomer where Account_no= '{0}", textBox1.Text);
break;
case "Name":
sql = string.Format("Select * from addcustomer where Name= '{0}", textBox1.Text);
break;
case "CNIC":
sql = string.Format("Select * from addcustomer where CNIC_no= '{0}", textBox1.Text);
break;
case "Contact No":
sql = string.Format("Select * from addcustomer where Phone_No= '{0}", textBox1.Text);
break;
case "Registration No":
sql = string.Format("Select * from addcustomer where Registration_no= '{0}", textBox1.Text);
break;
case "Frame No":
sql = string.Format("Select * from addcustomer where Frame_no= '{0}", textBox1.Text);
break;
case "Engine No":
sql = string.Format("Select * from addcustomer where Engine_no= '{0}", textBox1.Text);
break;
}
if (sql != "")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, my);
da.Fill(dt);------------------------------------------error
dataGridView1.DataSource = dt.DefaultView;
my.Close();
}
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
}
}
}
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 26, 2013, 9:23 PM EDT
Regards,
Iftikar
ta mu198 reputationPosted Aug 26, 2013, 10:32 PM EDT
if (comboBox1.SelectedItem.ToString() == "Acc_No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Account_no= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Name")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Name= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "CNIC")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where CNIC_no= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Contact No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Phone_No= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Registration No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Registration_no= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Frame No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Frame_no= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
if (comboBox1.SelectedItem.ToString() == "Engine No")
{
my.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from addcustomer where Engine_no= '" + textBox1.Text + "'", my);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
if (dt.Rows.Count == 0)
{
MessageBox.Show("No record found");
}
my.Close();
}
Pranay PatelRank #2,858 · 501 reputationPosted Dec 4, 2013, 11:20 PM EST
For Better Code performance Use "Switch Case".
It Provide the Code realibility then if..else statement.
for an Example :-
Switch(Combobox1.selected.item.ToString())
{
case "Acc_No":
Your Code
case "Name":
Your Code
}
i hope this is help you.