19

I've been trying to connect to my database (which is on the same computer as my code) through my C# code. The problem is I keep getting the "Login failed for user " "" error... I admit that my knowledge of connecting to databases is minimal and I've tried almost all the steps in other questions!

here's part of my code:

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
        SqlCommand command = connection.CreateCommand();
        command.CommandText = @"IF EXISTS
                            (
                              SELECT *
                              FROM user 
                              WHERE EMAILADRES = @email and WACHTWOORD = @password
                            ) SELECT CAST (1 as bit) 
                            ELSE
                              SELECT CAST(0 as bit)";
        command.Parameters.AddWithValue("email", email);
        command.Parameters.AddWithValue("password", password);


        connection.Open();
        object ReturnBool = command.ExecuteScalar();
        connection.Close();

and this is my connection string:

<add name="SQLServerConnection" connectionString="Server=localhost; Database=database1;uid=NT AUTHORITY\NETWORK SERVICE" />
3
  • In your web.config file, what is your authentication mode set to? Commented Jan 28, 2013 at 17:44
  • 2
    Try removing the uid=NT AUTHORITY\NETWORK SERVICE part and replace that with Trusted_Connection=True. Commented Jan 28, 2013 at 17:45
  • 1
    passwords in plain text? say it aint so! Commented Jan 28, 2013 at 17:47

5 Answers 5

33

you need to change the connection string;

<add name="SQLServerConnection" connectionString="Server=localhost;Database=database1;Trusted_Connection=True;/>

if you are using windows authentication to connect to the local DB, you need to set Trusted_Connection=True; if you are using SQL server authentication, you need to declare User Id=myUsername; Password=myPassword;.

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

Comments

9

I would recommend do this:

1) Change connection string to:

<add name="SQLServerConnection" connectionString="Server=.;Database=database1;Trusted_Connection=True;"/>

'Server=.' - default instance of SQL Server on your machine is used,

'Trusted_Connection=True' - Windows Authentication is used to validate your access to SQL Server instance.

2) Check in Sql Management Studio is your windows user has permissions to access 'database1'.

The second error you are getting because you should add '@' in name of parameter like this:

command.Parameters.AddWithValue("@email", email);
command.Parameters.AddWithValue("@password", password);

I would also recommend that you change your code like this:

using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString))
{
    using (var command = connection.CreateCommand())
    {
        command.CommandText = @"IF EXISTS
                        (
                          SELECT *
                          FROM user 
                          WHERE EMAILADRES = @email and WACHTWOORD = @password
                        ) SELECT CAST (1 as bit) 
                        ELSE
                          SELECT CAST(0 as bit)";

        command.Parameters.AddWithValue("@email", email);
        command.Parameters.AddWithValue("@password", password);

        connection.Open();
        var result = command.ExecuteScalar();
    }
}

2 Comments

Hi! Thanks for the quick reply, I changed my connection string to yours. But now i get an error saying: "The parameterized query '(@email nvarchar(4000),@password nvarchar(4000))IF EXISTS ' expects the parameter '@email', which was not supplied." I guess it's finally connecting, though i don't know what's causing this error!
Yes, somebody else gave me the same answer about the second error. But I'm still getting it.
3

Change your connection string.

<add name="SQLServerConnection" connectionString="Server=.;Database=database1;Trusted_Connection=True;"/>

If you use server authentication,

<add name="SQLServerConnection" connectionString="Server=.;Database=database1; UserId = Username; Password = Password;"/>

If you still have error,

Check your sql services and protocols in sql server configuration manager.

Comments

2

Try like below... it will help you..

        string email ="[email protected]" //Give your email id to check
        string password ="Test" //Give your Password to check
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLServerConnection"].ConnectionString);
        SqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM user WHERE EMAILADRES = @email and WACHTWOORD = @password" 
        command.Parameters.AddWithValue("@email", email);
        command.Parameters.AddWithValue("@password", password);
        connection.Open();
        SqlDataReader dr = command.ExecuteReader();
        if (dr.Read())
        MessageBox.Show("Exists");
        else
        MessageBox.Show("Not Exists");
        connection.Close();

2 Comments

Tried this, but it didn't work! Still getting the same error.
The parameterized query '(@email nvarchar(4000),@password nvarchar(4000))IF EXISTS ' expects the parameter '@email', which was not supplied.
2

Probably you are missing "Integrated Security=true" in the connection string. If the connection string looks like:

"server=@server_name\\SQLEXPRESS;database=EmployeeDB;TrustServerCertificate=true"

and you are connected to that database server using Windows Authentication then you need to add "Integrated Security=true" to the connection string.

Comments

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.