0

I have tried alot of changes from previous posts. Does anyone know what could be cause this?

(I know the code shouldn't be used from a security point of view but its a small local based system)

Con.Open();
oleDbCmd.Connection = Con;

OleDbDataReader rdr = null;
OleDbCommand cmd = new OleDbCommand("select * from Customers", Con);
rdr = cmd.ExecuteReader();

while (rdr.Read())
{
                Int32 intChech = Convert.ToInt32(cboCustomerID.Text);
                if (intChech == (Int32)rdr.GetValue(0))
                {
                    txtTitle.Text = (string)rdr.GetValue(1);
                    txtSurname.Text = (string)rdr.GetValue(2);
                    txtForename.Text = (string)rdr.GetValue(3);
                    txtAddress.Text = (string)rdr.GetValue(4);
                    txtTown.Text = (string)rdr.GetValue(5);
                    txtCounty.Text = (string)rdr.GetValue(6);
                    txtPostCode.Text = (string)rdr.GetValue(7);
                    txtTelephone.Text = (string)rdr.GetValue(8);
                }
            }
            //PULL DATABASE INFORMATION FOR CAR INFO
           string strSearch = Convert.ToString(cboCustomerID.Text);
           string strSQL = @"select * from Hire if [Customer ID] = '"; 


           OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
           OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

            DataTable dataTable1 = new DataTable();
            DataSet ds1 = new DataSet();

            dAdapter1.Fill(dataTable1);
            for (int i = 0; i < dataTable1.Rows.Count; i++)

            {
                dgHire1.Rows.Add(dataTable1.Rows[i][0], dataTable1.Rows[i][1], dataTable1.Rows[i][2], 
                dataTable1.Rows[i][3], dataTable1.Rows[i][4]);
            }
}
3
  • I don't think you can do that: select * from Hire if [Customer ID] = ... What's the database and the language you are using? Commented Apr 29, 2015 at 8:39
  • This IF seems to be the problem... Commented Apr 29, 2015 at 8:39
  • Look closely at the value of strSQL. Exactly what should that customer id be? Commented Apr 29, 2015 at 8:50

1 Answer 1

2

You should use this code:

//PULL DATABASE INFORMATION FOR CAR INFO
string strSQL = @"select * from Hire where [Customer ID] = ?";

OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
dAdapter1.SelectCommand.Parameters.Add("[Customer ID]", cboCustomerID.Text);
OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

Or if you don't want to use command parameters you can use this:

string strSQL = @"select * from Hire where [Customer ID] = '" + cboCustomerID.Text + "'";
OleDbDataAdapter dAdapter1 = new OleDbDataAdapter(strSQL, Con);
OleDbCommandBuilder cBuidler1 = new OleDbCommandBuilder(dAdapter1);

But this is bad practice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.