Andrew
Andrew

Reputation: 3

Unable to connect to database C#

I've been trying to connect to my MS Sql database through the connection string in my App.config, but for some reason it fails to login, i just can't seem to figure it out.

This is my Connection method:

public void Con()
{

        string userName = userNameBox.Text;
        string passWord = passWordBox.Text;

        bool loginFail;

        SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lagerConn"].ConnectionString);

        //Search Connstring for User ID= & Password= and replace with username and password from Textboxes
        if (_Connstring.Contains("User ID="))
        {
            _Connstring = _Connstring.Replace("User ID=;", "User ID=" + userName + ";");

        }
        if (_Connstring.Contains("Password="))
        {
            _Connstring = _Connstring.Replace("Password=", "Password='" + passWord + "'");
        }

        try
        {
            Conn.Open();
            Conn.Close();
            loginFail = false;
        }catch
        {
            MessageBox.Show("Login Failed");
            loginFail = true;
        }
        if(loginFail == false) //If login is successful it will change to the next form and hide the Connect form
        {
            mainMenu secondForm = new mainMenu();
            secondForm.Show();
            this.Hide();

        }

    }`

And here is my App.config

        <add name="lagerConn" connectionString="Data Source=LagerServer;Initial Catalog=LagerDB;Persist Security Info=True;User ID=;Password="
        providerName="System.Data.SqlClient" />
</connectionStrings>

I got this exception

Upvotes: 0

Views: 485

Answers (3)

jmcilhinney
jmcilhinney

Reputation: 54487

There's no point putting an empty user ID and password field in the connection string at all. Just leave them out altogether and get rid of that pointless Persist Security Info too. Use a connection string builder, e.g.

SqlConnection builder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["lagerConn"].ConnectionString);

builder.UserID = userID;
builder.Password = password;

SqlConnection connection = new SqlConnection(builder.ConnectionString);

After that, don't just provide a generic error message and ignore the information the system gives you. Look at the exception and it will tell you what went wrong.

Upvotes: 2

mybirthname
mybirthname

Reputation: 18137

First you should not write UserID and Password in TextBoxes write them static in connection string. The exception told you everything-> Login failed for user ''. You didn't write correct user or password in the textbox.

Upvotes: 0

Guy Lowe
Guy Lowe

Reputation: 2370

Try:

string userName = userNameBox.Text;
string passWord = passWordBox.Text;
string connStr = ConfigurationManager.ConnectionStrings["lagerConn"].ConnectionString;
connStr = connStr.Replace("User ID=;", "User ID=" + userName + ";");
connStr = connStr.Replace("Password=", "Password='" + passWord + "'");
bool loginFail = false;
try{
    using (SqlConnection Conn = new SqlConnection(connStr){
       loginFail true;
    }
}
catch (SqlConnection sqlEx){
   //already false
}

And yes, beware of sql injection attacks!

Upvotes: 0

Related Questions