Click here to Skip to main content
14,330,121 members
Rate this:
Please Sign up or sign in to vote.
See more:
i am trying to get all values of a particular id from my access database. here is the code:
           CommandText = "SELECT * FROM arrear where [uid] ='"+ uidd+"'";
           cmd = new OleDbCommand(CommandText);
           cmd.Connection = conn;

           dr = cmd.ExecuteReader();

when i run the prgram it gives the following error
Data type mismatch in criteria expression.
could somebody please tell me what is wrong with my code
thanks in advance
Kuthuparakkal 23-Aug-12 0:08am
plz find the answer suggested
Rate this:
Please Sign up or sign in to vote.

Solution 3

Seriously? :S Not a single soul provided an answer with a SqlParameter. It's sad to see a professional community still encourages people to generate SQL as string which is vulnerable to so many hacks.
OleDbConnection connection = new OleDbConnection("Read your connection string from a config file. May be an encrypted one");


	// This automatically sets the command.Connection property
	OleDbCommand command = connection.CreateCommand();
	command.CommandType = CommandType.Text;
	command.CommandText = "SELECT * FROM arrear where [uid] = @uid";

	// The point of parameters is that they escape unsafe values, so that
	// your code doesn't break or is not vulnerable for basic hacks
	OleDbParameter parameter = command.CreateParameter();
	parameter.ParameterName = "@uid";
	parameter.DbType = DbType.Int32;
	parameter.Value = someLabel.Text;

	// Check out the 'CommandBehavior' enum, which can be passed as a parameter to this method
	// Very useful in some occasions
	OleDbDataReader reader = command.ExecuteReader();
	if (connection.State != ConnectionState.Closed)

This will take away many data formatting issues, unless the value you pass into the parameter fails to be converted to the specified data type specified in the parameter DbType

Hope this helps, regards
Rate this:
Please Sign up or sign in to vote.

Solution 1

uid might be stored as int and you supply a string values.

also try this :
CommandText = "SELECT * FROM arrear where [uid] =" + uidd ;
saifullahiit 23-Aug-12 0:15am
Syntax error (comma) in query expression '[uid] =System.Windows.Forms.Label
Rate this:
Please Sign up or sign in to vote.

Solution 2

Fix the syntax error.
CommandText = string.Format("SELECT * FROM arrear where [uid] = {0}",uidd);
Rate this:
Please Sign up or sign in to vote.

Solution 4

Your uid from table having int as datatype.
so convert parameter to int & also remove single quote as folllows
CommandText = "SELECT * FROM arrear where [uid] ="+ Convert.ToInt32(uidd) +"";

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