Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here Is my Code. And error of 'Command doesnot exist in current context' in Command.Parameters .please answer me please

DateTime param_date;
param_date = Command.Parameters.Add("@asof_date", SqlDbType.DateTime.ToString());
param_date = Convert.ToDateTime(invoice_date);
Posted
Comments
Richard MacCutchan 11-Jun-15 7:15am    
Where is Command declared, and what is it declared as?

1 solution

Your problem is telling you that the variable (object) that you are trying to use, does not exist in your context. A quite possible problems that can raise this are,

1. Identifier case-sensitive

C# is a case-sensitive language, so Command and command are two different objects.

C#
SqlCommand command = new SqlCommand("sql command", connection);

Command.Parameters.Add(new SqlParameter("", "")); // Command does not exist in current context


So, from this you should check if this is the case.

2. Scope[^]

You should always consider checking the scope of the variables, such as if else block. If a variable is generated in one code block, it won't be available under second.

C#
if(condition) {
   SqlCommand Command = new SqlCommand("sql command", connection);
} else {
   SqlCommand Command = new SqlCommand("other command", connection);
}

Command.Parameters.Add(new SqlParameter("", "")); // Command does not exist in current context


The same error gets popped in the other scenario, because the object Command is deleted as soon as the code hits the closing braces.

Check for what is the possible problem in your code. Otherwise, debug your application[^]. :)
 
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