Solved

C# Class to return datatable

Posted on 2014-03-27
1
513 Views
Last Modified: 2014-05-20
Hi Experts

I'm new to C# I'm writing some code to return a datatable when a user passes a sql statement to a class, it's not working though as the class want's a sting returned.


 class SQL_Array
    {

        string SQL;
        DataTable results;

         public SQL_Array()
        {

        }

        public SQL_Array(string strSQL,DataTable result )
        {
            results = result;
            SQL = strSQL;
        }

        public string SQL_Results()
        {
           // string []SQL_Results;

            string SQL = "Select * from dmt.dbo.DIT_tblBlah where txtstaffid is not null";

            SqlConnection cnnDIT = new SqlConnection("Blah");
            cnnDIT.Open();

      

            SqlDataAdapter adap = new SqlDataAdapter(SQL, cnnDIT);

          // create an empty DataTable to hold the results
            DataTable results = new DataTable();

            // populate the DataTable. Note: the SqlDataAdapter will take care of opening/closing the connection for you
            adap.Fill(results);

            cnnDIT.Close();

            return results;

Open in new window

0
Comment
Question by:MrDavidThorn
1 Comment
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39959513
Change the return type of the method:

public DataTable SQL_Results()
...

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
Convert string to date 3 40
Round a string to two digits 12 24
getting id from database 5 24
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now