Click here to Skip to main content
15,904,828 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
In a page, I write the code:(for data insert in to Database)
C#
string Openingdate = tbopendate.Text;
myCommand.Parameters.Add("@Openingdate", System.Data.SqlDbType.DateTime).Value = Openingdate;


This code is working fine, there is no error.

But the same code I write in another page & the code is ((for data insert in to Database).
C#
string Joindate = tbJoindate.Text;
myCommand.Parameters.Add("@Joindate", System.Data.SqlDbType.DateTime).Value = Joindate;


This code is showing error::Failed to convert parameter value from a String to a DateTime.

Why & where is the problem???
Posted
v2
Comments
PrashantSonewane 10-Apr-13 7:27am    
can you try
DateTime dt = DateDateTime.Parse(tbJoindate.Text);

myCommand.Parameters.Add("@Joindate", System.Data.SqlDbType.DateTime).Value = dt;
prodipjsr 10-Apr-13 7:36am    
I hv try but the system show the error::String was not recognized as a valid DateTime.
What is the format and value you are getting in tbJoindate.Text ?
Please give some example.
prodipjsr 11-Apr-13 2:07am    
dateFormat: 'mm-dd-yy',

I don't know how you are not getting error in the first. Are both having datetime type in database? If yes then first one also should not work.
You have to convert text to datetime
Try this
[Edit]
C#
DateTime date = DateTime.ParseExact(tbJoindate.Text.ToString(),
                         "yyyy-MM-dd HH:mm:ss.fff",
                         CultureInfo.InvariantCulture);

cmd.Parameters.Add("@Joindate", SqlDbType.DateTime).Value = date;
 
Share this answer
 
v2
Comments
prodipjsr 10-Apr-13 7:38am    
yes first one is working fine..And both data type is same DateTime...
prodipjsr 10-Apr-13 7:54am    
can u give me the solution please..
Anuja Pawar Indore 10-Apr-13 8:00am    
I have updated please try and see
prodipjsr 11-Apr-13 2:09am    
not working
Try parsing the date. Let's check whether the given tbJoining date is a DATE or not. Try this:
C#
DateTime dt;
DateTime.TryParse(tbJoindate.Text.Trim(), out dt);
myCommand.Parameters.Add("@Joindate", System.Data.SqlDbType.DateTime).Value = dt;



--Amit
 
Share this answer
 
C#
DateTime date=Format(CDate(tbJoindate.Text),"yyyy-MM-dd")
mycommand.parameters.Add("@Joindate",date);

or
C#
DateTime date=DateSerial(Mid(tbJoindate.Text, 7, 4), Mid(tbJoindate.Text, 4, 2), Mid(tbJoindate.Text, 1, 2))
mycommand.parameters.Add("@Joindate",date);
 
Share this answer
 
v2
As we don't have the sample dates you are using, I am assuming that the first portion of you code is working because the date you are passing is correct(unintentionally). For example, passing "10/10/2013" is a valid date both in "dd/MM/yyyy" format as well as "MM/dd/yyyy" format. The second part is not executing because passing "15/12/2013" is correct in "dd/MM/yyyy" format but it is incorrect in "MM/dd/yyyy" format. And maybe the culture of your application is "en-US". So there is an error in the dates you are passing. If the textboxes you are using takes dates in "MM/dd/yyyy" format, your code should work fine. But, if your textboxes are taking dates in "dd/MM/yyyy" format, you can convert it in "MM/dd/yyyy" format by using the below code:

C#
string InputDate = YourDateTextBoxId.Text.Trim();
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime OutputDate = Convert.ToDateTime(InputDate, dateInfo); 


OutputDate will be the valid date. Try it and tell us if it works.
 
Share this answer
 
Comments
prodipjsr 11-Apr-13 2:11am    
my dateFormat: 'mm-dd-yy', i put the date format from calendar..
Zafar Sultan 11-Apr-13 3:12am    
You can change the date format of your calendar control to "MM/dd/yyyy" or it has to be "mm-dd-yy"? If you can't change, you can use: DateTime date = DateTime.ParseExact(YourDateTextBoxId.Text.Trim(), "MM-dd-yy", System.Globalization.CultureInfo.InvariantCulture);
helpr_b 16-Nov-15 6:56am    
DateTime date = DateTime.ParseExact(YourDateTextBoxId.Text.Trim(), "MM-dd-yy", System.Globalization.CultureInfo.InvariantCulture);
helpr_b 16-Nov-15 6:57am    
string.Format("{0}", Convert.ToDateTime(pro.ValueParameter.ToString()).Date.ToString("MM/dd/yyyy"));

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