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 6784301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:58:27+00:00 2026-05-26T16:58:27+00:00

I’m learning about classes in C# and am trying to build a DataAccessClass. I

  • 0

I’m learning about classes in C# and am trying to build a “DataAccessClass”. I have a working “OpenSqlConnection” void and am using it as a guide to build a “OpenSqlDatareader” void. I am attempting to consolidate all my database calls in this class. My boggle is how to pass a DataTable from the class to the aspx page to bind it with a ListView. I have been reading and trying for a day to make this work and cannot figure out what I am doing wrong…

Here’s the Page_Load:

// --------------------------------------------------------------------------------
// Populate the Customers page.
// --------------------------------------------------------------------------------


// Define the query
string sqlQuery = " SELECT CustomerID, LastName + ', ' + FirstName AS CustomerName, Email, City, State, Phone"
                            + " FROM Customer"
                            + " ORDER BY LastName, FirstName";

string strErrorMessage = "";

if (DataAccessClass.OpenSqlConnection(out strErrorMessage) == false)
{
    string strErrorType = "Database Connection Error:";
    SendErrorMessageToClient(strErrorType, strErrorMessage);
}

else if (DataAccessClass.OpenSqlDatareader(sqlQuery, out dt, out strErrorMessage) == false)
{
    string strErrorType = "Datareader Error:";
    SendErrorMessageToClient(strErrorType, strErrorMessage);
}

else

{
    // Bind the Listview
    lvCustomers.DataSource = dt;
    lvCustomers.DataBind();
    dt.Dispose();
}

And here’s the class:

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

/// <summary>
/// Summary description for DataAccessClass
/// </summary>
/// 
public class DataAccessClass
{
    public DataAccessClass()
{
    //
    // TODO: Add constructor logic here
    //
}



// -----------------------------------------------------------------------------------------
// Name: OpenSqlConnection
// Abstract: Open a connection to a SQL Server
// -----------------------------------------------------------------------------------------
public static bool OpenSqlConnection(out string strErrorMessage)
{

    SqlConnection theConnection = new SqlConnection();
    bool blnResult = false;
    string strConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
    strErrorMessage = "";

    if (theConnection == null)
        theConnection = new SqlConnection();

    try
    {
        switch (theConnection.State)
        {
            case ConnectionState.Broken:
                theConnection.Close();
                theConnection.ConnectionString = strConnectionString;
                theConnection.Open();
                blnResult = true;
                break;

            case ConnectionState.Closed:
                theConnection.ConnectionString = strConnectionString;
                theConnection.Open();
                blnResult = true;
                break;

            case ConnectionState.Open:
                blnResult = true;
                break;

            default:
                strErrorMessage = "Connection state is " + theConnection.State.ToString();
                break;
        }
    }
    catch (Exception excError)
    {
        strErrorMessage = excError.Message;
    }

    return blnResult;
}

// -----------------------------------------------------------------------------------------
// Name: OpenSqlDataReader
// Abstract: Open a SQL DataReader
// -----------------------------------------------------------------------------------------
public static bool OpenSqlDatareader(string sqlQuery, out dt, out string strErrorMessage)
{

    SqlConnection theConnection = new SqlConnection();

    bool blnResult = false;
    string strConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
    strErrorMessage = "";

    if (theConnection == null)
        theConnection = new SqlConnection();

    try
    {
        // Declare a SQL Adapter
        SqlDataAdapter da = new SqlDataAdapter(sqlQuery, theConnection);

        // Declare a DataTable
        DataTable dt = new DataTable();

        // Populate the DataTable
        da.Fill(dt);

        // Clean up.
        dt.Dispose();
        da.Dispose();
        theConnection.Close();
    }
    catch (Exception excError)
    {
        strErrorMessage = excError.Message;
    }

    return blnResult;
  }
}

The error message is: “The type or namespace name ‘dt’ could not be found (are you missing a using directive or an assembly reference?)”
Any suggestions?

  • 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-26T16:58:27+00:00Added an answer on May 26, 2026 at 4:58 pm

    before calling OpenSqlDatareader you need to declare dt

    DataTable dt;// no need to assign 
    
    ....
    else if (DataAccessClass.OpenSqlDatareader(sqlQuery, out dt, out strErrorMessage) == false)
    {
        string strErrorType = "Datareader Error:";
        SendErrorMessageToClient(strErrorType, strErrorMessage);
    }
    ...
    

    change your signature of OpenSqlDatareader with out DataTable dt

    public static bool OpenSqlDatareader(string sqlQuery, out DataTable dt, out string strErrorMessage)
    

    inside the method

    // create new  DataTable
    dt = new DataTable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.