Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:22:43+00:00 2026-05-29T06:22:43+00:00

When i debug my application, it gives an error that SelectCommand.Connection Property is not

  • 0

When i debug my application, it gives an error that SelectCommand.Connection Property is not been initialized. i dont know what am i doing here wrong :s. I actually want to add a filter over my search on the textchanged event of a textbox.

public class ConnectionClass
{
    static SqlConnection cn;

    public static SqlConnection Connection()
    {
        string myConnection = ConfigurationManager.ConnectionStrings["_uniManagementConnectionString1"].ConnectionString;
        if (cn != null)
        {
            cn = new SqlConnection(myConnection);
            cn.Open();
        }
        return cn;
    }

}    

public class ClassDataManagement 
{ 
    SqlConnection cn = ConnectionClass.Connection();

    public DataTable GetData(string SQL)
    {
         SqlCommand cmd = new SqlCommand(SQL, cn);
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         DataTable dt = new DataTable();
         da.Fill(dt);
         return dt;
    } 
}

protected void TextBoxFilterText_TextChanged(object sender, EventArgs e)
{
   ClassDataManagement dm = new ClassDataManagement();
   string query = "Select CourseCode from _Courses where coursecode like'%" + TextBoxFilterText.Text.TrimEnd() + "%'";
   dm.GetData(query);
   GridViewCourses.DataBind();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:22:45+00:00Added an answer on May 29, 2026 at 6:22 am

    That’s because your cn variable is null, and not getting initialized. Yet another example why it’s a bad idea to initialize and open database connections in a static method.

    Try this:

    public class ClassDataManagement 
    { 
       public DataTable GetData(string SQL)
        {
              string YourConnectionString = ConfigurationManager.ConnectionStrings["_uniManagementConnectionString1"].ConnectionString;     
              DataTable dt = new DataTable();             
    
              using (SqlConnection cn = new SqlConnection(YourConnectionString))
              using (SqlCommand cmd = new SqlCommand(SQL, cn))
              using (SqlDataAdapter da = new SqlDataAdapter(cmd))
              {
                  da.Fill(dt);
              }
              return dt;
        } 
    }
    

    With the SqlDataAdapter class, you don’t need to explicitly call SqlConnection.Open(). The SqlDataAdapter.Fill() method handles all of the connection opening (and closing).

    MSDN Reference on SqlDataAdapter.Fill()

    As per the above reference, quoted:

    The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to debug an application (under PostgreSQL) and came across the following error:
I'm using VS2008 to debug an application that starts a new process. I believe
I am trying to debug a web application through IIS that has a virtual
I just deleted my Debug.keystore because an application displayed an error on the project
I'm trying to debug and step through an Android application that segfaults. I've tried
My Project1 is here \Project1\Bin\Debug\application.exe and my dlls are in \Project1\Debug\3rdparty.dll How to get
I have an ASP.NET MVC application that I'm working on. I've been developing it
Related question: Running my application on another machine gives me an error This is
I am trying to write a Windows application that gives me back all the
How do I debug errors that have no error message? When loading a PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.