?
Solved

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

Posted on 2012-03-28
28
Medium Priority
?
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Accepted Solution

by:
Cluskitt earned 1200 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 1200 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 51

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 1200 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 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 400 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 400 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 51

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
 
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 51

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 51

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 51

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 51

Expert Comment

by:Gustav Brock
ID: 37781203
Good.

/gustav
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

800 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