none
how to solve this problem 'System.NullReferenceException was unhandled' in vb.net

    General discussion

  • Hi friends.

    I am newer in vb 2010.

    I want insert multiple rows from datagridview.

    I have a problem. By move to next row(second row) in datagridview it get an error.This is my code:

    Imports System.Data

    Imports System.Data.SqlClient

    Imports System.Windows.Forms.Form

    Public Class frm_unit

        Dim conn As SqlConnection

        Dim ds As New DataSet

        Dim da As SqlDataAdapter

        Dim cmd As SqlCommand

        Dim tb As DataTable

        Dim tb1 As New DataTable

        Dim builder As SqlCommandBuilder

        Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave

            conn = New SqlConnection("Data Source='Accounting-pc';Initial Catalog='Rasol';Integrated Security='true';")

            conn.Open()

            cmd = New SqlCommand("INSERT  INTO unit (unit_name) VALUES (@unit)", conn)

            Try

                cmd.Parameters.Add("@unit", SqlDbType.VarChar, 50)

                For Each row As DataGridViewRow In DataGridView1.Rows

                    If row.IsNewRow Then

                        cmd.Parameters("@unit").Value = row.Cells(0).Value.ToString

                    End If

                Next

                cmd.ExecuteNonQuery()

            Catch ex As SqlException

                MsgBox(ex.ToString)

            Finally

                conn.Close()

            End Try

        End Sub

       Private Sub shippers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            load_data()

        End Sub

        Sub load_data()

            conn = New SqlConnection("Data Source='Accounting-pc';Initial Catalog='Rasol';Integrated Security='true';")

            cmd = New SqlCommand()

            da = New SqlDataAdapter()

            tb = New DataTable()

            With cmd

                .Connection = conn

                .CommandType = CommandType.Text

                .CommandText = "select unit_name as from unit"

            End With

            da.SelectCommand = cmd

            da.Fill(tb)

            DataGridView1.DataSource = tb

        End Sub

    Pls help me.

    If required, modify my code.

    Best regards.


    • Edited by rasol Wednesday, October 16, 2013 5:35 PM correct
    Wednesday, October 16, 2013 5:32 PM

All replies

  • I rewrote the TRY section of the code.  See below

            Try
                cmd.Parameters.Add("@unit", SqlDbType.VarChar, 50)
                For Each row As DataGridViewRow In DataGridView1.Rows
                    If not row.Cells(0).value is nothing Then
                        cmd.Parameters("@unit").Value = row.Cells(0).Value.ToString
                    End If
                    cmd.ExecuteNonQuery()
                Next
            Catch ex As SqlException
                MsgBox(ex.ToString)
            Finally
                conn.Close()
            End Try


    jdweng

    Wednesday, October 16, 2013 5:50 PM