CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    192

    Conversion error

    Hi,

    I have the following code;

    Code:
      Dim Notes As String
    
            Dim Prac_Req As New System.Data.SqlClient.SqlCommand(("Select prac_no, prac_enabled, notes From dbo.TblPracExclude where Prac_no = " & _
                               DgvPracExcl.Rows(e.RowIndex).Cells(0).Value), conn)
            Try
                Using Autoreader As System.Data.SqlClient.SqlDataReader = Prac_Req.ExecuteReader()
                    While Autoreader.Read()
    
                        prac_no = Autoreader.GetValue(0)
                        prac_enabled = Autoreader.GetValue(1)
                        Notes = FixNull(Autoreader.GetValue(2))
    
    
                    End While
                End Using
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "VeriSIS2")
            End Try
    
    
            If prac_no = DgvPracExcl.Rows(e.RowIndex).Cells(0).Value And prac_enabled = DgvPracExcl.Rows(e.RowIndex).Cells(4).Value And  Notes = DgvPracExcl.Rows(e.RowIndex).Cells(5).Value Then
                'MsgBox("No Practice need Update", MsgBoxStyle.Information, "VeriSIS")
            Else
    .............................

    1) Notes filed is underlined in green with a warning - Variable 'Notes' is used before it has been assigned a value. A null reference exception could result at runtime. Note: Notes field has NULL values.
    2) At runtime, I receive the error - InvalidCastException was unhandled. Operator '=' is not defined for String "" and type 'DBNull'
    Notes = DgvPracExcl.Rows(e.RowIndex).Cells(5).Value

    Any help please

  2. #2
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Conversion error

    Does Notes have a value coming out of the Try? Maybe FixNull doesn't work.

  3. #3
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Conversion error

    The Null may also be the
    Code:
    DgvPracExcl.Rows(e.RowIndex).Cells(5).Value
    I would put a break on the If and see which one is Null

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Conversion error

    Show the code for FixNull() !
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2009
    Posts
    192

    Re: Conversion error

    Code:
      Public Shared Function FixNull(ByVal dbvalue) As String
    
            If dbvalue Is DBNull.Value Then
                Return ""
            Else
                Return dbvalue.ToString
            End If
    
        End Function

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Conversion error

    I'm not sure about your type mismatch but I see you are making the same mistake that you have been making in the past when it comes to reading data. You are looping through a recordset and assigning values to the same variables with each pass then you compare the variable after the loop is complete so in every case you will only be looking at the last record. If there is only one record then there is no problem but then there is no reason to use a loop if only one record is expected and if more than one record is expected then you are going about it all wrong.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Feb 2009
    Posts
    192

    Re: Conversion error

    any advice?

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Conversion error

    Sorry, look back at your older threads, between here and VBForums you have been shown how to properly check your records.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured