Conversion from string "" to type 'Date' is not valid

I am getting the error

Conversion from string "" to type 'Date' is not valid

On the following line: only when st.DueDate is blank ..
st.WfTemplateCreate(strWorkflowType, st.StID, i, Me.UserProperties.Fullname, SystemHandler.GetGMT, st.DueDate)

How to resolve this error?
LVL 8
PeteEngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CluskittCommented:
That happens because your function requires a date. If the argument provided is empty, it will generate that error trying to convert it. You have to place a check before and either replace it with a default value, or just don't run the function.
0
CluskittCommented:
Use Datetime.Tryparse to check for a valid date.
0
PeteEngineerAuthor Commented:
I have to run this function , so what i will do in case of null value in the variable?

Can you write a code and show it here?
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

CluskittCommented:
You can try something like this (rusty at C#):

Datetime Mydt
if (!(DateTime.TryParse(st.DueDate, out Mydt)))
{
Mydt = "01-01-1900";
}
else
{
Mydt = st.DueDate;
}
st.WfTemplateCreate(strWorkflowType, st.StID, i, Me.UserProperties.Fullname, SystemHandler.GetGMT, Mydt)

replace the 1900 value with whatever default date you want
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CluskittCommented:
If you want to run the function, but with a null or empty value, then you have to change the function itself. Replace the date in the last argument with a string. Then you have to check the code for date usage, which would depend on what the function does. If it doesn't assume that to be a date (for example, just assigns its value to a textbox), then you would be fine.
0
PeteEngineerAuthor Commented:
can you write in Vb ?
0
CluskittCommented:
That is actually easier for me, as that's what I've been using the last year. I just used C# because that's what you placed as the question zone.

Dim Mydt As Date
If Not IsDate(st.DueDate) Then
  Mydt = "01-01-1900"
Else
  Mydt = st.DueDate
End If
st.WfTemplateCreate(strWorkflowType, st.StID, i, Me.UserProperties.Fullname, SystemHandler.GetGMT, Mydt)
0
Gustav BrockCIOCommented:
Never use string for dates.
So you could do declare the DateTime with a default value:
Dim Mydt As DateTime = New DateTime(1900, 1, 1)
If IsDate(st.DueDate) Then
  Mydt = st.DueDate
End If

Open in new window

/gustav
0
CluskittCommented:
I usually use DateSerial myself. I just used a string, in this case, so the OP would understand better what I was trying to do. Also, I used an If Then Else structure to clarify the intended results. It seems that the OP isn't really VB fluent, so I tried to make it as structured and intuitive as possible.
0
Gustav BrockCIOCommented:
OK, I see your point, but given the many traps and errors handling dates as strings can produce, it should only be done in those cases where it is mandatory for some (usualy outside) reason.

/gustav
0
Jacques Bourgeois (James Burger)PresidentCommented:
The TryParse used in the C# code also works in VB and would be preferred. IsDate is specific to VB while TryParse is used in any .NET language.

Most of the time, that using .NET classes and methods is better than using the VB internal functions, that were left there to preserve some form of backward compatibility and making it easier for amateurs to make the transition between VB6 and VB.NET.
0
PeteEngineerAuthor Commented:
Is it possible to do a DBNULL ?
what will be the syntax for this?
0
PeteEngineerAuthor Commented:
I have checked the table it will allow null values,, then why this error?
0
Gustav BrockCIOCommented:
What error?

You can declare the parameter as DateTime? to accept Null values as well, or you could just filter the values for Null and skip those records from further handling.

/gustav
0
PeteEngineerAuthor Commented:
I cannot change the datatype since it is in a dll.

How can i make to take nulls in the code? db accepts nulls
0
Gustav BrockCIOCommented:
IsDate shouldn't accept Null, thus this should still work:
Dim Mydt As DateTime = New DateTime(1900, 1, 1)
If IsDate(st.DueDate) Then
  Mydt = st.DueDate
End If

Open in new window

/gustav
0
CluskittCommented:
This error happens because the function is defined, in VB code, something like:
st.WfTemplateCreate(strWorkflowType As String, st.StID As Integer, i As Integer, Me.UserProperties.Fullname As String, SystemHandler.GetGMT As Date, st.DueDate As Date)

The names might be different, and maybe some data types as well, but the last argument is defined as a date. Therefore, when you call the function and use an empty variable, it tries to convert to a date and generates an exception. To avoid this, you'd have to change the type to String. Then, in the function, when you insert/update to the database, you can use DBNull.Value to insert a null, or, if you're simply using string concatenation (not advisable due to SQL injection vulnerability), insert either '' or NULL. But you'd have to do the check inside the function, not before you call it.
If you require assistance adjusting that function, you'd have to post the code here, so we can adapt it for you.
0
PeteEngineerAuthor Commented:
Dim Mydt As DateTime = New DateTime(1900, 1, 1)


I in the above code there is a defualt date , i want it to be as NULL not 1900/1/1

How to achieve this ?
0
CluskittCommented:
As I said, you have to change that from inside the function. Change the type to String, then check for empty strings and send as null. But it has to be inside the function itself, not before it's being called.
0
PeteEngineerAuthor Commented:
Are you saying to change the datatype from date to date time inside the function?
0
Gustav BrockCIOCommented:
Try this (works in C#, not tested in VB.NET):
Dim Mydt As DateTime? = Null
If IsDate(st.DueDate) Then
  Mydt = st.DueDate
End If

Open in new window

/gustav
0
CluskittCommented:
No. I'm saying to change the datatype from date to string inside the function. What's generating the error is the function call itself. If the argument passed isn't a date, whether it's a string, an empty string or a null value, it will create an exception because it can't implicitly convert to a date, which is what the function requires. Therefore, you have to change it to a string and, inside the function, check if it's a date to turn the value into a null.
0
PeteEngineerAuthor Commented:
Dim Mydt As DateTime? = System.DBNull

and

 Dim Mydt As DateTime? = Null


Both giving errors

Cluskitt..OK let me see
0
Gustav BrockCIOCommented:
Not knowing your function, neither what you really are doing, it would be something like this as a Variant accepts any type of value:

Function st.WfTemplateCreate(WorkflowType As String, StID As Long, i As Long, Fullname As String, GMT As Variant, DueDate As Variant)

Dim Mydt As DateTime
If IsDate(DueDate) Then
  Mydt = DueDate
End If

' more ...

End Function

/gustav
0
PeteEngineerAuthor Commented:
Mydt = DueDate

why ur assinging duedate if there is a value in duedate? like below

f IsDate(DueDate) Then

Shouldnt be

'If st.DueDate = "" Then


Anyway still now working ..thou!
0
CluskittCommented:
Inside the function, after you change to a string, you must have something like:
sql = "INSERT INTO MyTable VALUES ('" & WorkflowType & "','" & StID & ".... etc... '" & st.DueDate & "')"
What you have to do is:
If Not IsDate(st.DueDate) Then
  st.DueDate = NULL
Else
  st.DueDate = "'" & st.DueDate & "'"
End If
then change the sql concatenation to remove the ' before and after st.DueDate.

If you're using a parameter instead, just do:
If IsDate(st.DueDate) Then
  sqlComm.Parameter("DueDate").Value = CType(st.DueDate, Date)
Else
  sqlComm.Parameter("DueDate").Value = DBNull.Value
End If

I'm just guessing at names here. If you want specific assistance, please post your function code.
0
PeteEngineerAuthor Commented:
I have omiteed the parameter when there is a empty string ..issue solved !
0
Gustav BrockCIOCommented:
Good.

/gustav
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.