Hello..I am developing a windows application in vb.net and getting an error of system.nullreference exception.
I am trying to insert data in the database, insertion occurs in the first go. But, when i stop the porgram and re-run, i get the exception. Can anybody please help me with this ? Here is the code :

Imports System.Data
Imports System.Data.OleDb
Imports System.EventArgs
Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb.OleDbConnection

Public Class ContactInfo_supp
Inherits System.Windows.Forms.Form

Dim Message As String
Dim cn As OleDbConnection
Dim cmd As OleDbCommand

Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click

Try
'MsgBox("Open")
cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
cn.Open()
cmd = New OleDbCommand("insert into contactinfo(usertype,compname,compadd,compcity,comppin,compphcode,compph,compfaxcode,compfax,compmail,compsite,compregoff,compfact,products,interest,region,place,contper1,comprole1,compemail1,contnum1,ext1,mobnum1,contper2,comprole2,compemail2,contnum2,ext2,mobnum2,contper3,comprole3,compemail3,contnum3,ext3,mobnum3,weeklyoff,vat,cst,pan,ecc,commissionerate,range,division,servicetax,bankname,bankbranch,accnum,rtgs,instrname,modelnum,installdate,amc,callibration,period,expdate) values ('Supplier','" & textBox1.Text & "','" & textBox2.Text & "','" & textBox39.Text & "','" & textBox41.Text & "','" & textBox45.Text & "','" & textBox3.Text & "','" & textBox4.Text & "','" & textBox46.Text & "','" & textBox5.Text & "','" & textBox6.Text & "','" & textBox7.Text & "','" & textBox8.Text & "','" & textBox42.Text & "','" & textBox43.Text & "','" & ComboBox1.SelectedItem.ToString() & "','" & textBox44.Text & "','" & textBox9.Text & "','" & textBox10.Text & "','" & textBox47.Text & "','" & textBox11.Text & "','" & textBox12.Text & "','" & textBox13.Text & "','" & textBox18.Text & "','" & textBox17.Text & "','" & textBox48.Text & "','" & textBox16.Text & "','" & textBox15.Text & "','" & textBox14.Text & "','" & textBox23.Text & "','" & textBox22.Text & "','" & textBox49.Text & "','" & textBox21.Text & "','" & textBox20.Text & "','" & textBox19.Text & "','" & textBox50.Text & "','" & textBox25.Text & "','" & textBox26.Text & "','" & textBox27.Text & "','" & textBox28.Text & "','" & textBox29.Text & "','" & textBox30.Text & "','" & textBox31.Text & "','" & textBox32.Text & "','" & textBox33.Text & "','" & textBox34.Text & "','" & textBox35.Text & "','" & textBox36.Text & "','" & textBox37.Text & "','" & textBox38.Text & "','" & dateTimePicker1.Text & "','" & ComboBox2.SelectedItem.ToString() & "','" & ComboBox3.SelectedItem.ToString() & "','" & textBox40.Text & "','" & dateTimePicker2.Text & "')", cn)
cmd.ExecuteNonQuery()

MsgBox("Your Account Created Successfully ")

Dim f9 As New Action()
f9.Show()
Me.Hide()

Catch myException As Exception

MsgBox("No Record Inserted" + myException.ToString())

Finally
'MsgBox("Closing Connection")

cn.Close()
End Try

End Sub

Recommended Answers

All 5 Replies

At what line of code ur getting the error?

Imports System.Data
Imports System.Data.OleDb
Imports System.EventArgs
Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb.OleDbConnection

Public Class ContactInfo_supp
Inherits System.Windows.Forms.Form

Dim Message As String
Dim cn As OleDbConnection
Dim cmd As OleDbCommand



Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click

Try
'MsgBox("Open")
cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
cn.Open()
cmd = New OleDbCommand("insert into contactinfo(usertype,compname,compadd,compcity,comppin,compphcode,compph,compfaxcode,compfax,compmail,compsite,compregoff,compfact,products,interest,region,place,contper1,comprole1,compemail1,contnum1,ext1,mobnum1,contper2,comprole2,compemail2,contnum2,ext2,mobnum2,contper3,comprole3,compemail3,contnum3,ext3,mobnum3,weeklyoff,vat,cst,pan,ecc,commissionerate,range,division,servicetax,bankname,bankbranch,accnum,rtgs,instrname,modelnum,installdate,amc,callibration,period,expdate) values ('Supplier','" & textBox1.Text & "','" & textBox2.Text & "','" & textBox39.Text & "','" & textBox41.Text & "','" & textBox45.Text & "','" & textBox3.Text & "','" & textBox4.Text & "','" & textBox46.Text & "','" & textBox5.Text & "','" & textBox6.Text & "','" & textBox7.Text & "','" & textBox8.Text & "','" & textBox42.Text & "','" & textBox43.Text & "','" & ComboBox1.SelectedItem.ToString() & "','" & textBox44.Text & "','" & textBox9.Text & "','" & textBox10.Text & "','" & textBox47.Text & "','" & textBox11.Text & "','" & textBox12.Text & "','" & textBox13.Text & "','" & textBox18.Text & "','" & textBox17.Text & "','" & textBox48.Text & "','" & textBox16.Text & "','" & textBox15.Text & "','" & textBox14.Text & "','" & textBox23.Text & "','" & textBox22.Text & "','" & textBox49.Text & "','" & textBox21.Text & "','" & textBox20.Text & "','" & textBox19.Text & "','" & textBox50.Text & "','" & textBox25.Text & "','" & textBox26.Text & "','" & textBox27.Text & "','" & textBox28.Text & "','" & textBox29.Text & "','" & textBox30.Text & "','" & textBox31.Text & "','" & textBox32.Text & "','" & textBox33.Text & "','" & textBox34.Text & "','" & textBox35.Text & "','" & textBox36.Text & "','" & textBox37.Text & "','" & textBox38.Text & "','" & dateTimePicker1.Text & "','" & ComboBox2.SelectedItem.ToString() & "','" & ComboBox3.SelectedItem.ToString() & "','" & textBox40.Text & "','" & dateTimePicker2.Text & "')", cn)
cmd.ExecuteNonQuery()

MsgBox("Your Account Created Successfully ")

Dim f9 As New Action()
f9.Show()
Me.Hide()

Catch myException As Exception

MsgBox("No Record Inserted" + myException.ToString())

Finally
'MsgBox("Closing Connection")

cn.Close()
End Try

End Sub

Please use code tags.. it will be easy to read.

Line 22

Most propably is that one (or more) of the comboboxes has no SelectedItem.

By default, there is no SelectedItem on a combobox.

To verify if the combo has one, verify that the SelectedIndex value is greater than -1.

Also is a common good practice to verify that all the fields (tex boxes text, and combo boxes selected items ToString, etc.) does not contain an apostrophe before inserting them in a command.

Hope this helps.

Line 22

nd sorry for inconvenience..

Hey !! the suggestion helped.. Thank You so very much !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.