Note that there are some explanatory texts on larger screens.

plurals
  1. POVisual C# OleDbException when using variable in SQL WHERE clause
    primarykey
    data
    text
    <p>Im currently using C# in visual studio and am using an access database. Im attempting to bring back data from a database when a customer is selected from a list box. This works perfectly when the sql is hard coded in e.g.</p> <pre><code>command.CommandText = "SELECT * FROM Customers WHERE CustomerID = 2 "; </code></pre> <p>However when I attempt to use a String variable to store the selected user ID I receive a "Data type mismatch in criteria expression" on the </p> <pre><code>"OleDbDataReader reader = command.ExecuteReader();". </code></pre> <p>I have used message boxes to confirm that the s2 variable contains the correct ID when chosen so I am unsure of the problem.</p> <p>Does anyone know a solution to this problem?</p> <pre><code> private void lst_disp_SelectedIndexChanged(object sender, EventArgs e) { String s = (String)lst_disp.SelectedItem; // the s string contains the selected customers ID + name, string s2 = s.Split(' ').FirstOrDefault(); // by spliting we can gain only the ID and store in s2 MessageBox.Show("Selected " + s2); showCust(s2); } private void showCust(string s2) { dataGridView1.AllowUserToAddRows = false; dataGridView1.Columns.Add("CustomerID", "Customer ID"); dataGridView1.Columns.Add("CustomerName", "Customer Name"); dataGridView1.Columns.Add("Description", "Description"); dataGridView1.Columns.Add("Email", "Email"); dataGridView1.Columns.Add("Telephone", "Telephone"); dataGridView1.Columns.Add("DeliveryAddress", "Delivery Address"); dataGridView1.Columns.Add("Notes", "Notes"); OleDbConnection connect = new OleDbConnection(); connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Uni\Year 3\Final Year Project\independent foods\coding\showCustomers\Database1.accdb;Persist Security Info=False"; connect.Open(); MessageBox.Show("Connection open"); OleDbCommand command = new OleDbCommand(); command.Connection = connect; MessageBox.Show("SELECT * FROM Customers WHERE CustomerID = '" + s2 + "' "); command.CommandText = "SELECT * FROM Customers WHERE CustomerID = '" + s2 + "' "; try { OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CustomerID"].Value = reader[0].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["CustomerName"].Value = reader[1].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Description"].Value = reader[2].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Email"].Value = reader[3].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Telephone"].Value = reader[4].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["DeliveryAddress"].Value = reader[5].ToString(); dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Notes"].Value = reader[6].ToString(); } } catch(Exception e) { MessageBox.Show("The File cann't be read. Error: " + e.Message); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload