Click here to Skip to main content
11,481,916 members (64,486 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 5:49am
Edited 22-Nov-12 11:59am
Nelek70.7K
v3
Comments
Ravi Bhavnani at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.150520.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2015
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