Reputation: 21
I am having some trouble with my code and didn't get any result from the resources found,so below is my example when I select from the combobox a value the event SelectionChangeCommitted
is fired up and should fill my listview control with the date,from the database related to my SELECT,but I am getting an error and I don't know how to handle it,I'd appreciate it if someone can help me.
In the constructor i have this code:
this.medic.SelectionChangeCommitted += new EventHandler(medic_SelectionChangeCommitted);
private void medic_SelectionChangeCommitted(object sender, EventArgs e)
{
SqlDataAdapter alegsectie = new SqlDataAdapter("SELECT C.denumire,OC.Data,OC.ora_inc,OC.ora_sf FROM cabinete AS C INNER JOIN orar_clinica AS OC ON c.id_cabinet = OC.id_cabinet INNER JOIN medici AS M ON OC.id_medic = M.id_medic and M.nume =" + medic.SelectedValue, conn);
DataSet listView1 = new DataSet();
alegsectie.Fill(listView1);
ERROR:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Conversion failed when converting the varchar value 'pop' to data type int.
'pop' is the selectedvalue in combobox.
Upvotes: 0
Views: 445
Reputation: 2345
If M.nume is of type varchar you will need the value to be wrapped in single quotes in order for the sql statement to be valid:
SqlDataAdapter alegsectie = new SqlDataAdapter("SELECT C.denumire,OC.Data,OC.ora_inc,OC.ora_sf FROM cabinete AS C INNER JOIN orar_clinica AS OC ON c.id_cabinet = OC.id_cabinet INNER JOIN medici AS M ON OC.id_medic = M.id_medic and M.nume ='" + medic.SelectedValue + "'", conn);
Note however that this approach to running queries is vulnerable to Sql injection. Maybe think about using parameters to run your queries.
Hope that helps
Upvotes: 2