Click here to Skip to main content
12,952,398 members (36,470 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 22-Nov-12 4:49am
Updated 22-Nov-12 10:59am
Nelek121.2K
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try this.
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.
}
 
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

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

Maybe you can try something like the following:
string str = Convert.ToString(com.ExecuteScalar());
DateTime date = DateTime.Parse(str);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,289
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,900


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100