在为变量赋值之前使用变量

时间:2015-10-07 00:42:02

标签: vb.net

Private Sub btnCIAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnCIAdd.Click

    Dim cistatus, cifn, ciln, cicontact, cieadd, cidoa, ciRD, ciRT, ciRN, ciPPD, ciMOP, ciAmount As String
    Dim i, cicid As Integer

    For i = 0 To Me.DataGridView1.Rows.Count - 1
        cicid = DataGridView1.Rows(i).Cells(0).Value
        cistatus = Me.DataGridView1.Rows(i).Cells(1).Value
        cifn = Me.DataGridView1.Rows(i).Cells(2).Value
        ciln = Me.DataGridView1.Rows(i).Cells(3).Value
        cicontact = Me.DataGridView1.Rows(i).Cells(4).Value
        cieadd = Me.DataGridView1.Rows(i).Cells(5).Value
    Next

    cidoa = DateTimePicker2.Text
    ciRD = txtCIRD.Text
    ciRT = comboCIRT.Text
    ciRN = comboCIRN.Text
    ciPPD = txtCIPD.Text
    ciMOP = comboCIMD.Text
    ciAmount = txtCIAmount.Text



    cmd = "INSERT INTO checkin_info VALUES ('" & cicid & "','" & cistatus & "','" & cifn & "','" & ciln & "','" & cicontact & "','" & cieadd & "',','" & cidoa & "','" & ciRD & "','" & ciRT & "','" & ciRN & "','" & ciPPD & "','" & ciMOP & "')"
    rs.CommandText = cmd
    res = rs.ExecuteReader()
    MessageBox.Show("Entry added!")


End Sub

这是我的整个私有子,我得到的错误就像“变量'cicontact'在被赋值之前一样使用。空引用异常可能在运行时产生”与cistatus,cifn,ciln,cicontact和cieadd  我没有得到这个..我初始化是一个错误,我该如何解决?

1 个答案:

答案 0 :(得分:1)

检查此代码

 Private Sub btnCIAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnCIAdd.Click

        Dim cistatus As String=""
 Dim cifn As String="" 
Dim ciln As String=""
Dim  cicontact As String=""
Dim  cieadd As String=""
Dim  cidoa As String=""
Dim  ciRD As String=""
Dim  ciRT As String="" 
Dim ciRN As String=""
Dim  ciPPD As String=""
Dim  ciMOP As String=""
 Dim ciAmount As String=""
        Dim i  as Integer =0
Dim cicid As Integer=0

    cidoa = DateTimePicker2.Text
        ciRD = txtCIRD.Text
        ciRT = comboCIRT.Text
        ciRN = comboCIRN.Text
        ciPPD = txtCIPD.Text
        ciMOP = comboCIMD.Text
        ciAmount = txtCIAmount.Text

        For i = 0 To Me.DataGridView1.Rows.Count - 1
            cicid = DataGridView1.Rows(i).Cells(0).Value
            cistatus = Me.DataGridView1.Rows(i).Cells(1).Value
            cifn = Me.DataGridView1.Rows(i).Cells(2).Value
            ciln = Me.DataGridView1.Rows(i).Cells(3).Value
            cicontact = Me.DataGridView1.Rows(i).Cells(4).Value
            cieadd = Me.DataGridView1.Rows(i).Cells(5).Value

        cmd = "INSERT INTO checkin_info VALUES ('" & cicid & "','" & cistatus & "','" & cifn & "','" & ciln & "','" & cicontact & "','" & cieadd & "',','" & cidoa & "','" & ciRD & "','" & ciRT & "','" & ciRN & "','" & ciPPD & "','" & ciMOP & "')"
        rs.CommandText = cmd
        res = rs.ExecuteReader()
    next
        MessageBox.Show("Entry added!")


    End Sub