Solved

Login failed for user...

Posted on 2014-10-05
10
153 Views
Last Modified: 2014-10-08
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

        Try
            '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
            cnn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader

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

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 5
  • 3
  • 2
10 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 400 total points
ID: 40362442
Remove the single quotes the connection string.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40362448
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.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 40362624
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?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40362699
For single quote and ; use, please see:

Formatting Rules for Connection Strings  at http://www.connectionstrings.com/formating-rules-for-connection-strings/
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40363742
Are you sure you are looking at the correct code? The error message indicates windows authentication being used.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40363784
I want to use windows authentication.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 40363796
With Windows authentication, you do not provide the user name and password. Its the login of the user in Windows that provides the credentials.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40363939
With or without, I get the same error.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 100 total points
ID: 40364002
For the windows user, you need to add the user as a Login within SQL Server.

http://msdn.microsoft.com/en-us/library/aa337562.aspx
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40368896
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.

Thanks,

Mike
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now