0

Guys when i run the code below i get this error:-Object reference not set to an instance of an object. everything is initialized and there are no empty fields in the database please help !

Dim con As New OleDbConnection

        Dim cmd2 As New OleDbCommand
        con.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\mydb.accdb")

        cmd2.CommandText = "update items set itmbrcd=@p5,itemname=@p1,itemprice=@p2,itemGroup=@group,remarks=@p3,vat=@p6 where itmcode=@p4"
        cmd2.Connection = con
        con.Open()

        cmd2.Parameters.AddWithValue("@p5", ItmBrcdTextBox.Text)
        cmd2.Parameters.AddWithValue("@p1", ItemNameTextBox.Text)
        cmd2.Parameters.AddWithValue("@p2", ItemPriceTextBox.Text)
        cmd2.Parameters.AddWithValue("@group", ItemGroupComboBox.SelectedItem.ToString)
        cmd2.Parameters.AddWithValue("@p3", RemarksTextBox.Text)
        cmd2.Parameters.AddWithValue("@p4", ItmcodeTextBox.Text)
        cmd2.Parameters.AddWithValue("@p6", VatNumericUpDown.Value)

        cmd2.ExecuteNonQuery()
        MsgBox("item updated!")
        con.Close()
charles
  • 55
  • 1
  • 10
  • P.S. it stopped working when i added the "vat" field to the database, everything was normal before and the insert and delete are working – charles Dec 19 '15 at 20:57
  • The line it happens on should tell you a lot. My guess is that nothing is selected in the ComboBox. You add the last few params in the wrong order too. – Ňɏssa Pøngjǣrdenlarp Dec 19 '15 at 21:02
  • 4
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – marc_s Dec 19 '15 at 21:11
  • @Plutonix this didnt work tho but thanks anyway. – charles Dec 19 '15 at 21:26
  • @plutonix debugging the lines returned no error, think problems updating the dataset – charles Dec 19 '15 at 21:27

1 Answers1

0

As Plutonix mentioned, you get this error on the following line when nothing is selected in the combo box. Check to see if combo1.SelectedItem is nothing.

cmd2.Parameters.AddWithValue("@group", ItemGroupComboBox.SelectedItem.ToString)
xpda
  • 15,585
  • 8
  • 51
  • 82