2013-02-27 66 views
0

我的代碼有問題;並希望得到一些幫助,讓她做我想做的事情而不會產生惱人的錯誤。閱讀數據綁定列表框字符串,找到用戶選擇的字符串並將所述字符串寫入文件。我需要知道如何避免vb 2010中的System.Data.DataRowview

Private Sub btnContinue_Click(sender As System.Object, e 
    As System.EventArgs) Handles btnContinue.Click 
    ' 1st file and mySQL Update to testing table 
    If txtLocation.Text.ToString <> "" And txtUnitTested.Text.ToString <> "" Then 
     Dim filewriter As New System.IO.StreamWriter(
       "C:\Users\OER\Documents\Visual Studio 2010\Projects\frmTest1_0.txt") 
     Dim curItem = lstCustomer.SelectedItem 
     Dim now As DateTime = DateTime.Now 
     With filewriter 
      .WriteLine(vbCrLf + (now.ToString("F"))) ' Write the DateTime to the file 
     End With 

     For Each objDataRowView As DataRowView In lstCustomer.SelectedItems 
      Dim item As String 
      For Each item As DataRowView In Me.lstCustomer.Items.Item("customer") 
       item = String.Parse(lstCustomer.SelectedValue) 
       WriteLine(curItem(item)) 
      Next item 
     Next ' THIS IS THE FOR LOOP I AM HAVING ISSUES WITH!!!!!!! 

     With filewriter 
      .WriteLine(vbCrLf + txtLocation.Text + vbCrLf + txtUnitTested.Text) 
     End With 
     filewriter.Close() 
     frmTest1.Show() 
    Else 
     MsgBox("Please Type the Location and Unit Tested!!!", vbCritical) 
     txtLocation.Clear() 
     txtUnitTested.Clear() 
     txtLocation.Focus() 
    End If 
+2

你會得到什麼「煩人的錯誤」?我懷疑它是'HavingIssueException'。 – 2013-02-27 20:23:49

+0

沒有先生,這是邏輯錯誤(該程序將System.Data.DataView添加到我的數據庫中) – mauritaniawashin 2013-02-27 20:38:12

+0

就好像列表框中的數據需要單獨聲明或與數據綁定列表一起聲明,可以這麼說 – mauritaniawashin 2013-02-27 20:45:10

回答

1

這些循環:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems 
    Dim item As String 
    For Each item As DataRowView In Me.lstCustomer.Items.Item("customer") 
     item = String.Parse(lstCustomer.SelectedValue) 
      WriteLine(curItem(item)) 
    Next item 
Next 

將會給你帶來一些問題。您已經聲明瞭一個與另一個變量名稱相同的循環變量。然後你爲它分配一個值。

此外,我不是很清楚兩個循環的目的是什麼。我對你在這部分代碼中試圖完成的東西有些困惑。

Dim item as String 

我會用這個例子:我在這一行重命名變量開始

Dim result as string 

做別的東西,你不能再更改項目的值(因爲這是循環變量)。這會導致錯誤。

循環內更改代碼:

result = String.Parse(lstCustomer.SelectedValue) 

奇怪的是,這仍然不會讓你到你試圖去,但它是一個良好的開端。除非您的lstCustomer中的每個項目都包含多個要迭代的項目,否則無論如何,我認爲您可能只需要首先使用外部循環。

如果你的循環開始是這樣的:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems 

那麼我認爲你會想:

objDataRowView.Item("customer") 

的是什麼輸出。我認爲這將成爲您桌子上客戶領域的價值。

你基本上是說這一行有一列叫顧客....我希望這個價值。

+0

的DisplayMember屬性的「col」被試圖回答,儘管它很難理解。 – Neolisk 2013-02-27 21:12:16

+0

謝謝@D;我真的很感謝你的答案 – mauritaniawashin 2013-02-27 21:31:16

+0

一旦你理清了循環,我認爲你只需要引用數據行中的正確項目而不是數據行本身作爲輸出。爲了確保,我需要查看綁定列表的數據表的結構。 – Jay 2013-02-27 21:33:43