.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

String was not recognized as a valid DateTime.

Posted By:      Posted Date: October 21, 2010    Points: 0   Category :ASP.Net


"String was not recognized as a valid DateTime."

what can i do?

View Complete Post

More Related Resource Links

String was not recognized as a valid DateTime.

Hai friends,            I have used following code                  newClient.Dob = DateTime.Parse (textBoxDOB.Text.Trim());I convert to string also..But  It Shows  error in Run Time Please verify it,                 Error :  String was not recognized as a valid DateTime.

Conversion from string to type 'Date' is not valid



Im receiving a strange error Error Message: Conversion from string "15/08/2010 22:21:35" to type 'Date' is not valid.

I know this is generally down to cultural date formats etc, however the reason this is strange is it just randomly occured twice in the last week, the website has been running since october and nothing has changed,

The error constantly occured until the IIS was reset and recompilled the site, its been running fine since, however im just trying to figure out what could cause such an error to occur.

I've narrowed it down to a line of code which is:

bktime = DReader.Item("Bktime").ToString

bktime = Year(CDate(bktime)) & "-" & fmt(Month(CDate(bktime)), 2) & "-" & fmt(Day(CDate(bktime)), 2)

Has anyone else encountered this randomly occuring and is there anything i can put in place to prevent this in future?



Data Conversion - string to datetime help!!!

Hi all, I need help on concatenate two fields into one then display it as datetime format as such yyyy-mm-dd hh:mm:ss AM/PM. The db is on SQL 2008 SP1. The date and time columns are on the same table and store as varchar. Date has varchar(10) and time has varchar(16). Date column shows result from query is 20100827 while the time column is either 070101.000000 or 070101. The question is how go about concatenate the two together and display in the format above in a report? How should I write my select query for this report? I am using SQL 2008 R2 BI Development Studio to design my report. Thank you. VCHS

Convertion failed when converting datetime to String

DECLARE @usr datetime SET @usr = user+'::Date' IF NOT EXISTS (SELECT 'X' FROM table WHERE Name = 'File' AND CONVERT(varchar,DateAdd, 101) = CONVERT(varchar, getdate(), 101)) BEGIN INSERT INTO table (Code, Name, Date) VALUES ('DD', 'File',@usr) END the date will be created by another process and its value will be loaded in the variable DATE. I'm calling that variable here. I;m getting the error Convertion failed when converting datetime to String. how to resolve this..??

Convertion failed when converting datetime to String

DECLARE @usr datetime SET @usr = user+'::Date' IF NOT EXISTS (SELECT 'X' FROM table WHERE Name = 'File' AND CONVERT(varchar,DateAdd, 101) = CONVERT(varchar, getdate(), 101)) BEGIN INSERT INTO table (Code, Name, Date) VALUES ('DD', 'File',@usr) END I;m getting the error Convertion failed when converting datetime to String. how to resolve this..??

Conversion from type 'TextBox' to type 'String' is not valid

I have a FormView that allows a user to register for a company event. I want to do a couple of things with this form. 1. I need to write this data to a MS SQL database 2. I need to send an email confirmation of their registration to their email. I know how to do both of these things, but i am having trouble doing both at the same time. It would be nice if I could do it in one click, but I coded it into two pages to try to help simplify it. The first page allows the user to input their information into a formview and when they click the submit button, it places those answers into session variables:Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Session("FirstName") = Me.FormView1.FindControl("FirstName") Session("LastName") = Me.FormView1.FindControl("LastName")  <li> <label> First Name</label> <asp:TextBox ID="FirstName" runat="server" class="text-box" Text='<%# Bind("FirstName") %>' /> </li> </ul> </div>

Converting string to DateTime

Hello, I am trying to figure out the best way to convert a string object into a DateTime with only using the year.I have something like:Date Time HSYear = DateTime.ParseExact(HighSchoolyear, "yyyy", System.Globalization.CultureInfo.CurrentCulture); But it is still giving a "String was not a recognized as a valid DateTime."In the full context, I have a webpage with a formview on it and within the formview is a TextBox control that is taking a user input such as "1999"  then an ObjectDataSource picks it up and runs it through a BLL. And within the BLL is where I am trying to convert it before it gets inserted into the database.

converting string to datetime and then to shortdate?/

Hi,I have a field (varchar) field that holds date.  Some data was transferred to this column but the transferred data was a timestamp (datetime in the format of say 10/26/2009 4:54:13 AM).  Now, I need to modify the new table to only contain 10/26/2009.  How can I write a statement to delete the time leavin gonly 10/26/2009??  I can use trim function but the # of characters vary as the date might be 1/10/2010 12:50:00 PM, etc....?thanks .

how to insert null or empty string to datetime var ?

hi i get data from xml file and sometime the date is empty. i have this code:     try { TimeTo = Convert.ToDateTime(R[15].ToString()); }         catch { TimeTo = null ; } but i got error because i cant insert null to datetime var what i can do ? thak's in advance

Convert SSIS DateTime to a String


Being a newbie to SSIS I'm not sure of the most efficient method of converting a DateTime object to a String.


I'm from a C# background where this would be easy using DateTime.ToString("YYYYMMdd"). I want to use the date in a file name so don't require most of the parts.


I'm sure I could do this using a script task to produce a file name for each row of data in my table and add that filename to the dataset but it seem like overkill to do something that should be simple. Also as I'm supposed to be getting to grips with SSIS I shouldn't keep running back to what I know.


My current approach is to derive a column and build up an expression to convert the date into a string. The only problem being that it doesn't work.

The expression I'm working with is:

(DT_WSTR, 50)([OrgName] ) + "_" + (DT_WSTR, 50)( [PayrollName] ) + (DT_WSTR, 4)(YEAR( [ProcessedDate] )) + (DT_WSTR, 2)(MONTH( [ProcessedDate] )) + (DT_WSTR, 2)(DAY( [ProcessedDate] )) ".txt"


Can anyone see where I'm going wrong?


All comments greatly received.

datetime formating with string.format


I have a table in my SQL DB that is a DateTime table, what I want to do is take the numeric datetime it stores and convert it to a long date (i.e. 12/12/2012 conversts to December 12, 2012). Seaching the forums it looks like the string.format is the way to go and searching for info on that I found http://www.csharp-examples.net/string-format-datetime/ which lists all the different formats but they do not list a formating string to convert to the format I want to use. Does anyone know what format string I would use to convert a numeric date to a long date like I want? And can you show it in a code example because I am not 100% sure I understand the code examples shown on that site.

How do I convert a Sql Datetime value into a string including the miliseconds?


 Hi all,

Currently I am able to bring the datetime from my SQL table but I also need the milisecods. Here is the line of code:

DateTime latestTimeStamp = DateTime.UtcNow;
string select = "select max(timeStamp) from Table1";

SqlConnection myConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["UctConnectionString"].ConnectionString);
SqlCommand mySqlCommand = new SqlCommand(select, myConnection);

latestTimeStamp = (DateTime)mySqlCommand.ExecuteScalar();

So latestTimeStamp = {9/22/2010 6:24:09 PM} but I needed it to be latestTimeStamp = {9/22/2010 6:24:09:080 PM}
since the SQL field value is 2010-09-22 18:24:09.080 and I want to do an accurate datetime comparison.

Thank you

dateTime string format


when my system date separator set to "-", the following return the date format with "dd-mm-yyyy HH:mm"

DateTime b = DateTime.ParseExact(date, "dd/MM/yyyy HH:mm", provider);
                            strDate = b.ToString("dd/MM/yyyy HH:mm");

return strDate;


why? how can i return the dayeformat with "/" separator ingore PC datatime setting

ERROR-Conversion from string "'," to type 'Double' is not valid



I am having  badly stuck!!I have spent hours in trying to solve that.

I am having the following loving function:


   Public Function InsertDeleteUpdate(ByVal command As String) As Boolean
            cmd.Connection = objectconnection
            cmd.CommandText = command
            Return True
        Catch ex As Exception
            Return False
End Try

 In the above function I pass the following string:

gridcost = Convert.ToDecimal(txtcost.Text)
_date = Convert.ToDateTime(txtdatefrom.Text)
patient = Convert.ToInt32(ddlpatients.SelectedValue)

 sqlstring = "execute dbo.InsertDeleteUpdate " & patient & ",'" & _date + "'," & gridcost & ",'" & txtinsurances.Text & "','" & checkreceipt.Checked + "'," & appointment_xid

The string includes a stored procedure.The store procedure includes the following parameters:

Conversion from string "09/28/10 9.00AM" to type 'Date' is not valid.


I am getting the below error. Please see my code below and let me know what is wrong.


dim sDatetime as string = startdate & " " &starttime & "AM"

dim dt as Date = CDate(sDatetime)

I am getting the Conversion from string "09/28/10 9.00AM" to type 'Date' is not valid error in this line. 

string not converting in datetime format


hi to all,

 i'm developing a website. i have a registration form where user can registered with this website.

this have a birthdate field for store user's birthdate.that is a textbox.

here is my code.

protected void submitbtn_Click(object sender, ImageClickEventArgs e)
System.Globalization.CultureInfo en = new System.Globalization.CultureInfo("en-GB");
li.fname = firstname.Text;
li.lname = lastname.Text;
li.gender = gender.SelectedItem.Text;
li.date = Convert.ToDateTime(birthdate.Text,en); //getting error here
li.address = addrs.Text;
li.contactno = contno.Text;
li.countryid =Convert.ToInt32( dropcontry.SelectedValue);
li.stateid = Convert.ToInt32( dropstate.SelectedValue);
li.cityid =Convert.ToInt32( dropcity.SelectedValue);
li.emailid = prmrmailid.Text;
li.username = createuserid.Text;
li.password = createpassword.Text;
li.secutityque = dropsecque.SelectedItem.Text;
li.securityans = secanswer.Text;

int a = li.personid;
li.personid= a;
int p = li.personid;

How do I handle Conversion from type 'DBNull' to type 'String' is not valid


help i am getting error :

Conversion from type 'DBNull' to type 'String' is not valid

Dim OledbString As String = ("SELECT  Product_Hierarchy,Model,Origin,Item_Code,Description,Price_USD FROM [sheet1$]")
                    'Dim eAdapter As New SqlDataAdapter(sqlString, connection)
                    Dim eAdapter As New OleDbDataAdapter(OledbString, connection)
                    Dim eTable As New DataTable()

Dim i As Integer = 0
                    While (i < eTable.Rows.Count)
                        If ((eTable.Rows(i)(0)).ToString().Tri

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend