Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

sqlConnection cnn and sqlCommand cmd objects are instantiated but Debug mode says otherwise

Posted on 2011-05-03
2
Medium Priority
?
512 Views
Last Modified: 2013-12-16
I've started writing some code using the "Using" block to dispose of the objects being used within. I checked my SqlConnectionString and that checks out fine.
I've pasted some simple code below and I'm clueless as to why I'm receiving the following errors:
1. Getting a NullReferenceException was unhandled and 2. "Object reference not set to an instance of an object".
As you can see the SqlConnection "cnn " object is being instantiated as well as the SqlCommand "cmd" object too. I'm getting sguiggly lines under cnn and cmd and am stumped.
I'm using VS 2010 Ultimate. I attemped to add code to declare cnn and cmd var's to no avail.

Thanks for your help Experts,
Wallace
private void ViewProducts_Load(object sender, EventArgs e)
    {
        FillCategories();
    }

    private void FillCategories()
    {
        using (SqlConnection cnn = 
            new SqlConnection(
                Properties.Settings.Default.SqlConnectionString));
        {
            using (SqlCommand cmd = new SqlCommand(
                "SELECT CategoryID, CategoryName " +
                "FROM Categories " +
                "ORDER BY CategoryName", cnn)) ;
            {
                cnn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    //Insert Loop here
                }
            }
        }
    }

Open in new window

0
Comment
Question by:wally_davis
2 Comments
 
LVL 11

Accepted Solution

by:
brutaldev earned 2000 total points
ID: 35517502
Remove the semicolon's from the end of your two using statements. They are defining the end of the statement so missing the inside of the braces.
 
private void ViewProducts_Load(object sender, EventArgs e)
    {
        FillCategories();
    }

    private void FillCategories()
    {
        using (SqlConnection cnn = 
            new SqlConnection(
                Properties.Settings.Default.SqlConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(
                "SELECT CategoryID, CategoryName " +
                "FROM Categories " +
                "ORDER BY CategoryName", cnn))
            {
                cnn.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    //Insert Loop here
                }
            }
        }
    }

Open in new window

0
 

Author Closing Comment

by:wally_davis
ID: 35517568
Oh for the love of pete...I can't believe I did that.
Good catch brutaldev!!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question