Click here to Skip to main content
14,367,095 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am creating an app in c# using window form. i want to read data from access database using datareader. here is the code.:
conn.Open();
string CommandText = "SELECT * FROM entry where userid ='"+ userid.Text + "'";
           OleDbCommand cmd = new OleDbCommand(CommandText);
           cmd.Connection = conn;
           OleDbDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                panel1.Visible = true;
                label13.Text = dr["userid"].ToString();
                label13.Text = dr["Name"].ToString();
                label13.Text = dr["NIC"].ToString();
                label13.Text = dr["Father_Name"].ToString();
             }

i got the following error:
Data type mismatch in criteria expression
please help me out this error.

thanks in advance
Posted
Updated 3-Jul-12 0:47am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

If user id is an integer then try this
conn.Open();
string CommandText = "SELECT * FROM entry where userid ="+ userid.Text ;
           OleDbCommand cmd = new OleDbCommand(CommandText);
           cmd.Connection = conn;
           OleDbDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                panel1.Visible = true;
                label13.Text = Convert.ToString(dr["userid"]);
                label13.Text = Convert.ToString(dr["Name"]);
                label13.Text = Convert.ToString(dr["NIC"]);
                label13.Text = Convert.ToString(dr["Father_Name"]);
             }
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

string CommandText = "SELECT * FROM entry where userid ='"+ userid.Text + "'";

check userid column datatype is int.
If it is change above sentence to
string CommandText = "SELECT * FROM entry where userid ="+ Convert.TOInt32(userid.Text) + "";
   
Comments
Matt T Heffron 12-Apr-13 13:19pm
   
You're converting the userid.Text to Int32 and then immediately converting it back to string when concatenating the strings??

If the .Text is not the string representation of an integer, then your code will throw a FormatException.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100