Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Sir,
I am creating a window application but i have some problem in connection with Sql Database. Here we copy all code , the error point is conn.open(); I do not understand what is the error. can you help me.

private void btnLogin_Click(object sender, EventArgs e)
        {
            int t = 0;
            SqlConnection conn = new SqlConnection("Data Source=DONUT-90CF4E6F4\SQLEXPRESS;Initial Catalog=Resort;Integrated Security=True");
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from AdminLogin", conn);
            SqlDataReader dr= cmd.ExecuteReader();
            dr.Read();
            try
            {
                do
                {
                    if (textBox1.Text.Equals((dr.GetString(0)).ToString()) && textBox2.Text.Equals((dr.GetString(0)).ToString()))
                    {
                        t = 1;
                    }
                    
                } while (dr.Read());
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
                dr.Close();
            }
            if (t == 1)
            {
                Form2 WelAdmin = new Form2();
                WelAdmin.Show();
                this.Hide();
            }
            else
            {
               MessageBox.Show("UserName and Password did not match! Try Again...");
                
            }
      }


[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 2-Jun-11 21:01pm
v4

Replace
SqlConnection conn = new SqlConnection("Data Source=DONUT-90CF4E6F4\SQLEXPRESS;Initial Catalog=Resort;Integrated Security=True");


with

SqlConnection conn = new SqlConnection("Data Source=DONUT-90CF4E6F4\\SQLEXPRESS;Initial Catalog=Resort;Integrated Security=True");


in your connection string.
 
Share this answer
 
v3
Comments
ritesh88sharma 3-Jun-11 3:19am    
Thank u sir
Uday P.Singh 3-Jun-11 3:21am    
welcome :)
Member 8656859 20-Feb-12 1:06am    
sir?
how to connect between the msaccess database to c# applications sir pls sample code send my mail id sir....sugumarsw@gmail.com thanking you........
Your connection string is wrong:
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Resort;Integrated Security=True");
Check it: In VS open the Server Explorer pane. Click on your Database name, and look at the "Connection String" property in the Properties Pane: If necessary, Right click, "Select All" and copy, then paste it into your app.

Later, you will probably want this in a settings file - not difficult, and Google can help you there!
 
Share this answer
 
Your connection string is probably incorrect.
Have a look at http://www.connectionstrings.com[^] to write the correct connection string.
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900