叶无歆 2022-06-23 23:45 采纳率: 100%
浏览 71
已结题

vb.net的变量在赋值前被使用

问题遇到的现象和发生背景

vb.net的变量‘sqlcmd’在赋值前被使用,可能会在运行时导致null引用异常

问题相关代码,请勿粘贴截图

Public Class FrmLInfoEdit
    Private Sub SetHeadTitle()
        '设置每列标题
        dGridL.Columns(0).HeaderText = "住户号"
        dGridL.Columns(1).HeaderText = "姓名"
        dGridL.Columns(2).HeaderText = "性别"
        dGridL.Columns(3).HeaderText = "年龄"
        dGridL.Columns(4).HeaderText = "籍贯"
        dGridL.Columns(5).HeaderText = "民族"
        dGridL.Columns(6).HeaderText = "出租人"
        dGridL.Columns(7).HeaderText = "房屋号"
        dGridL.Columns(8).HeaderText = "入住日期"
        dGridL.ColumnHeadersHeight = 28
        Dim i As Integer
        For i = 0 To 8
            dGridL.Columns(i).Width = 90
        Next i
        dGridL.Columns(11).Width = 0
    End Sub

    Private Sub frmLInfoEdit_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim da As SqlClient.SqlDataAdapter = Nothing
        Dim dt As DataTable
        dt = SQLQRY("SELECT*FROM Resident", da)
        dGrdL.DataSource = dt
        SetHeadTitle()
    End Sub

    Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Click
        Dim sqlcmd As String
        If Not "OptLNo" And Not "OptLName" And Not "OptAll" Then
            MsgBox("请输入要查询的方式!", vbOKOnly, "提示")
            Exit Sub
        End If
        If "OptLNo" Then
            If txtLNo.Text = "" Then
                MsgBox("请输入住房号!", vbOKOnly, "提示")
                Exit Sub
            End If
            sqlcmd = "SELECT*FROM Resident WHERE Resident_ID=""&txtLNo.Text&"""
        End If
        If "OptLName" Then
            If txtLName.Text = "" Then MsgBox("请输入姓名!", vbOKOnly, "提示")

            Exit Sub
        End If
        If sqlcmd = "SELECT*FROM Resident WHERE Name=""&txtLName.Text&""" Then
        End If
        If "OptAll" Then sqlcmd = "SELECT*FROM Resident"

        Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter()
        Dim dt As DataTable = New DataTable()
        dt = SQLQRY(sqlcmd, da)
        If dt.Rows.Count = 0 Then MsgBox("查无结果!", vbOKOnly, "提示")
        Exit Sub

        dGrdL.DataSource = dt
        SetHeadTitle()
    End Sub
    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Dim sqlcmd As String
        Dim dt As DataTable
        Dim Resident_Id As String
        Resident_Id = dGrdL.CurrentRow.Cell("Resident_Id").Values.ToString()
        If String.IsNullOrEmpty(Resident_Id) Then
            MsgBox("没有被选择的记录!", vbOKOnly, "提示")
            Exit Sub
        End If
        sqlcmd = "SELECT*FROM Resident WHERE Resident_ID=""&ResidenID&"""
        dt = SQLQRY(sqlcmd, Nothing)
        If dt.Rows.Count = 0 Then
            MsgBox("找不到住户资料!", vbOKOnly, "提示")
            Exit Sub
        End If
        If dt.Rows(0).Item("Leave_Time") & "" <> "" Then
            MsgBox("住户已经退租了!", vbOKOnly, "提示")
            Exit Sub
        End If
        frmLInfoRecord.Text = "住户住房信息修改"
        frmLInfoRecord.txtAdNum.Text =
        frmLInfoRecord.txtName.Text =
        frmLInfoRecord.txtAge.Text = dt.Rows(0).Item("Age")
        frmLInfoRecord.CboSex.Text = dt.Rows(0).Item("Sex")
        frmLInfoRecord.txtOrigin.Text = dt.Rows(0).Item("Native_Place")
        frmLInfoRecord.txtVolk.Text = dt.Rows(0).Item("Nation")
        frmLInfoRecord.txtHouseNo.Text = dt.Rows(0).Item("House_No")
        frmLInfoRecord.txtCboDate.Text = dt.Rows(0).Item("InCome_Time")
        frmLInfoRecord.ShowDialog()
    End Sub


    Private Function SQLQRY(ByVal p1 As String, ByVal da As SqlClient.SqlDataAdapter) As DataTable
        Throw New NotImplementedException
    End Function

    Private Function dGrdL() As Object
        Throw New NotImplementedException
    End Function

    Private Function dt() As Object
        Throw New NotImplementedException
    End Function
    Private Sub OptAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptAll.CheckedChanged
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim Resident_ID As String
        Dim sqlcmd As String
        Resident_ID = dGrdL.CurrentRow.Cells("Resident_ID").Values.ToString()
        If String.IsNullOrEmpty(Resident_ID) Then
            MsgBox("没有被选择的记录!", vbOKOnly, "提示")
            Exit Sub
        End If
        If MsgBox("是否确定实需要删除该住户档案?", vbYesNo + vbQuestion + vbDefaultButton2, "提示") = vbYes Then
            sqlcmd = "Delete From Resident Where Resident_ID=""&ResidentID&"""
            SQLDML(sqlcmd)
            MsgBox("已删除该记录!", vbOKOnly, "提示")
            frmLInfoEdit_Load(Me, New EventArgs())
        End If
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
        Me.Dispose()
    End Sub
End Class

运行结果及报错内容

img

求指点,再次感谢!

  • 写回答

2条回答 默认 最新

  • 急速光粒 2022-06-24 12:03
    关注

    跟sqlcmd 有没有初始化没什么关系啊!而是你的SQLQRY根本没有实现,直接抛出异常了啊!你得实现SQLQRY函数实现查询才能正常使用。有帮助请采纳,谢谢!

    img

        Private Function SQLQRY(ByVal p1 As String, ByVal da As SqlClient.SqlDataAdapter) As DataTable
            Throw New NotImplementedException
        End Function
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月2日
  • 已采纳回答 6月24日
  • 赞助了问题酬金10元 6月23日
  • 创建了问题 6月23日

悬赏问题

  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事
  • ¥15 系统容量变化的几种多址方式(TDMA,FDMA,OFDMA,CDMA)对比(相关搜索:曲线图)
  • ¥15 worldclim 历史及未来气候数据矫正
  • ¥15 ajax服务器不能下载
  • ¥15 运用c++和opencv实现二维码的识别和三维坐标的建立
  • ¥100 理想汽车的ADB为什么到了国外换了SIM就可以打开?
  • ¥15 k210烧入flash报错error:2005