Link to home
Start Free TrialLog in
Avatar of deNZity
deNZity

asked on

Conversion failed when converting datetime from character string.

ms visual studio 2005
sql erver 2005

I get the following error when trying to insert into db from web app

have tried....
txtrequireddate.Text.ToString("dd-MMM-yyyy ")
reqrddate = Convert.ToDateTime(txtrequireddate.Text)
reqrddate= CDate(txtrequireddate.Text)


Server Error in '/' Application.
--------------------------------------------------------------------------------

Conversion failed when converting datetime from character string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string.

Source Error:


Line 94:                 "values ('" & username & "','" & reqrddate & "','" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"
Line 95:                 Dim myCommand As New SqlCommand(sql, myConnection)
Line 96:                 myCommand.ExecuteNonQuery()<--- highlighted
Line 97:             ElseIf (j = 0) Then
Line 98:                 lblnoitem.Visible = True
 

Source File: C:\Inetpub\loans\MemberPages\Request.aspx.vb    Line: 96

Stack Trace:


[SqlException (0x80131904): Conversion failed when converting datetime from character string.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857242
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734854
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +380
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
   Request.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\loans\MemberPages\Request.aspx.vb:96
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Hi deNZity,

A couple of quick things:

(1) try txtrequireddate.Text = txtrequireddate.Text.ToString("dd-MMM-yyyy ")
(2) check that txtrequireddate.Text is a valid date before trying to convert to a datetime

Cheers,
Hillwaaa
Avatar of deNZity
deNZity

ASKER

Thanks for the reply
the date is valid-- using asp.net compare validator validating datatype date
txtrequireddate.Text = txtrequireddate.Text.ToString("dd-MMM-yyyy ") results in following error

Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

Source Error:


Line 38:
Line 39:         'assign values to vars
Line 40:         txtrequireddate.Text = txtrequireddate.Text.ToString("dd-MMM-yyyy ")
Line 41:         username = txtusername.Text
Line 42:         reqrddate = txtrequireddate.Text
 
Ah - sorry. looking at the error trace, the problem is a sql error - you are trying to insert a non ISO standard date format which is what is giving you the error.  There are two ways to fix

(1) change the format of the string that reqrddate prints in your insert statement to be ISO standard yyyyMMdd
(2) use the following insert:
"values ('" & username & "',convert(datetime,'" & reqrddate & "',XXX),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"

where XXX depends on the format of reqrddate in that string.  See http://msdn2.microsoft.com/en-us/library/ms174450.aspx for appropriate values.
did u try datetime.parse(txtrequireddate.Text)?

http://msdn2.microsoft.com/en-us/library/1k1skd40.aspx

HTH
~BC
Avatar of deNZity

ASKER

tried...

"values ('" & username & "',convert(datetime,'" & reqrddate & "',dd-mm-yyyy),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"


The name "dd" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: The name "dd" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
it has to be a number.  if " & reqrddate & " will add a date of the form 22-11-2006 to the string, use:

"values ('" & username & "',convert(datetime,'" & reqrddate & "',105),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"

Cheers,
Hillwaaa
Avatar of deNZity

ASKER

bchoor,
 tried what you suggested got this error

Conversion failed when converting datetime from character string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string.



Avatar of deNZity

ASKER

Hillwaaa,
"values ('" & username & "',convert(datetime,'" & reqrddate & "',105),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"
resulted in this error

Conversion failed when converting datetime from character string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting datetime from character string.


ok - maybe reqrddate isn't being set properly?

can you set a breakpoint to check what value is being set?

Or do a:
        MessageBox.Show(reqrddate.ToString)
before the "values (" line...

What is the value?
Avatar of deNZity

ASKER

this is not a vb.net project this is a asp.net website using vb.net code behind as such messageboxes can't be used.

where would I set  a breakpoint?
set it on the

Line 95:                 Dim myCommand As New SqlCommand(sql, myConnection)

line - then we can see what the "sql" variable is set to.
Avatar of deNZity

ASKER

I commented out the queries and ran the page to see what was in
reqrddate = txtrequireddate.Text
label1=reqrddate.text

resulted in 12/12/2006 which was the date entered
Avatar of deNZity

ASKER

Heres the sql
            sql      "insert into requests ( [User_Name], [required_date],[NHI], [Event_Number], [ACC_Number], [Request_Date]) values ('','12/12/2006','aaa2222',0,'0','12:00:00 a.m.' )"      String

ok - 12/12/2006 could be dd/MM/yyyy or MM/dd/yyyy

if dd/MM/yyyy try

"values ('" & username & "',convert(datetime,'" & reqrddate & "',103),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"

if MM/dd/yyyy try

"values ('" & username & "',convert(datetime,'" & reqrddate & "',101),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"
Avatar of deNZity

ASKER

Both of those resulted in  the original error.. maybe I should post this in the sql serverforum?
OK - from your sql line you have two problems:

(1) the reqrddate is not formatted properly - see my above
(2) the reqstdate is also not formatted properly - 12:00:00 a.m. is not a valid date - you need to do the same conversion on this value to get the MM/dd/yyyy value you have for reqrddate

What you want to end up with is (assuming MM/dd/yyyy):

insert into requests ( [User_Name], [required_date],[NHI], [Event_Number], [ACC_Number], [Request_Date]) values ('',convert(datetime,'12/12/2006',101),'aaa2222',0,'0',convert(datetime,'12/12/2006',101))
Avatar of deNZity

ASKER

the request date is set like so...

 Protected Sub TextBox2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtrequestdate.Load
        txtrequestdate.Text = DateTime.Now.ToString("dd-MMM-yyyy")
    End Sub
Avatar of deNZity

ASKER

the user puts the date in as dd/mm/yyyy for reqrddate


That sets the text for "txtrequestdate" - how does "txtrequestdate.Text" get into "reqstdate"?  This is the variable you use in your Line 94.

Avatar of deNZity

ASKER

its assigned to variable reqstdate=txtrequestdate.text
Avatar of deNZity

ASKER

Hillwaaa, Thanks for your effort the points are yours. I have asked the question in the sql server forum
as I think that is where the issue lies.
ASKER CERTIFIED SOLUTION
Avatar of Hillwaaa
Hillwaaa
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deNZity

ASKER

this works

Dim reqrddate As String = Format(CDate(txtrequireddate.Text), "dd MMM yyyy").ToString
Dim reqstdate As String = Format(Date.Now, "dd MMM yyyy").ToString