Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.:
C#
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 2-Jul-12 23:47pm
v2

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"]);
             }
 
Share this answer
 
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) + "";
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900