Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
DateTime date = (DateTime)com.ExecuteScalar();//HERE M GETTING ERROR OF CASTING

if(date!=DateTime.Now.Date)
{

}


[edit]code block added[/edit]
Posted
Updated 22-Nov-12 10:59am
v3
Comments
Ravi Bhavnani 22-Nov-12 11:09am    
Inspect the type of object returned from your call to ExecuteScalar(). That will lead you to the solution.

Try this.
C#
using( SqlConnection cn =new SqlConnection(ConfigurationManager.ConnectionStrings["Conection Name  here"].ConnectionString))
{
SqlCommand com=new SqlCommand();
com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
con.Open();
var OrderDate = (String)com.ExecuteScalar();
if(!string.IsNullOrEmpty(OrderDate))
{
  DateTime oDate = Convert.ToDateTime(OrderDate);
  // rest of the code.
}

}
 
Share this answer
 
If there is no created date at all for the given code, the result will be DBNull (and cannot be cast to a DateTime). Check the result type and convert accordingly.

Some other remarks:
- WHERE sponcorid='" + code + "' -> You know "Bobby Tables"? Search google, its the first hit.
- you compare sponcorid to code... but an id isn't a code, and vice versa.

Good luck!
 
Share this answer
 
Hi,

you cannot cast object (the return type of ExecuteScalar()) to DateTime directly.

Maybe you can try something like the following:
C#
string str = Convert.ToString(com.ExecuteScalar());
DateTime date = DateTime.Parse(str);
 
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