Login failed for user...

I was under impression that this code will be using windows authentication. But I am getting:

Login failed for user 'Mike-VAIO\Mike'.

Question: How can I handle this error?
        Dim cnn As SqlConnection = New SqlConnection

            'Dim strCnn As String = "Data Source=.\SQLEXPRESS12;AttachDbFilename=NORTHWND.MDF;Integrated Security=True;Connect Timeout=30"
            Dim strCnn As String = "Server='Mike-Vaio\SQLEXPRESS12';Database='NORTHWND.MDF';User='Mike';Password='XXXXX';Trusted_Connection=False;"
            Dim cmd As SqlCommand = New SqlCommand("Select * From tblProduct", cnn)
            cnn.ConnectionString = strCnn
            Dim dr As SqlDataReader = cmd.ExecuteReader

            If dr.HasRows Then
                MessageBox.Show("Has Records")
                MessageBox.Show("Has no Records")
            End If
            ' DataTable dt = new DataTable();
            'dataGridView1.DataSource = dt;
        Catch ex As Exception
        End Try

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Remove the single quotes the connection string.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Without single quotes I get other errors. I think I need to have them as they are.

For example my paswword is something XXXX;YYAA so for ; I have to use an extra \

This means I have to keep them inside single quotes (password =' XXXX\;YYAA') in order for my application not to get confused.
Jacques Bourgeois (James Burger)PresidentCommented:
A connection string in not SQL. I have never seen one that uses single quotes.

And the semi-colon in the password could well be your problem.

; separates the elements of a connections string. I do not think that the escape character works in that context. This is not something I have seen in the documentation. I tried building the string with the SqlConnectionStringBuilder, a class whose purpose is to build a connection string through properties, but it has no reaction to the ;

Have you tried changing the password or logging as another user who does not have a ; in the password?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
For single quote and ; use, please see:

Formatting Rules for Connection Strings  at http://www.connectionstrings.com/formating-rules-for-connection-strings/
Are you sure you are looking at the correct code? The error message indicates windows authentication being used.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I want to use windows authentication.
Jacques Bourgeois (James Burger)PresidentCommented:
With Windows authentication, you do not provide the user name and password. Its the login of the user in Windows that provides the credentials.
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
With or without, I get the same error.
For the windows user, you need to add the user as a Login within SQL Server.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi James,

I got the solution. You were correct on the comments you have posted:

Dim strCnn As String = "Server=.\SQLEXPRESS12;Database=NORTHWIND.MDF';Trusted_Connection=True"
cnn.ConnectionString = strCnn
Dim cmd As SqlCommand = New SqlCommand("Select * From Products", cnn)

The letter in bold also were missing before.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.