I have an issue with my code; and would appreciate some assistance getting her to do what I want without an annoying error. Read the databound listbox strings, find the user selected string and write said string to a file.

    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
有帮助吗?

解决方案

These loops:

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 

Are going to cause you some problems. You have declared a loop variable with the same name as another variable. And then you are assigning a value to it.

Also, I am not very clear on what the purpose of the two loops are. I am somewhat confused about what you are trying to accomplish in this section of code. I would start by renaming the variable in this line:

Dim item as String

I am going to use this for an example:

Dim result as string

Make that something else, and you cannot then change the value of item (since that is the loop variable) in the loop. That will cause an error.

Changing the code inside the loop to:

result = String.Parse(lstCustomer.SelectedValue)

Odds are this still won't get you to where you are trying to go, but it is a good start. Unless each item in your lstCustomer contains multiple items to iterate through I think you probably only need that first, outer loop, anyway.

If your loop starts like this:

For Each objDataRowView As DataRowView In lstCustomer.SelectedItems

Then I think you will want:

objDataRowView.Item("customer")

as what is output. I think that will be the value of the customer field from your table.

You are basically saying that this row has a column called customer.... I would like that value please.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top