Reputation: 13
I want to know how to see whether the user selected doctor name or doctor special in the combobox, if the user selects doctor name from the combobox then the first if condition should run, but if the user enters doctor special the second should run. But when i enter doctor name it doesnt work.
if (comboxBox1.Text = "Doctor name") ;
{
try
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\dev\\assignt_soft\\healthline_\\healthline_\\healthline_db.accdb");
OleDbCommand com = new OleDbCommand("select * from doctorss where uuname='" + textBox1.Text + "'", con);
DataTable dt = new DataTable();
con.Open();
dt.Load(com.ExecuteReader());
dataGridView1.DataSource = dt;
con.Close();
MessageBox.Show("Displaying doctors with the search criteria doctor name");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (comboxBox1.Text = "Doctor special") ;
{
try
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\dev\\assignt_soft\\healthline_\\healthline_\\healthline_db.accdb");
OleDbCommand com = new OleDbCommand("select * from doctorss where doctor special='" + textBox1.Text + "'", con);
DataTable dt = new DataTable();
con.Open();
dt.Load(com.ExecuteReader());
dataGridView1.DataSource = dt;
con.Close();
MessageBox.Show("Displaying doctors with the search criteria doctor name");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Upvotes: 0
Views: 64
Reputation: 10078
Try to format your code like this.
try
{
if (comboxBox1.Text != "Doctor special")
{
//doctor name code...
}
else
{
//special doctor code...
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
For if
statements, always use ==
equality operator in C#, like if (count == 0)
. Single equal sign is used for assignment like int count = 1
. And no ;
after if(...), it will make the compiler think there is a blank statement, and associate that with the if condition. The block following that will become a normal code block and will execute always!
First get this working. Then, make your queries parameterized to minimize sql injection threat as @Steve mentioned in the comment. Refer this msdn article.
Upvotes: 1