Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,
i am getting an error in date which i have taken in my Web page as PO Date.
in stored procedure i have use (@POdate as Datetime).
in Code i have used like this:

C#
CultureInfo en = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = en;
string format = "dd/MM/yyyy";
string Podate = txtPODate.Text.Trim().ToString();
DateTime DT = DateTime.ParseExact(dt1, format, en.DateTimeFormat);
cmd.Parameters.Add("@PODate", SqlDbType.DateTime).Value = DT;


but i get an Error after
C#
cmd.ExecuteNonQuery();
statement,
Error:Conversion failed when converting date and/or time from character string.


i just want the Date 24/07/2012 not the time.
in database the format is like (2012-07-24)(yyyy-MM-dd).

wht to do guyzz!!!
pl reply
Aamir
Posted
Updated 23-Jul-12 20:05pm
v2
Comments
hitech_s 24-Jul-12 2:10am    
while inserting you can insert directly and while retrieving, use convert method of sql in query
aamir07 24-Jul-12 2:14am    
hitech_s,
its giving me error while Inserting.
Santhosh Kumar Jayaraman 24-Jul-12 2:41am    
You have declared string PODdate and u dint use it anywhere.
How you are setting DT?what is dt1?

Try:
C#
CultureInfo en = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = en;
string format = "dd/MM/yyyy";
string Podate = txtPODate.Text.Trim().ToString();
DateTime DT = DateTime.ParseExact(dt1, format, en.DateTimeFormat);


// Add the parameter
cmd.Parameters.Add("@PODate", SqlDbType.DateTime);
// Set the parameter value based on parameter index
cmd.Parameters[0].Value = DT;


Or add the parameter like this:

C#
cmd.Parameters.Add(new SqlParameter
                        {
                            Value = DT,
                            SqlDbType = SqlDbType.DateTime,
                            ParameterName = "@PODate"
                        });
 
Share this answer
 
v3
Hi,
One suggestion I want go give you is, do the conversion in database sp only.
Send it like:
C#
cmd.Parameters.Add("@PODate", SqlDbType.DateTime).Value = Convert.ToDateTime(txtPODate.Text);


All the best.
-=-Amit
 
Share this answer
 
v3
Comments
aamir07 24-Jul-12 2:29am    
still i shows the errro:Error
The best overloaded method match for 'string.ToString(System.IFormatProvider)' has some invalid arguments
_Amy 24-Jul-12 2:34am    
See my updated answer. Convert varchar date to datetime in your sp. The problem will be solved.
aamir07 24-Jul-12 2:37am    
i have used datetime field((@POdate as Datetime) in my Stored Procedure not as varchar.
_Amy 24-Jul-12 2:40am    
Then not a problem. Pass it directly... And change the format there.
aamir07 24-Jul-12 3:06am    
String datetime = txtPODate.Text.Trim();
DateTime date = DateTime.ParseExact(datetime, "dd/MM/yyyy", CultureInfo.InvariantCulture);
cmd.Parameters.Add("@PODate", SqlDbType.DateTime).Value = date;

Error:Conversion failed when converting date and/or time from character string.
I hope this link will help you
String was not recognized as a valid DateTime[^]
 
Share this answer
 
Comments
aamir07 24-Jul-12 3:48am    
Error:Conversion failed when converting date and/or time from character string
gyuzz, do reply on my queryy.
veryy frustrated by this error
string dateString = txtPODate.Text.Trim();
DateTime date1 = new DateTime();
date1 = DateTime.Parse(dateString, new CultureInfo("en-GB").DateTimeFormat);

SqlParameter prm = new SqlParameter("@PODate", DbType.DateTime);
prm.Value = date1;
cmd.Parameters.Add(prm);

error:Conversion failed when converting date and/or time from character string
 
Share this answer
 
after so much amount of R&D i got my solution for my query has been resolved.
the issue was in SQL Query not in the Code.

guyz,Thanks for ur reply

Regards,
Aamir
 
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