[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 425
  • Last Modified:

C# Database Connection

Hi All,

I'm trying to use a windows for application with a database file and having some dificulty with the connection.  Here is the code I've written:

if (rdoIP.Checked == true)
                {
                    //New connection using string in program settings.

                    using (SqlConnection con = new SqlConnection(PhoneApplication.Properties.Settings.Default.PhoneAppConnectionString))
                        con.Open();
                    using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"))
                    using (SqlDataReader reader = command.ExecuteReader())

                    //Set MAC Address
                    strMac = "0004f004f004";

                    //Set Circuit
                    strCircuit = "16,12,12";
                }

Open in new window


However, when I run the method I get "ExecuteReader: Connection property has not been initialized." for the line of code: using (SqlDataReader reader = command.ExecuteReader())

Am I missing something obvious?  Thanks for any help!
0
ImaCircularSaw
Asked:
ImaCircularSaw
  • 3
  • 3
1 Solution
 
DhaestCommented:
Add the connection to your command :) Try this

using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"), con)
                                    }
0
 
ImaCircularSawAuthor Commented:
Thanks,

I've changed the line:

using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"))

To what you suggested:

using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"), con)

And I get an error "must provide an initializer in a fixed or using statement".  My method, with your suggestion reads as follows:

if (rdoIP.Checked == true)
                {
                    //New connection using string in program settings.

                    using (SqlConnection con = new SqlConnection(PhoneApplication.Properties.Settings.Default.PhoneAppConnectionString))
                        con.Open();
                    
                    using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"), con)
                    using (SqlDataReader reader = command.ExecuteReader())

                    //Set MAC Address        
                    strMac = "0004f004f004";

                    //Set Circuit
                    strCircuit = "16,12,12";
                }

Open in new window

0
 
DhaestCommented:
Your connection is already closed again, you need to add some brackets ....
if (rdoIP.Checked == true)
                {
                    //New connection using string in program settings.

using (SqlConnection con = new SqlConnection (PhoneApplication.Properties.Settings.Default.PhoneAppConnectionString))
  {
    con.Open();
                    
    using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"), con)
    {
      using (SqlDataReader reader = command.ExecuteReader())
      {
                    //Set MAC Address        
                    strMac = "0004f004f004";

                    //Set Circuit
                    strCircuit = "16,12,12";
      }
    }
  }
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ImaCircularSawAuthor Commented:
Thanks,

Dosn't seem to be working though.  I copied in your suggestion (replacing the entire method) and the line :

using (SqlCommand command = new SqlCommand("SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)"), con)

Gives error "A local variable named 'con' cannot be declared in this scope because it would give a different meaning to 'con', which is already used in a 'parent or current' scope to denote something else" and "You must provide an initializer in a fixed or using statement declaration".

Thanks,
0
 
DhaestCommented:
string sqlConnection = PhoneApplication.Properties.Settings.Default.PhoneAppConnectionString;
string sqlQuery = "SELECT cir_circuit, cir_ID FROM Circuits WHERE (cir_circuit LIKE '%16%') AND (cir_assigned = 0)";

if (rdoIP.Checked == true)
{
                    //New connection using string in program settings.
       using (SqlConnection con = new SqlConnection(sqlConnection)
      {                
              using (SqlCommand command = new SqlCommand(sqlQuery, con)
              {
                    con.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                   {
                        //Set MAC Address        
                       strMac = "0004f004f004";

                       //Set Circuit
                       strCircuit = "16,12,12";
                   }
               }
         }
}
0
 
ImaCircularSawAuthor Commented:
Thanks, that works!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now