# Database Discussions > Microsoft SQL Server 2008 >  Connect c# application to database

## Angelica

I have this code to connect a c# application to my database in sql managment studio:

 public Form1()
        {
            InitializeComponent();
        }
        private bool CompareStrings(string string1, string string2)
        {
            return string.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture)   == 0 ? true : false;
        }
        private void Login_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection("Server=servername;DataBase=database  _db;Trusted_Connection=True;User Id=Id;Password=password");
                con.Open();
                SqlCommand command = new SqlCommand("SELECT ISNULL(UserName,'') AS UserName, ISNULL(Password, '') AS Password FROM LoginDetails WHERE UserName = " + UserName.Text + " and Password =" + Password.Text + "'", con);
                SqlDataReader dr = command.ExecuteReader();

                string userText = UserName.Text;
                string passwordText = Password.Text;

                while (dr.Read())
                {
                    if (this.CompareStrings(dr["UserName"].ToString(), userText) && this.CompareStrings(dr["Password"].ToString(), passwordText))
                    {
                        MessageBox.Show("OK");
                    }
                    else
                    {
                        MessageBox.Show("ERROR");
                    }
                }

                dr.Close();
                con.Close();
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message);
            }
        }

I have a simple login page to write username and password
the problem is that when I start this code and write  username and password I get an error: the login failed for user". the user is not associeted with a trusted sql connection

----------


## rmiao

When you set Trusted_Connection=True, you ask for windows authentication that doesn't use you id/pwd specified. It uses current windows account instead.

----------


## Angelica

what can I do to fix it?

----------


## rmiao

Depends on whether you like to use windows or sql authentication, you can find all kind of connection strings here http://www.connectionstrings.com/sql-server-2008

----------


## caral

Connect barcode features to SQL reporting services in to C# application, http://www.avapose.com/dotnet_barcod...enerator.shtml

----------

