2015-10-07 73 views
-1
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一樣 我沒有得到這個..我初始化是一個錯誤,我該如何解決這個問題?使用變量之前,它已被分配一個值

+0

您有SQL注入漏洞。 – SLaks

+1

你的「For循環」是多餘的。所有你最終得到的是來自datagrid最後一行的值,所以你可以直接設置'i = Me.DataGridView1.Rows.Count - 1'並執行一次。 – DeanOC

+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 
+0

我試過了。它給出了一個錯誤,如「顯式初始化不允許使用單個類型說明符聲明多個變量」 –

+2

@KipRussel,所以在單行上聲明所有內容,但是這個回答是正確的,以便在循環中移動插入。此外,如果這兩行是連續的,則在一行上聲明並在另一行上分配是不好的做法。在一行上聲明和初始化變量 –

+0

Ye @ T.S。你需要用單行初始化每個變量 – makdu

相關問題