Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

testing for null fails in vb.net

I have made some code, that fails with error:
{"Conversion from type 'DBNull' to type 'String' is not valid."}
when StrCurrent has some value its fine but when its null it fails and I dont know how to correct this.

Whatever I select gets linked in a table if it currently has a link (StrCurrent not null)  they get deleted then new links created otherwise if strcurrent is currently null then just create the links without deletion event.

The sub is here

 Private Sub PanelLinkShort_Click(sender As Object, e As EventArgs) Handles PanelLinkShort.Click
        Dim StrCommon As String
        Dim StrSearch As String
        Dim StrProcName As String


        Dim i As Integer
        i = 0
        StrCommon = ""
        StrSearch = ""
        StrProcName = "usp_DeleteShortDescLinkWords"

        Dim selectedRowCount As Integer = DataGridViewStringsBulk.Rows.GetRowCount(DataGridViewElementStates.Selected)
        If selectedRowCount > 0 Then
            'check if datagridview StrCommen is populated if so call usp to delete the rows from link table
            If Not String.IsNullOrWhiteSpace(DataGridViewStringsBulk.SelectedRows(i).Cells("StrCommon").Value) Then 'FAILS HERE
                'strcommon populated
                StrSearch = DataGridViewStringsBulk.SelectedRows(i).Cells("StrCommon").Value
                Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
                    Using cmdInsert As New SqlCommand(StrProcName, cnSql)
                        cmdInsert.CommandType = CommandType.StoredProcedure
                        cnSql.Open()
                        cmdInsert.CommandTimeout = 0
                        cmdInsert.Parameters.AddWithValue("@Word", StrSearch)
                        cmdInsert.ExecuteScalar()


                    End Using
                End Using
            End If

            'strcommon not populated so go ahead and create the links
            Using cnSql As New SqlClient.SqlConnection("Data Source=MAIN-PC\SQLEXPRESS;Initial Catalog=Dictionary;Integrated Security=True;MultipleActiveResultSets=True")
                Using cmdInsert As New SqlClient.SqlCommand
                    cmdInsert.Connection = cnSql
                    cnSql.Open()
                    cmdInsert.CommandTimeout = 0
                    For i = 0 To selectedRowCount - 1
                        'get rows
                        If i = 0 Then
                            StrCommon = DataGridViewStringsBulk.SelectedRows(i).Cells("Strshort").Value
                        End If
                        cmdInsert.CommandText = "INSERT INTO TblShortDescLink (StrShort, StrCommon) VALUES ('" & DataGridViewStringsBulk.SelectedRows(i).Cells("Strshort").Value & "','" & StrCommon & "'" & ")"
                        cmdInsert.ExecuteNonQuery()
                    Next i
                End Using
            End Using
        End If
        MessageBox.Show("Linking finished")
    End Sub

Open in new window


EXex2
0
PeterBaileyUk
Asked:
PeterBaileyUk
1 Solution
 
David Johnson, CD, MVPOwnerCommented:
StrCurrent is not in the code block presented, do you mean strCommon?
0
 
Russ SuterCommented:
Instead of checking whether the object is null you need to check if the value is DBNull. Use the Convert.IsDBNull() function for this.

https://msdn.microsoft.com/en-us/library/system.convert.isdbnull%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
0
 
Tapan PattanaikSenior EngineerCommented:
Hi PeterBaileyUk,

In your stored procedure add the below line of code.

IsNull (StrCommon,' ')  StrCommon

Regards,
Tapan Pattanaik
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now