0

I want to return a DataTable from DLL file that is Created from ClassLibrary Project. So is it possible to return a DataTable from the DLL file which contain the code to return the DataTable? And also i want to use this in asp.net website. Plz give any suggestions...! EG:

public DataTable getData()
    {
       DataTable dt=new DataTable("Test");
       SqlCommand cmd=new SqlCommand();
       SqlDataAdapter da=new SqlDataAdapter();
       using(SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["EMP"].ConnectionString))
        {
            da.SelectCommand = con.CreateCommand();
            da.SelectCommand.CommandType = CommandType.Text;
            da.SelectCommand.CommandText = "Select *from EMP";
            da.SelectCommand.Connection = con;
            try
            {
                con.Open();
                da.Fill(dt);
                con.Close();
            }
            catch (Exception ec)
            {

            }
            finally
            {

                con.Close();
            }

        }
        return dt;
    }
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143

3 Answers3

1

Yes it is possible to return Datatable. What you are doing is correct. Make sure that you provide Connection string value of

ConfigurationManager.ConnectionStrings["EMP"].ConnectionString

some how in dll.

You can provide parameter of getData

public DataTable getData(string constr)
{
   //// Your code

   using(SqlConnection con=new SqlConnection(constr))
   { 
   ///// Your code
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • want to know why to pass connectionstring as parameter is it necessary? Can't i use "ConfigurationManager.ConnectionStrings["EMP"].ConnectionString" in DLL? i dont know so...but thanks for your suggestion..! – SHEKHAR SHETE May 04 '12 at 09:43
  • @SHEKHAR: Passing constr as parameter is just one of way you can do so. If you dont give it. How will dll know what is the value of ConfigurationManager.ConnectionStrings["EMP"].ConnectionString. Dll is library which you can use not just in asp project but in winforms or WPF apps. In winforms and WPF there is no ConfigurationManager. how will you use this function there without providing connection string. – Nikhil Agrawal May 04 '12 at 10:07
  • ok fine..! nw i got it nw i will pass the ConnectionString as Parameter..! Thanks Nikhil – SHEKHAR SHETE May 04 '12 at 10:59
1

Sure you can. You have to create a class in your ClassLibrary project, then reference the project or DLL from your ASP.NET project, create an instance of that class and call the method.

Your class in the ClassLibrary project could be like this:

namespace Data
{
    public class MyDataProvider {
        public DataTable GetData()
        {
            // Your method code here
        }
    }
}

You should use the code from your ASP.NET project like this:

// Create provider
Data.MyDataProvider provider = new Data.MyDataProvider();

// Get data
DataTable table = provider.GetData();

// Do stuff with your data...
German Latorre
  • 10,058
  • 14
  • 48
  • 59
0

Yes absolutely. You can return dataset /datatable from dll. infact this what other developers do for separating data layer from business layer.

I suggest you to read some stuff of three tier architecture/ mvc etc

Deepesh
  • 5,346
  • 6
  • 30
  • 45