-1

i am trying to print from crystal report but i am getting an error. here is pic of what i want to do pic . the text box is for the users ID. when i type the id then press the button i want to print only that user information. this is my code

if (conect.State != ConnectionState.Open)
        {
            conect.Open();
        }

        string cs = "select * from Sign_Up where ID='"+textBox1.Text+"'";
        OleDbCommand command = new OleDbCommand(cs, conect);
        OleDbDataAdapter adap = new OleDbDataAdapter(command);
        DataSet ds = new DataSet();

        adap.Fill(ds, "Sign_Up");

        CrystalReport1 crs1 = new CrystalReport1();
        crs1.SetDataSource(ds.Tables["Sign_Up"]);
        crs1.SetDataSource(ds);
        crystalReportViewer1.ReportSource = crs1;
        conect.Close();
        crystalReportViewer1.Refresh();

and this is my error

System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.'

at this code adap.Fill(ds, "Sign_Up");

anyone have any suggestions ?

June7
  • 19,874
  • 8
  • 24
  • 34
  • You are forcing ID - which sounds like a number - to be passed as text. Always use SQL Parameters. Always. – Ňɏssa Pøngjǣrdenlarp Mar 11 '19 at 15:24
  • @MakeStackOverflowGoodAgain actually i see a lot of people talk about parameters but i don' know how to use it that's why i am trying this this my collage project and this is the last part of it – John wiliam Mar 11 '19 at 15:46
  • Please read [Why do we prefer using parameters in SQL statements](https://stackoverflow.com/questions/7505808/)? – Dour High Arch Mar 11 '19 at 16:11
  • @DourHighArch i still didnt get parameters like in this line SqlCommand query = new SqlCommand("SELECT empSalary FROM employee WHERE salary = @sal;"); what @ sal mean ? is it the data base filed name or its just a random name :/ ? – John wiliam Mar 11 '19 at 16:29
  • Don't use apostrophe delimiters for number type field criteria, they are for text fields. – June7 Mar 11 '19 at 20:04
  • @June7 what should i use ? – John wiliam Mar 11 '19 at 20:15

1 Answers1

0

Don't use apostrophe delimiters for number type field criteria, they are for text fields. This is causing 'data type mismatch' error.

string cs = "select * from Sign_Up where ID=" + textBox1.Text;

Use of parameters would be vital if project has any sort of public accessibility (web, remote user). The sal would be a declared parameter variable and @ signifies use of parameter in SQL statement.

June7
  • 19,874
  • 8
  • 24
  • 34