Solved

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

Posted on 2012-03-28
28
598 Views
Last Modified: 2012-04-04
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?
0
Comment
Question by:PeteEngineer
  • 10
  • 10
  • 7
  • +1
28 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37776757
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37776770
Use Datetime.Tryparse to check for a valid date.
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37776785
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
 
LVL 18

Accepted Solution

by:
Cluskitt earned 300 total points
ID: 37776803
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37776821
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37777140
can you write in Vb ?
0
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 300 total points
ID: 37777194
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37777490
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
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 300 total points
ID: 37777545
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
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 100 total points
ID: 37777650
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
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 37777669
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37780670
Is it possible to do a DBNULL ?
what will be the syntax for this?
0
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37780700
I have checked the table it will allow null values,, then why this error?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37780720
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Author Comment

by:PeteEngineer
ID: 37780733
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
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37780765
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37780769
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37780773
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37780798
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37780810
Are you saying to change the datatype from date to date time inside the function?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37780876
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37780994
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37781013
Dim Mydt As DateTime? = System.DBNull

and

 Dim Mydt As DateTime? = Null


Both giving errors

Cluskitt..OK let me see
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37781036
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37781053
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
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37781066
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
 
LVL 8

Author Comment

by:PeteEngineer
ID: 37781187
I have omiteed the parameter when there is a empty string ..issue solved !
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 37781203
Good.

/gustav
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now