Solved

testing for null fails in vb.net

Posted on 2016-09-22
3
39 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 78

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now