Search code examples
mysqlsql.neterror-handlingobject-reference

SQL inserting multiple lines, object reference not set to an instance of an object error


I'm fairly new to how to use SQL Database, my goal in this area of code is to make a do loop that will continuously read the files made in that month and insert it into their respective columns on the SQL Database. Please do forgive me if there is a simple mistake, this is for my coursework which is based on a theatre/cinema. I cant seem to figure out the error other than the obvious one it displays which says "Object reference is not set to an instance of an object", help would be much appreciated, thanks!

SpecificPath() retrieves the file from the hard drive and reads a specific line from the text file it accesses.

   Do Until C = 32

            If File.Exists(SearchPath(C)) Then
                Dim sr As New StreamReader(SearchPath(C))
                Items(1, C) = ReadSpecificLine(SearchPath(C), 14) 'Total Product Profit
                Items(2, C) = ReadSpecificLine(SearchPath(C), 15) 'Total Ticket Profit
                Items(3, C) = ReadSpecificLine(SearchPath(C), 16) 'Total Profit from Water
                Items(4, C) = ReadSpecificLine(SearchPath(C), 17) 'Total Profit from Large Soft Drink
                Items(5, C) = ReadSpecificLine(SearchPath(C), 18) 'Total Profit from Small Soft Drink
                Items(6, C) = ReadSpecificLine(SearchPath(C), 19) 'Total Profit from Buttered Popcorn
                Items(7, C) = ReadSpecificLine(SearchPath(C), 20) 'Total Profit from Salted Popcorn
                Items(8, C) = ReadSpecificLine(SearchPath(C), 21) 'Total Profit from Sweet Popcorn
                Items(9, C) = ReadSpecificLine(SearchPath(C), 28) 'Total Profit from Adult Tickets
                Items(10, C) = ReadSpecificLine(SearchPath(C), 29) 'Total Profit from Teen Tickets
                Items(11, C) = ReadSpecificLine(SearchPath(C), 30) 'Total Profit from Child Tickets
                Items(12, C) = ReadSpecificLine(SearchPath(C), 34) 'Gross Profit
                Items(13, C) = ReadSpecificLine(SearchPath(C), 35) 'Net Profit

                MySqlConn = New MySqlConnection
                Try
                    MySqlConn.ConnectionString = "Placeholder for pasting onto website"
                    MySqlConn.Open()
                    cmd.Connection = MySqlConn
                    cmd.CommandText = "INSERT INTO Cinema_Graph_Data (Year,Month,Water,Large_SD,Small_SD,Buttered_Popcorn,Salted_Popcorn,Sweet_Popcorn,Adult_Tickets,Teen_Tickets,Child_Tickets,Products_Profit,Ticket_Profit,Gross_Profit,Net_Profit) VALUES(@year,@month,@water,@largeSD,@smallSD,@buttered,@salted,@sweet,@adult,@teen,@child,@product,@ticket,@gross,@net)"
                    cmd.Parameters.Add("@year", MySqlDbType.VarChar).Value = Year
                    cmd.Parameters.Add("@month", MySqlDbType.VarChar).Value = Month
                    cmd.Parameters.Add("@water", MySqlDbType.VarChar).Value = Items(3, C)
                    cmd.Parameters.Add("@largeSD", MySqlDbType.VarChar).Value = Items(4, C)
                    cmd.Parameters.Add("@smallSD", MySqlDbType.VarChar).Value = Items(5, C)
                    cmd.Parameters.Add("@buttered", MySqlDbType.VarChar).Value = Items(6, C)
                    cmd.Parameters.Add("@salted", MySqlDbType.VarChar).Value = Items(7, C)
                    cmd.Parameters.Add("@sweet", MySqlDbType.VarChar).Value = Items(8, C)
                    cmd.Parameters.Add("@adult", MySqlDbType.VarChar).Value = Items(9, C)
                    cmd.Parameters.Add("@teen", MySqlDbType.VarChar).Value = Items(10, C)
                    cmd.Parameters.Add("@child", MySqlDbType.VarChar).Value = Items(11, C)
                    cmd.Parameters.Add("@product", MySqlDbType.VarChar).Value = Items(1, C) 'This is product profit - employee form saves data in file first
                    cmd.Parameters.Add("@ticket", MySqlDbType.VarChar).Value = Items(2, C) 'This is ticket profit - employee form saves data in file second then its water
                    cmd.Parameters.Add("@gross", MySqlDbType.VarChar).Value = Items(12, C)
                    cmd.Parameters.Add("@net", MySqlDbType.VarChar).Value = Items(13, C)
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
                Finally
                    MySqlConn.Close()
                    sr.Close()
                End Try
            ElseIf C = 31 Then
                MsgBox("Imported Data From " & MonthName(Month, True) & " " & Year & vbNewLine & "Successfully uploaded data to SQL Database.")

            End If
    C = C + 1
        Loop
    Else
        MsgBox("Please select a product above to import its data.")
    End If

Solution

  • Thank you guys for all your help, i finally figured out that the "Object reference not set to an instance of an object." Was actually referring to the line @nick was talking about.

    The problem with the code was:

    MySqlConn.ConnectionString = "Placeholder for pasting onto website" MySqlConn.Open() cmd.Connection = MySqlConn

    and i replaced that code with MySqlConn = New MySqlConnection

    MySqlConn.ConnectionString = "Placeholder for pasting onto website" Query = "INSERT INTO Cinema_Graph_Data (Rest of the giant query) cmd = New MySqlCommand(Query, MySqlConn) with the MySQLConn.Open() right at the bottom of all the parameters. The cmd variable was infact not initialised at all hence being the target for the object reference error. Thanks for telling me about debugging and thanks for pointing out the error @Nick .

    The only problem i have now is the database not being able to translate a "£" sign and only showing a "?" but i'll try figuring that out on my own. Thanks again for the help!