Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27212

VS 2010 Error: Reader is closed...

$
0
0
Hello all,

I am fairly new to vb so excuse me for making stupid mistakes :)

I'm writing a simple form that gets data from a sql server and for every result there should be 2 files be written.
One .xml file and one .txt file

All works well but for some reason I get the error that the reader is closed...
The error is shown by VS2010 on the "Next" from "For Each User in rsUsers"
Here is my error:
Name:  vs2010-error.png
Views: 46
Size:  14.9 KB

The code I'm using is this:
Code:

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Security.Principal
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement

Public Class Form1

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim intAantalUsers As Int16 = 0

        Dim strSenderEmail As String = UserPrincipal.Current.EmailAddress

        Dim strFileNameXML As String
        Dim strFileNameTXT As String
        Dim strPath As String = "c:\temp"
        Dim strFullPathXML As String
        Dim strFullPathTXT As String
        Dim strGSMNummer As String

        Dim strXMLPart01 As String = "test"
        Dim strXMLPart02 As String = "test"
        Dim strXMLPart03 As String = "test"
        Dim strXMLPart04 As String = "test"
        Dim strXMLPart05 As String = "test"

        If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then

            strQuery = "SELECT TOP (100) PERCENT dbo.database.No_ AS Nummer FROM dbo.database "

            Dim cn As System.Data.SqlClient.SqlConnection
            cn = New System.Data.SqlClient.SqlConnection("Data source=server.domain.org;database=database;user id=userID;password=Password;")
            cn.Open()

            Dim getData As New System.Data.SqlClient.SqlCommand(strQuery, cn)

            Dim rsUser As System.Data.SqlClient.SqlDataReader
            rsUser = getData.ExecuteReader(Data.CommandBehavior.CloseConnection)

            intAantalUsers = 0
            For Each User In rsUser
                intAantalUsers = intAantalUsers + 1
            Next

            Dim strAantalUsers As String
            Dim strMessageBox As String

            strAantalUsers = Convert.ToString(intAantalUsers)
            strMessageBox = "Wil je de boodschap: '" + TextBox1.Text + "' naar " + strAantalUsers + " gebruikers sturen?"
            Dim drResult1 As DialogResult = MessageBox.Show(strMessageBox, "Bevestiging", MessageBoxButtons.YesNo)

            If drResult1 = DialogResult.Yes Then
                For Each User In rsUser
                    ' MessageBox.Show(User("Nummer"))

                    strGSMNummer = User("Nummer")
                    strGSMNummer = strGSMNummer.Replace("/", "")
                    strGSMNummer = strGSMNummer.Replace(".", "")
                    strGSMNummer = strGSMNummer.Replace(" ", "")
                    strGSMNummer = strGSMNummer.Replace("+320", "+32")

                    strFileNameTXT = strGSMNummer.Replace("+32", "0032") + ".txt"
                    strFileNameXML = strGSMNummer.Replace("+32", "0032") + ".xml"

                    strFullPathXML = strPath + "\" + strFileNameXML
                    strFullPathTXT = strPath + "\" + strFileNameTXT
                    Dim objWriterXML As New System.IO.StreamWriter(strFullPathXML)
                    Dim objWriterTXT As New System.IO.StreamWriter(strFullPathTXT)

                    objWriterTXT.Write(TextBox1.Text)
                    objWriterTXT.Close()

                    objWriterXML.Write(strXMLPart01)
                    objWriterXML.Write(Environment.NewLine)
                    objWriterXML.Write(strXMLPart02)
                    objWriterXML.Write(Environment.NewLine)
                    objWriterXML.Write(strXMLPart03)
                    objWriterXML.Write(Environment.NewLine)
                    objWriterXML.Write(strXMLPart04)
                    objWriterXML.Write(Environment.NewLine)
                    objWriterXML.Write(strXMLPart05)
                    objWriterXML.Write(strFileNameTXT)
                    objWriterXML.Close()
                Next
            End If

        Else
            MessageBox.Show("No input...")
        End If
    End Sub
End Class

If anyone could point me out where the problem is?
Thnx!

Tazzy
Attached Images
 

Viewing all articles
Browse latest Browse all 27212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>