Click here to Skip to main content
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
Nelek69.5K
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)

  Print Answers RSS
0 Maciej Los 495
1 Sergey Alexandrovich Kryukov 469
2 OriginalGriff 360
3 Sascha Lefévre 255
4 Abhinav S 250
0 Sergey Alexandrovich Kryukov 10,102
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076


Advertise | Privacy | Mobile
Web04 | 2.8.150327.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