2013-06-06 65 views
1

我已經閱讀了現有的代碼在stackoverflow,但我仍然失去了我如何解決這個問題。 此行已屬於此表。 任何建議總是表示讚賞。 它在我的命令按鈕保存事件時出錯。 我列出了整個表單的代碼,只是爲了讓你知道我在做什麼。希望這有助於「這行已經屬於這個表」

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click 
    DataGridView1.EndEdit() 
    Dim con As SqlConnection = CompDB.GetConnection 
    Dim cmd As New System.Data.SqlClient.SqlCommand 
    Dim SPgetStandards As String = "dbo.SPgetStandards" 
    Dim SPgetRecordDetail As String = "dbo.SPgetRecordDetail" 


    Dim dsStandard As New DataSet 

    Dim dsRecords As New DataSet 
    Dim currentrow As DataRow 
    Dim currentcolumn As DataColumn 


    MsgBox("Record id is " & RecordID) 
    Using con 
     con.Open() 

     Dim Standardsdataadapter As New SqlClient.SqlDataAdapter(SPgetStandards, con) 

     Dim Detaildataadapter As New SqlClient.SqlDataAdapter(SPgetRecordDetail, con) 
     Detaildataadapter.Fill(dsRecords, "Record") 
     Standardsdataadapter.Fill(dsStandard, "Standard") 




     '5/31/13 
     'Figured out how to run a loop through a table and write to the console 

     For Each currentrow In dsStandard.Tables("Standard").Rows 
      For Each currentcolumn In dsStandard.Tables("Standard").Columns 
       'Debug.WriteLine(currentrow(currentcolumn) & ControlChars.Tab & ControlChars.Tab) 
       'MsgBox(currentcolumn.ToString) 
       'Debug.WriteLine(currentrow.Item(0)) 
      Next currentcolumn 

     Next currentrow 

     ' con.Close() 
     'con.Dispose() 

     Dim newrow As DataRow = dsRecords.Tables("Record").NewRow 
     For Each oRow As DataGridViewRow In DataGridView1.Rows 
      If Convert.ToInt16(oRow.Cells("chkbox").Value) = 1 Then 
       ' Debug.WriteLine("It was clicked ", DataGridView1.Columns.Item(1)) 
       Debug.WriteLine(oRow.Cells("ID").Value) 
       'dsStandard.Tables.Add.Rows("RecordID") = 1234 
       'dsRecords.Tables.Add.Rows("Standard_Name_ID") = 1234 
       ' newrow("Record_Detail_ID") = RecordID 
       ' newrow("Record_ID") = RecordID 
       newrow("Standard_Name_ID") = (oRow.Cells("ID").Value) 
'/////Errors out here!!!!!dsRecords.Tables("Record").Rows.Add(newrow) '/////Errors out here!!!!! 
       dsRecords.AcceptChanges() 


      End If 

     Next 
    End Using 




End Sub 

回答

0

移動這一行:

Dim newrow As DataRow = dsRecords.Tables("Record").NewRow 

是你If塊內的第一線。

+0

這實際上幫助沒有錯誤,但是,我沒有在我的數據庫表中。所以,我還有其他不正確的東西。 – Plenahan

相關問題