Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am having problem converting text to datefield

Status
Not open for further replies.

Ken011

MIS
Oct 13, 2006
66
US
I am trying to find out the correct format for assigning a date value to one of my form elements.

I am getting the following error:

"String was not recognized as a valid DateTime"

with this code;

Code:
objCommand.Parameters("@txttargetDate").Value = txttargetDate.Text

I tried to replace the code with this:
Code:
If Not (txttargetDate.Text Is Nothing) then
 objCommand.Parameters("@txttargetDate").Value = CType( txttargetDate.Text , System.DateTime)
else
 objCommand.Parameters("@txttargetDate").Value = Nothing
end if

Then now, I am getting this error:

Prepared statement '(@txtrequest varchar(64),@txtassignedTo varchar(64),@description' expects parameter @txttargetDate, which was not supplied.

Does anyone have any idea how to fix this?

Thanks in advance


 
do you define the Parameter as SqlDbType.DateTime? this may be what's missing

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
i did that already
Code:
        objCommand.Parameters.Add("@txttargetDate", System.Data.SqlDbType.DateTime)
 
this may be the problem... txttargetDate.Text will never be nothing. it can be an empty string, but not nothing.

try this instead
Code:
If Not (txttargetDate.Text = "") then
 objCommand.Parameters("@txttargetDate").Value = CType( txttargetDate.Text , System.DateTime)
else
 objCommand.Parameters("@txttargetDate").Value = Nothing
end if

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I try your code, I get same error but if I try this:

Code:
If Not (txttargetDate.Text = nothing) then
 objCommand.Parameters("@txttargetDate").Value = CType( txttargetDate.Text , System.DateTime)
else
 objCommand.Parameters("@txttargetDate").Value =now()
end if

it seems to work.

I don't understand it.
 
maybe the value in the text box isn't formatted correctly.
place this code directly above the if/then/else statement.
view the results in the output or imidiate window.
post the output here.
Code:
System.Diagnostics.Debug.WriteLine(txttargetDate.Text)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top