Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code in vb.net , when i pass the insert query to the SQL server from VB.net i got exception at query that object reference is not set to the instance object,
in sql fileds are allow null and ID is auto incrrament.
one of my companion said that use record set and tell the condition somthing like that but i have totally no idea about record set in New in VB.net,
please help me, what should i do,

VB
Imports System.Data
Imports System.Data.SqlClient
Public Class MaintenanceTask
    Dim cn As New System.Data.SqlClient.SqlConnection
    Sub connect()
        cn = New System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=Fleet Maintainance;Integrated Security=True")
    End Sub
    Sub lockall()
        cBx1.Enabled = False
        cBx2.Enabled = False
        tBx1.Enabled = False
        tBx2.Enabled = False
        tBx3.Enabled = False
    End Sub
    Sub unlockall()
        cBx1.Enabled = True
        cBx2.Enabled = True
        tBx1.Enabled = True
        tBx2.Enabled = True
        tBx3.Enabled = True
    End Sub
    Sub setall()
        cBx1.Text = ""
        cBx2.Text = ""
        tBx1.Text = ""
        tBx2.Text = ""
        tBx3.Text = ""
    End Sub
    Sub updatecombo1()
        Call connect()
        Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [Name],[NameID] FROM [RepairName] order by [Name]", cn)
        Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
        Dim ds As New DataSet
        adp.Fill(ds)
        'ComboBox2.Items.Clear()
        cBx1.DisplayMember = "Name"
        cBx1.ValueMember = "NameID"
        cBx1.DataSource = ds.Tables(0)
    End Sub
    Sub updatecombo2()
        Call connect()
        Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [RepairType],[RepairTypeID] FROM [RepairType] order by [RepairType]", cn)
        Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
        Dim ds As New DataSet
        adp.Fill(ds)
        'ComboBox2.Items.Clear()
        cBx2.DisplayMember = "RepairType"
        cBx2.ValueMember = "RepairTypeID"
        cBx2.DataSource = ds.Tables(0)
    End Sub
    Sub updatecombo3()
        Call connect()
        Dim cd As New System.Data.SqlClient.SqlCommand("SELECT [Service],[ServiceID] FROM [Service] order by [Service]", cn)
        Dim adp As New System.Data.SqlClient.SqlDataAdapter(cd)
        Dim ds As New DataSet
        adp.Fill(ds)
        'ComboBox2.Items.Clear()
        cBx1.DisplayMember = "Service"
        cBx1.ValueMember = "ServiceID"
        cBx1.DataSource = ds.Tables(0)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If cBx1.Enabled = True Then
            Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, LaborCost, Total) values (" & (cBx1.SelectedValue.ToString()) & ", " & (cBx2.SelectedValue.ToString()) & ", " & CInt(tBx1.Text) & "," & CInt(tBx2.Text) & "," & CInt(tBx3.Text) & ")"
            Call connect()
            Dim cd As New System.Data.SqlClient.SqlCommand(str, cn)
            cd.Connection.Open()
            cd.ExecuteNonQuery()
            cd.Connection.Close()
            MsgBox(" New Task is added successfully ")
            Call lockall()
            Me.Close()
            Call IssueWorkOrder.listView2load()

        Else
            MsgBox(" Task is not added Try again ")

        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AddService.Show()
        AddService.Visible = True
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        RadioButton1.Text = "Preventive"
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        RadioButton2.Text = "Repair"
    End Sub

    Private Sub RadioButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.Click
        Call updatecombo3()
        Label3.Text = "Service"
        cBx2.Enabled = False
    End Sub

    Private Sub RadioButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.Click
        Call updatecombo1()
        Call updatecombo2()
        Label3.Text = "Repair"
        cBx2.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 15-Mar-13 23:22pm
v3
Comments
OriginalGriff 15-Mar-13 4:41am    
Where did you get the error?
There are so many possible sources of the problem that identifying the line is normally helpful - the error will tell you exactly which line caused the problem.

Check that the SelectedValue of your comboboxes is not Nothing as a first step in Button1_Click. Thou shalt not call ToString on Nothing!
 
Share this answer
 
Comments
Zubair Lohani 16-Mar-13 5:27am    
i didnot got ur point,
i face the problem in this insert query,
Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, LaborCost, Total) values (" & (cBx1.SelectedValue.ToString()) & ", " & (cBx2.SelectedValue.ToString()) & ", " & CInt(tBx1.Text) & "," & CInt(tBx2.Text) & "," & CInt(tBx3.Text) & ")"


all are int in data base and from here i also passing int value
Bernhard Hiller 18-Mar-13 6:29am    
The error is NOT raised on the database side! It is in your local code. Likely in the line I told you.
E.g. no selection was made in cBx2. Then cBx2.SelectedValue is Nothing, and you insist on calling ToString on cBx2.SelectedValue, i.e. you call Nothing.ToString. And that's exactly what a NullReference exception is.
Object Reference Not Set to an instance of object error comes when there is an mismatch in datatype of database and the object in which you set the data

ex :

datatype=int

but you set an data of string type...

Hope so it will solve your problem..:)
 
Share this answer
 
v2
Comments
Zubair Lohani 16-Mar-13 5:21am    
no all are int in database and i also sent the int, i face the problem in this insert query.
Dim str As String = "insert into MaintenanceTask (MainID, TypeID, PartCost, LaborCost, Total) values (" & (cBx1.SelectedValue.ToString()) & ", " & (cBx2.SelectedValue.ToString()) & ", " & CInt(tBx1.Text) & "," & CInt(tBx2.Text) & "," & CInt(tBx3.Text) & ")"
jaya verma 18-Mar-13 3:40am    
i think mainid and typeid are of int type..but @ time of setting the values in these fields you have converted them into string formate..

try this(code is in c# just convert it into vb)

String str = "insert into MaintenanceTask (MainID, TypeID ) values
(int.parse( cBx1.SelectedValue.ToString()),(int.parse(cBx2.SelectedValue.ToString()) ))
and so on for another fields
Bernhard Hiller 18-Mar-13 6:30am    
That's totally wrong. See my answer below.
Zubair Lohani 18-Mar-13 8:36am    
Bernhard where is ur answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900