I need to export data from excel to datagrid and have done it..whole excel data is displayed in datagrid..coding but i got an error when Conversion from type 'DBNull' to type 'String' is not valid coz' i have some null data in my excel file

here's my code everytime i try to select a row they said "Conversion from type 'DBNull' to type 'String' is not valid."

Dim ds As New DataSet
        Dim i As Integer

        Try
            Dim stFileName As String
            Dim filename As String
            With OpenFileDialog2
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    filename = .FileName
                    TextBox5.Text = filename

                End If
            End With
            Dim AAA As System.Data.OleDb.OleDbConnection
            Dim DtSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            AAA = New System.Data.OleDb.OleDbConnection _
            ("provider=Microsoft.Jet.OLEDB.4.0; Data Source= '" & TextBox5.Text & "'; Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter _
                ("select * from [Sheet1$]", AAA)
            MyCommand.TableMappings.Add("Table", "Excel")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0).DefaultView
            AAA.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try



      Dim i, j As Integer
        i = DataGridView1.CurrentRow.Index.ToString
        txtEmersonPO.Text = DataGridView1.Item(0, i).Value
        txtRONO.Text = DataGridView1.Item(1, i).Value
        txtPartNumber.Text = DataGridView1.Item(2, i).Value
        txtSoftwarePublisher.Text = DataGridView1.Item(3, i).Value
        txtSoftwareTitle.Text = DataGridView1.Item(4, i).Value
        txtVersion.Text = DataGridView1.Item(5, i).Value
        cboVersionType.Text = DataGridView1.Item(6, i).Value
        cboLicenseType.Text = DataGridView1.Item(7, i).Value
        cboSoftwareAssurance.Text = DataGridView1.Item(8, i).Value
        cboSubscription.Text = DataGridView1.Item(9, i).Value
        txtQuantityPurchased.Text = DataGridView1.Item(10, i).Value
        txtContractorAgreementNumber.Text = DataGridView1.Item(11, i).Value
        dtpStartDate.Text = DataGridView1.Item(12, i).Value
        dtpEndDate.Text = DataGridView1.Item(13, i).Value
        txtVendor.Text = DataGridView1.Item(14, i).Value
        txtSearch1.Text = DataGridView1.Item(15, i).Value

Edited 5 Years Ago by Nick Evan: Added CODE-tags

This article has been dead for over six months. Start a new discussion instead.