Results 1 to 2 of 2
  1. #1

    Thread Starter
    Registered User
    Join Date
    Feb 2018

    Error Occured: Object reference not set to an instance of an object.

    hi im begginer....i Have two Buttons ... Button1.Text For Import excel file, Button2.Text For Save
    im about to create a code which can import excel file to the datagridview ,( which i have made and resolve )
    next part i want to save the data on the datagridview to MySql database (manually connection since it was outside the vb app)
    This was my code for import excel fileSolve)

    Imports MySql.Data.MySqlClient
    Imports System.IO
    Imports System.Data.OleDb
    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim con As OleDbConnection
            Dim ds As DataSet
            Dim da As OleDbDataAdapter
            Dim op As New OpenFileDialog
            Dim excel As String
            op.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            op.Filter = "All Files (*.*)|*.*|Excel files(*.xlsx)|*.xlsx|CSV Files(*.csv)|*.csv|XLS Files(*.xls)|*.xls"
            If (op.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                Dim FInfo As New FileInfo(op.FileName)
                Dim FileName As String = op.FileName
                excel = FInfo.FullName
                con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + "; Extended Properties=Excel 12.0 ;")
                da = New OleDbDataAdapter("SELECT * from[Sheet1$]", con)
                ds = New DataSet
                da.Fill(ds, "[Sheet1$]")
                DataGridView1.DataSource = ds
                DataGridView1.DataMember = "[Sheet1$]"
                DataGridView1.AllowUserToAddRows = True
            End If
        End Sub
    and this is my code for inserting data to MySql Database Error)

     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Dim cmd As New MySqlCommand
            Dim second, third, forth As String
            Dim first As Integer
            If conn.State = ConnectionState.Closed Then
                For x As Integer = 0 To DataGridView1.RowCount - 1
                    ''first = DataGridView1.Rows(x).Cells(0).Value
                    'second = DataGridView1.Rows(x).Cells(1).Value
                    'third = DataGridView1.Rows(x).Cells(2).Value
                    'forth = DataGridView1.Rows(x).Cells(3).Value
                    cmd = New MySqlCommand("INSERT INTO cash_table(employee_count2,employee_ID,date,amount)VALUES(@employee_count2,@employee_ID,@date,@amount)", conn)
                    cmd.Parameters.AddWithValue("@employee_count2", first.ToString)
                    cmd.Parameters.AddWithValue("@employee_ID", second.ToString)
                    cmd.Parameters.AddWithValue("@date", third.ToString)
                    cmd.Parameters.AddWithValue("@amount", forth.ToString)
                MsgBox("successfully imported")
            End If
        End Sub
    i Got an error on this part "cmd.Parameters.AddWithValue("@employee_ID", second.ToString)"
    please tell me what i have to change
    TY and i really appreciated to those who share there idea more power and GOD Bless

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Bristol, UK

    Re: Error Occured: Object reference not set to an instance of an object.

    You currently have these lines commented out:
                    ''first = DataGridView1.Rows(x).Cells(0).Value
                    'second = DataGridView1.Rows(x).Cells(1).Value
                    'third = DataGridView1.Rows(x).Cells(2).Value
                    'forth = DataGridView1.Rows(x).Cells(3).Value
    ...and as they are the only lines that assign values to those variables, the variables will not have values (hence the error message).

    If you uncomment those lines then it should eliminate that issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.