Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

VS 2008 VB 2008 to SQL reader, display results as label

$
0
0
Ok so what i am trying to accomplish is to conenct to an sql database and read line by line displaying a single result in a label, then displaying the next result in a label, think of a picture slide show, this is my first attempt in doing this or much of anything with sql, i have most worked with text and excel files before. the error i am also getting is executereader is connection not initialized. thanks in advance!!


Code:

Dim connetionString As String

02        Dim cnn As SqlConnection 

03   

04        Dim sqlpost As SqlCommand 

05   

06   

07   

08        connetionString = "Data Source=EXCHANGESVR;Initial Catalog=SteelObrien09Jun2011;Integrated Security=True"

09        cnn = New SqlConnection(connetionString) 

10        cnn.Open() 

11   

12        sqlpost = New SqlCommand("SELECT Emailaddress FROM Customer Contacts Where Emailaddress <=getstring ()") 

13   

14           

15        Dim reader As SqlClient.SqlDataReader 

16        Dim previousConnectionState As ConnectionState = cnn.State 

17        Try

18            If cnn.State = ConnectionState.Closed Then

19                cnn.Open() 

20            End If

21            reader = sqlpost.ExecuteReader() 

22            Using reader 

23                While reader.Read 

24                    ' Process SprocResults datareader here. 

25                       

26                    Do

27                        If reader("CustMassEmail") = True Then

28                            LCustEmail.Text = reader("Emailaddress").ToString 

29                        Else

30                            reader.NextResult() 

31                            If reader.NextResult = "" Then

32                                Exit Do

33                            ElseIf reader("EmailAddress").ToString = "" Then

34                                Exit Do

35                               

36                            End If

37                        End If

38   

39                    Loop

40   

41                    'Console.WriteLine(reader.GetValue(0)) 

42                End While

43                reader.Close() 

44   

45            End Using 

46        Finally

47            If previousConnectionState = ConnectionState.Closed Then

48                cnn.Close() 

49            End If

50        End Try


Viewing all articles
Browse latest Browse all 42220

Trending Articles



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