Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to get all values of a particular id from my access database. here is the code:
C#
conn.Open();
           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
Posted
Comments
Kuthuparakkal 23-Aug-12 0:08am    
plz find the answer suggested

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.
C#
OleDbConnection connection = new OleDbConnection("Read your connection string from a config file. May be an encrypted one");

try
{
	connection.Open();

	// 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;
	command.Parameters.Add(parameter);

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

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
 
Share this answer
 
uid might be stored as int and you supply a string values.

also try this :
C#
CommandText = "SELECT * FROM arrear where [uid] =" + uidd ;
 
Share this answer
 
v2
Comments
saifullahiit 23-Aug-12 0:15am    
Syntax error (comma) in query expression '[uid] =System.Windows.Forms.Label
Fix the syntax error.
CommandText = string.Format("SELECT * FROM arrear where [uid] = {0}",uidd);
 
Share this answer
 
Your uid from table having int as datatype.
so convert parameter to int & also remove single quote as folllows
C#
CommandText = "SELECT * FROM arrear where [uid] ="+ Convert.ToInt32(uidd) +"";
 
Share this answer
 

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