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 HussainPosted Aug 27, 2013, 2:34 AM
Regards,
Iftikar
Pranay PatelPosted Dec 5, 2013, 4:20 AM
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.
ta muPosted Aug 27, 2013, 2:32 AM
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();
}
Iftikar HussainPosted Aug 27, 2013, 1:23 AM
Regards,
Iftikar
ta muPosted Aug 27, 2013, 12:56 AM
{
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 HussainPosted Aug 27, 2013, 12:51 AM
Regards,
Iftikar
ta muPosted Aug 27, 2013, 12:48 AM
Iftikar HussainPosted Aug 27, 2013, 12:19 AM
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 muPosted Aug 27, 2013, 12:18 AM
Nitesh KejriwalPosted Aug 26, 2013, 11:59 PM
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();
}