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.ToStr ing("dd-MM M-yyyy ")
reqrddate = Convert.ToDateTime(txtrequ ireddate.T ext)
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.SqlE xception: 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\MemberPag es\Request .aspx.vb Line: 96
Stack Trace:
[SqlException (0x80131904): Conversion failed when converting datetime from character string.]
System.Data.SqlClient.SqlC onnection. OnError(Sq lException exception, Boolean breakConnection) +857242
System.Data.SqlClient.SqlI nternalCon nection.On Error(SqlE xception exception, Boolean breakConnection) +734854
System.Data.SqlClient.TdsP arser.Thro wException AndWarning (TdsParser StateObjec t stateObj) +188
System.Data.SqlClient.TdsP arser.Run( RunBehavio r runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
System.Data.SqlClient.SqlC ommand.Run ExecuteNon QueryTds(S tring methodName, Boolean async) +192
System.Data.SqlClient.SqlC ommand.Int ernalExecu teNonQuery (DbAsyncRe sult result, String methodName, Boolean sendToPipe) +380
System.Data.SqlClient.SqlC ommand.Exe cuteNonQue ry() +135
Request.Button1_Click(Obje ct sender, EventArgs e) in C:\Inetpub\loans\MemberPag es\Request .aspx.vb:9 6
System.Web.UI.WebControls. Button.OnC lick(Event Args e) +105
System.Web.UI.WebControls. Button.Rai sePostBack Event(Stri ng eventArgument) +107
System.Web.UI.WebControls. Button.Sys tem.Web.UI .IPostBack EventHandl er.RaisePo stBackEven t(String eventArgument) +7
System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData) +33
System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt) +5102
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
sql erver 2005
I get the following error when trying to insert into db from web app
have tried....
txtrequireddate.Text.ToStr
reqrddate = Convert.ToDateTime(txtrequ
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.SqlE
Source Error:
Line 94: "values ('" & username & "','" & reqrddate & "','" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"
Line 95: Dim myCommand As New SqlCommand(sql, myConnection)
Line 96: myCommand.ExecuteNonQuery(
Line 97: ElseIf (j = 0) Then
Line 98: lblnoitem.Visible = True
Source File: C:\Inetpub\loans\MemberPag
Stack Trace:
[SqlException (0x80131904): Conversion failed when converting datetime from character string.]
System.Data.SqlClient.SqlC
System.Data.SqlClient.SqlI
System.Data.SqlClient.TdsP
System.Data.SqlClient.TdsP
System.Data.SqlClient.SqlC
System.Data.SqlClient.SqlC
System.Data.SqlClient.SqlC
Request.Button1_Click(Obje
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.WebControls.
System.Web.UI.Page.RaisePo
System.Web.UI.Page.RaisePo
System.Web.UI.Page.Process
--------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
ASKER
Thanks for the reply
the date is valid-- using asp.net compare validator validating datatype date
txtrequireddate.Text = txtrequireddate.Text.ToStr ing("dd-MM M-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.InvalidCastExceptio n: 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.ToStr ing("dd-MM M-yyyy ")
Line 41: username = txtusername.Text
Line 42: reqrddate = txtrequireddate.Text
the date is valid-- using asp.net compare validator validating datatype date
txtrequireddate.Text = txtrequireddate.Text.ToStr
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.InvalidCastExceptio
Source Error:
Line 38:
Line 39: 'assign values to vars
Line 40: txtrequireddate.Text = txtrequireddate.Text.ToStr
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.
(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(txtrequired date.Text) ?
http://msdn2.microsoft.com/en-us/library/1k1skd40.aspx
HTH
~BC
http://msdn2.microsoft.com/en-us/library/1k1skd40.aspx
HTH
~BC
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.SqlE xception: 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.
"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.SqlE
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
"values ('" & username & "',convert(datetime,'" & reqrddate & "',105),'" & nhi & "'," & eventnum & ",'" & accnum & "','" & reqstdate & "' )"
Cheers,
Hillwaaa
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.SqlE xception: Conversion failed when converting datetime from character string.
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.SqlE
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.SqlE xception: Conversion failed when converting datetime from character string.
"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.SqlE
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?
can you set a breakpoint to check what value is being set?
Or do a:
MessageBox.Show(reqrddate.
before the "values (" line...
What is the value?
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?
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.
Line 95: Dim myCommand As New SqlCommand(sql, myConnection)
line - then we can see what the "sql" variable is set to.
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
reqrddate = txtrequireddate.Text
label1=reqrddate.text
resulted in 12/12/2006 which was the date entered
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
sql "insert into requests ( [User_Name], [required_date],[NHI], [Event_Number], [ACC_Number], [Request_Date]) values ('','12/12/2006','aaa2222'
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 & "' )"
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 & "' )"
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/1 2/2006',10 1),'aaa222 2',0,'0',c onvert(dat etime,'12/ 12/2006',1 01))
(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/1
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
Protected Sub TextBox2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtrequestdate.Load
txtrequestdate.Text = DateTime.Now.ToString("dd-
End Sub
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.
ASKER
its assigned to variable reqstdate=txtrequestdate.t ext
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.
as I think that is where the issue lies.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
this works
Dim reqrddate As String = Format(CDate(txtrequiredda te.Text), "dd MMM yyyy").ToString
Dim reqstdate As String = Format(Date.Now, "dd MMM yyyy").ToString
Dim reqrddate As String = Format(CDate(txtrequiredda
Dim reqstdate As String = Format(Date.Now, "dd MMM yyyy").ToString
A couple of quick things:
(1) try txtrequireddate.Text = txtrequireddate.Text.ToStr
(2) check that txtrequireddate.Text is a valid date before trying to convert to a datetime
Cheers,
Hillwaaa