0

I would like to post my SQL results into a textlist in VB.net, I am selecting from 2 fields, UserName & Passwords

My query is:

USE fasttest2008;

SELECT [username],
       [password]
FROM   users;

I want it to post into tb_user & tb_pass

I have looked at another post on this site and managed to nearly get it, i feel that I am missing just one thing.

I am aware this code only posts the results into one field atm.

Dim sConnString As String = "Data Source=.\scuser;Initial Catalog=master;Integrated Security=True"
        Dim sText As String = String.Empty

        Using cn As New SqlConnection(sConnString)
            cn.Open()
            Dim cmd As New SqlClient.SqlCommand("use testdb select [username], [password] from users")
            Dim r As SqlDataReader = cmd.ExecuteReader()
            If Not r.HasRows Then Exit Sub

            Do While r.Read()
                sText = sText & ";" & r.GetString(0)
            Loop

            cn.Close()
        End Using

        lb_user.Text = sText
9
  • sText = r.GetString(0) & " - " & r.GetString(1) Passwords should not be stored as plaintext Commented Feb 12, 2016 at 14:49
  • @Plutonix The error I get is: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: ExecuteReader: Connection property has not been initialized. Commented Feb 12, 2016 at 14:50
  • on which line? is that the correct connection string? Commented Feb 12, 2016 at 14:51
  • Cause you didn't associate the cn to cmd.. Commented Feb 12, 2016 at 14:53
  • 2
    Note that your title has nothing to do with a connection problem, the post doesnt mention an error and asks about something completely different. Commented Feb 12, 2016 at 15:02

1 Answer 1

3

Try modify the Connection to this:

Dim cmd As New SqlClient.SqlCommand("use testdb select [username], [password] from users", cn);

You have to associate the cn to cmd

As in the comment, you said the query isn't returning anything, so try removing the use testdb, and try running it in the Database server, see if it returns anything

Sign up to request clarification or add additional context in comments.

3 Comments

Edit: Thanks, | This now goes through, however it doesn't return my results.
I used a breakpoint and it was contained in sText so I know it is their.
So you will have to find out when the sText is cleared

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.