Hi all; i'm trying to connect my program to an SQL Server Express database... however i am just getting an error that simply states:

"Login failed for user 'sa'."

Within my code this appears on:

my code so far is:

Imports System.Data.SqlClient
Imports System.Data

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim objConnection As SQLConnection = New _
SqlConnection("Server=[ServerName];Database=[DatabaseName];" & _
"user id=sa;password=")

Dim objCommand As SqlCommand = New SqlCommand()

objCommand.Connection = objConnection


ComboBox1.Text = objCommand.CommandText = "SELECT DISTINCT UserName" & _
"FROM tblUsers;"


End Sub

Essentially i am trying to connect to the Sql server database and draw the contents of the UserName column into the combobox.
Any advice on where i'm going wrong? Thanks

7 Years
Discussion Span
Last Post by Atove

ok stupid question: did u set a password for user sa? i think it have a password by default.

Also the 'sa' account can be enabled/disabled. Make sure you have the proper password and the account has been enabled.



SQL Express database allows Integrated Windows authentication scheme only. So, your connection string for MS-SQL remote database might be,

Dim Cn As SQLConnection
Cn = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True")

NOTE: Integrated Security=True - This property automatically uses windows os credentials.

@GeekByChoice - ok stupid question.
-- DON'T offend peoples.


@GeekByChoice - ok stupid question.
-- DON'T offend peoples.

i didnt :|
i didnt say he ask'd a stupid question. i felt like i ask a stupid question o.O


Thanks for the assistance guys; it was a combination of things that were preventing this from working.

I had a look at the 'sa' account and found it wasn't enabled; i then had password difficulty but managed to set it in the end.

I then had another look at my code and made some alterations; below is the working code:

Dim objConnection As SqlConnection = New  _
        SqlConnection("Server=[ServerName];Database=[DatabaseName];user id=sa;password=password")

        Dim objCommand As SqlCommand = New SqlCommand()

        objCommand.Connection = objConnection

        objCommand.CommandText = "SELECT DISTINCT UserName " & _
                           "FROM tblUsers;"

        Dim com As New SqlCommand(objCommand.CommandText, objConnection)

        Dim da As SqlDataReader
        da = com.ExecuteReader
        While da.Read
        End While

Once again; thanks for all your help! I really appreciate it. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.