Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB10.0 VB.NET
This is my code , i am new to VB.net, exception os unhandled that object reference is not set to the instance of an object. in data base all are int which i m passing from VB.Net and ID is auto increment, what should i do,
 
Public Class Maintainance
    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
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Try
        If cBx1.Enabled = True Then
            Dim str As String
            str = "insert into AddMaintainanceTask (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
        'Catch ex As Exception
        'MsgBox(" Invalid Try agian ")
        'End Try
    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 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 Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AddService.Show()
        AddService.Visible = True
    End Sub
 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
 
    Private Sub Maintainance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
    End Sub
 
    Private Sub tBx3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tBx3.TextChanged
 
    End Sub
End Class
Posted 17-Mar-13 21:26pm
Comments
Aarti Meswania at 18-Mar-13 2:29am
   
which line gives error?
Zubair Lohani at 18-Mar-13 5:53am
   
the query line .
Marco Bertschi at 18-Mar-13 4:40am
   
Can you tell us the variable name which is not set to the instance of an object?
 
cheers,
Marco Bertschi
Zubair Lohani at 18-Mar-13 5:54am
   
i used just single variable str

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I know, my answer is of-topic, but i would like to give a right direction to code optimization:
Your code:
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
Can be changed as follow:
Sub LockUnlockAll(Optional bState AS Boolean = False)
    cBx1.Enabled = bState
    cBx2.Enabled = bState
    tBx1.Enabled = bState
    tBx2.Enabled = bState
    tBx3.Enabled = bState
End Sub
 
Read about Optional Arguments[^] and/or Optional Parameters (VB)[^].
 
3 most popular reasons of "Object reference..." error: http://codebetter.com/raymondlewallen/2005/06/23/system-nullreferenceexception-object-reference-not-set-to-an-instance-of-an-object-3-common-causes-in-vb-net/[^]
  Permalink  
Comments
Zubair Lohani at 18-Mar-13 5:58am
   
but the exception is unhandled at query of insrtion dear,
plz help me
Maciej Los at 18-Mar-13 6:02am
   
In which line???
adrian@csdev at 21-Mar-13 18:17pm
   
Double check the column names in your database and make sure all names are correct also try this in your code
 
Just copy and replace your current insert string
 
str = "insert into AddMaintainanceTask(MainID, TypeID, PartCost, LaborCost, Total)values('" & (cBx1.SelectedValue.ToString()) & "','" & (cBx2.SelectedValue.ToString()) & "','" & Integer.Parse(tBx1.Text) & "','" & Integer.Parse(tBx2.Text) & "','" & Integer.Parse(tBx3.Text) & "')"
 
And let me know if this sorted out your problem

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

  Print Answers RSS
0 Marcin Kozub 275
1 OriginalGriff 264
2 Sergey Alexandrovich Kryukov 215
3 Praneet Nadkar 197
4 Richard MacCutchan 182
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100