-
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
-
Re: Conversion error
Does Notes have a value coming out of the Try? Maybe FixNull doesn't work.
-
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
-
Re: Conversion error
Show the code for FixNull() !
-
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
-
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.
-
Re: Conversion error
-
Re: Conversion error
Sorry, look back at your older threads, between here and VBForums you have been shown how to properly check your records.