0
votes

I am trying (much too long) to convert a DateTime column to a label.Text.

Various errors:

What am I missing?

drExpDatesRow = dtAllCompanies.Select("CompanyID = " + CompanyID.ToString())

    Dim ls_ExpiresDateString As String
    If (Not IsDBNull(drExpDatesRow("Expdate"))) Then
        Date.TryParse(drExpDatesRow("Expdate").ToString(), ls_ExpiresDateString)
        'ls_ExpiresDateString = ldt_ExpiresDate.ToString("MM/dd/yyyy")
        lbl_ExpireDate.Text = ls_ExpiresDateString.ToString()
    Else
        lbl_ExpireDate.Text = ""
    End If

System.InvalidCastException was unhandled Message=Conversion from string "Expdate" to type 'Integer' is not valid.
Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) at zzz.winCompanyInfo.CompanyInfo_Load(Object sender, EventArgs e) in C:\inetpub\zzz.vb:line 638 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: System.FormatException Message=Input string was not in a correct format. Source=Microsoft.VisualBasic StackTrace: at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) InnerException:

UPDATE** Added DataRow logic

2
Hmm are you sure the code that you posted is where the error occurs? Your error states that you are trying to convert "Expdate" to an integerDNKROZ
If this is a snippet of your average coding then please turn strict on ASAP. TryParse is defined TryParse(ByVal Object, ByRef Date) so ls_ExpiresDateString should be a Date not a String. Btw, a variable declared as a String will never be anything other than a String. So there is no need to call ToString for a String.Bjørn-Roger Kringsjå
I will look into how to turn strict on for now, ty for that tip.JoJo
Your drExpDatesRow = dtAllCompanies.Select("CompanyID = " + CompanyID.ToString()) returns array of rows. So you need to do IsDBNull(drExpDatesRow(0)("Expdate")). But also check the length of array first - may be there nothing foundT.S.

2 Answers

2
votes

When using TryParse use the same data type.

Dim drExpDatesRow = (From dr As DataRow In dtAllCompanies.Rows 
                     Where dr("CompanyId").ToString =
                     CompanyID.ToString).FirstOrDefault

Dim ls_ExpiresDate As Date      ' Date not String
If (Not drExpDatesRow Is Nothing) Then
  If Date.TryParse(drExpDatesRow("Expdate").ToString(), ls_ExpiresDate)
    lbl_ExpireDate.Text = ls_ExpiresDate.ToString()
  End If
Else
    lbl_ExpireDate.Text = ""
End If
1
votes

You had multiple issues in your code. This one should take care of things for good

drExpDatesRow = dtAllCompanies.Select("CompanyID = " + CompanyID.ToString()) ' returns array

lbl_ExpireDate.Text = ""
If drExpDatesRow.Length > 0 Then

    If (Not IsDBNull(drExpDatesRow(0)("Expdate"))) Then

        Dim myDate As DateTime
        Dim res As Boolean = Date.TryParse(drExpDatesRow("Expdate"), myDate)

        If res Then lbl_ExpireDate.Text = MyDate.ToString("<format>")
    End If
End If