对象引用未设置为实例对象, [英] object reference is not set to the instance object,

查看:52
本文介绍了对象引用未设置为实例对象,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在vb.net中的代码,当我从VB.net将插入查询传递给SQL服务器时,我在查询时得到异常,即对象引用未设置为实例对象,< sql fileds中的
是允许为null而ID是自动包含。

我的一个同伴说使用记录集并告诉条件这样的事情但我完全不知道在VB.net新建的记录集,

请帮帮我,我该怎么办,



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,

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]已添加代码块 - OriginalGriff [/ edit]




[edit]Code block added - OriginalGriff[/edit]

推荐答案

检查组合框中的 SelectedValue 是否为 Button1_Click 中的第一步。你不能在 Nothing 上调用 ToString
Check that the SelectedValue of your comboboxes is not Nothing as a first step in Button1_Click. Thou shalt not call ToString on Nothing!


对象引用未设置为当数据库的数据类型与您设置数据的对象不匹配时,会出现对象错误的实例



ex:



datatype = int



但是你设置了一个字符串类型的数据...



希望这样能解决你的问题.. :)
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..:)


这篇关于对象引用未设置为实例对象,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆