0

I have a sql query that I want to use to get results from a database. When I try to open a sqlconnection, my code won't compile and I get an error asking if i am missing a using directive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;


namespace HackOffice.Superadmin
{
    public partial class FoundUsBreakdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            grabData();
        }
        private void grabData()
        {
            string chartDataQuery = "select AccountType, COUNT(*) Entries from HackOffice.dbo.OnlineSignups group by AccountType";
            using (sqlconnection connection = new sqlconnection(connectionString));
        }
    }
}

I'm confused because I have all the same using directives on other pages and I am not getting the same error on those pages.

3
  • 3
    It's SqlConnection, not sqlconnection (case sensitive). Commented Jul 9, 2015 at 17:51
  • wow. you can tell im new. thanks! If you put as answer i will mark it. Commented Jul 9, 2015 at 17:54
  • Use Visual studio. It really helps in these kinds of issues Commented Jul 9, 2015 at 18:03

1 Answer 1

1

Class names are case sensitive in C#. You need SqlConnection, not sqlconnection.

Also, you're creating an empty using statement which will dispose the resource immediately. When you open a using block, you'll want to put all the code that uses the IDisposable resource inside the block:

using (SqlConnection connection = new SqlConnection(connectionString))
{
   // Code that uses SqlConnection goes inside the block.
}
Sign up to request clarification or add additional context in comments.

1 Comment

solid answer. will accept when i can. also i did not realize that. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.