Solved

testing for null fails in vb.net

Posted on 2016-09-22
3
54 Views
Last Modified: 2016-09-22
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
Comment
Question by:PeterBaileyUk
3 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 41811286
StrCurrent is not in the code block presented, do you mean strCommon?
0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 41811287
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
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 41811301
Hi PeterBaileyUk,

In your stored procedure add the below line of code.

IsNull (StrCommon,' ')  StrCommon

Regards,
Tapan Pattanaik
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 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