I would recommend doing this:
1) Change the connection string to:
<add name="SQLServerConnection" connectionString="Server=.;Database=database1;Trusted_Connection=True;"/>
"Server =. - using an instance of SQL Server on your computer,
'Trusted_Connection = True' - Windows authentication is used to verify your access to the instance of SQL Server.
2) Check Sql Management Studio - your Windows user has access rights to the database1.
The second error you get is because you have to add '@' to the parameter name, for example:
command.Parameters.AddWithValue("@email", email); command.Parameters.AddWithValue("@password", password);
I also recommend that you change your code as follows:
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(); } }
Bartลomiej mucha
source share