user1491801
user1491801

Reputation:

Assistance with SqlConnection

I am working on trying to connect my project to a database, which I am fairly new when it comes to working with databases. When I run the program I get a server error stating that login failed for user. I am working on a sign up form that when filled out will populate the fields in the database after clicking create account.

This is my back end code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class SignUp : System.Web.UI.Page
{

    SqlConnection conn = new SqlConnection();

    protected void Page_Load(object sender, EventArgs e)
    {
        conn.ConnectionString = @"Data Source=184.168.47.13;Initial Catalog=portfoliobrown;User ID=crossfire19;Password=***********";
    //error occurs here    conn.Open();
    }

    public void ExecuteConnection()
    {

        SqlCommand Command = new SqlCommand("Insert into SignUp" + "(FirstName, LastName, Password, Email)values(@FirsName, @LastName, #Password, @Email)",conn);

        Command.Parameters.AddWithValue("@FirstName", FirstName.Text);
        Command.Parameters.AddWithValue("@LastName", LastName.Text);
        Command.Parameters.AddWithValue("@Password", Email.Text);
        Command.Parameters.AddWithValue("@Email", Password.Text);
        Command.ExecuteNonQuery();

        conn.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ExecuteConnection();
    }
}

I have a table in the database. FirstName, LastName, Password, Email, Etc Also my password and user name for the database should be correct, as when I test the connection it works just fine

Thanks for any help I would appreciate it.

Upvotes: 1

Views: 89

Answers (2)

Prescott Chartier
Prescott Chartier

Reputation: 1653

This is the connection string format I use, it was coded with VS 2010 and has been in production for 8 years now. First coded with VS 2008 upgraded to VS 2010 a few years ago.

connectionString="server=192.168.1.1;database=MyDb;uid=sa; pwd=Password" providerName="System.Data.SqlClient"

Hope this helps.

R/ Prescott ....

Upvotes: 0

Richardissimo
Richardissimo

Reputation: 5765

Without any more detail, the only thing I have to go on is the phrase "login failed for user". So you need to consider what process the code is running as when it's trying to make this connection. Given that this appears to be a web-page, I would hazard a guess that it is being run by the IIS process (check which user that is). I can't tell whether the process will be able to "see" the IP address that you've given in the connection string; my guess would be that it can't. I'm guessing that you're using Microsoft SQL server, since you've used the tag.

There are a whole bunch of reasons why this error can occur: the process's ability to see the network (e.g. local services can't see the network), firewalls (blocking the port that SQL server uses), configuration of the SQL server to allow remote connections (I think it's 'off' by default), allow mixed-mode authentication (again, depending on the version, I think this is off by default), maybe the database name is wrong, maybe the user doesn't exist on the server, or doesn't have permission to that database, or maybe (for once) the error message is actually right and the password that has been given in the connection string isn't right. (This is not a complete list.)

If I'm right that this is running under IIS, I would suggest putting exactly the same code in a console application, to assist with the diagnosis. When you run the console app, it will be running as you, with your permissions to access the network, rather than the IIS user. If that doesn't work, then (if you can) run the console app on the same machine as the SQL server: if this works, then you know it is just a networking/connection/configuration issue.

As well as the concerns expressed above about SQL injection (thanks for addressing those), I would suggest that you also read up on things that are IDisposable, and how to use the "using" statement (not the one that appears with the namespaces at the top). Both the connection and the command are disposable, so are best used in a using block.

Hope this helps. If it doesn't, then you'll need to give more information.

Upvotes: 1

Related Questions