I have three tables: Patient, Doctor, Diagnosis. In the Diagnosis form I have two ComboBoxes, and I need to be able to choose the names of doctor and patient through these ComboBoxes.
Code of the methods:
void populatecombo()
{
string sql = "select * from Patient";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr;
try
{
conn.Open();
DataTable dt = new DataTable();
dt.Columns.Add("PatId", typeof(int));
rdr = cmd.ExecuteReader();
dt.Load(rdr);
PatId.ValueMember = "PatId";
PatId.DataSource = dt;
conn.Close();
}
catch
{
}
}
void populatedoc()
{
string mysql = "select * from Doctor";
SqlCommand cmd = new SqlCommand(mysql, conn);
SqlDataReader rdr;
try
{
conn.Open();
DataTable dt = new DataTable();
dt.Columns.Add("DocId", typeof(int));
rdr = cmd.ExecuteReader();
dt.Load(rdr);
DocId.ValueMember = "DocId";
DocId.DataSource = dt;
conn.Close();
}
catch
{
}
}
string patname;
string docname;
void fetchpatientname()
{
string mysql = "select * from Patient where PatId=" + PatId.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(mysql, conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
patname = dr["PatName"].ToString();
PatientTb.Text = patname;
}
}
void fetchdoctorname()
{
string str = "select * from Doctor where DocId=" + DocId.SelectedValue.ToString() + "";
SqlCommand cmd = new SqlCommand(str, conn);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
docname = dr["DocName"].ToString();
DocName.Text = docname;
}
}
So populatedoc
and populatecombo
should get the names of doctor and patient, and fetch should help to choose them from ComboBox, but it doesn't seem to work with the error:
System.Data.SqlClient.SqlException: "Incorrect syntax near '='."
Picture of the form: