• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

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

        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
Mike Eghtebas
Asked:
Mike Eghtebas
  • 5
  • 3
  • 2
4 Solutions
 
Jacques Bourgeois (James Burger)PresidentCommented:
Remove the single quotes the connection string.
0
 
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.
0
 
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?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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/
0
 
CodeCruiserCommented:
Are you sure you are looking at the correct code? The error message indicates windows authentication being used.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I want to use windows authentication.
0
 
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.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
With or without, I get the same error.
0
 
CodeCruiserCommented:
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
 
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.

Thanks,

Mike
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now