Click here to Skip to main content
15,913,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
       {
           con = new SqlConnection();
           string q = "Data Source=TOSHIBA-PC;Initial Catalog=ACCESSCONTROL;User ID=ABC;Password=hello;MultipleActiveResultSets=True";
           cmd = new SqlCommand (q,con);

           try
           {
               con.Open();
               MessageBox.Show("Connection opened.");
           }
           catch (SyntaxErrorException a)
           {
               MessageBox.Show("Error: " + a);
           }
           finally
           {
               con.Close();
               MessageBox.Show("Connection closed.");
           }

       }


Error : The ConnectionString property has not been initialized.
Posted

This will help you how to get connection to database line by line, even getting connection string.

http://www.homeandlearn.co.uk/csharp/csharp_s12p1.html[^]
 
Share this answer
 
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Oct-11 13:39pm    
Nice links! 5+

I've always found connectionstrings.com a very helpful website. Connection strings for all kinds of different DBMSs are found in abundance. :)
Monjurul Habib 13-Oct-11 14:49pm    
thanks..i also like connectionstrings.com
RaviRanjanKr 12-Oct-11 17:34pm    
My 5+ for links.
Monjurul Habib 13-Oct-11 14:49pm    
thanks
Rubaba 3-Dec-11 11:49am    
5* for link
Please look here: http://msdn.microsoft.com/en-us/library/aa326257(v=VS.71).aspx[^]. You'll see what you were doing wrong.

C#
private void button1_Click(object sender, EventArgs e)
{
    String connectionString = "Data Source=TOSHIBA-PC;Initial Catalog=ACCESSCONTROL;User ID=ABC;Password=hello;MultipleActiveResultSets=True";
    con = new SqlConnection(connectionString); // You'll have to initialize the connection with the connectionString

    cmd = new SqlCommand(con);

    try
    {
        con.Open();
        MessageBox.Show("Connection opened.");
    }
    catch (SyntaxErrorException a)
    {
        MessageBox.Show("Error: " + a);
    }
    finally
    {
        con.Close();
        MessageBox.Show("Connection closed.");
    }

}

Best Regards,

—MRB
 
Share this answer
 
v4
Comments
Sander Rossel 12-Oct-11 13:29pm    
The quotes around your connectionString got all messed up html like... Fixed them for you.
Manfred Rudolf Bihy 12-Oct-11 13:48pm    
Actually I just copied the original code from OP using the "Copy code" link.

Thank you very much, nevertheless!
Monjurul Habib 13-Oct-11 14:51pm    
good call 5++ and link 5++..but i can give only one ;)

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