Question

I am trying to figure out how to pass an object that i have created in the dll file to the code behind file in my web application.

Here is the class I made:

public class BugReports
{
    public object userRoleDropDown()
    {
        SqlConnection conn;
        SqlCommand userRoleComm;
        SqlDataReader reader;
        string connectionSrting = ConfigurationManager.ConnectionStrings["BugReports"].ConnectionString;
        conn = new SqlConnection(connectionSrting);
        userRoleComm = new SqlCommand(
            "SELECT UserRoleID, UserRoleName FROM userRoles", conn);

        try
        {
            conn.Open();
            reader = userRoleComm.ExecuteReader();
            /*
            addUserRollDropDownList.DataSource = reader;
            addUserRollDropDownList.DataValueField = "UserRoleID";
            addUserRollDropDownList.DataTextField = "UserRoleName";
            addUserRollDropDownList.DataBind();*/

            reader.Close();
        }
        finally
        {
            conn.Close();
        }

        return reader;
    }
}

I then want to use the reader in my cs file but where do I start? I thought a simple;

BugReports reader = new BugReports();

would work but nothing comes up.

Was it helpful?

Solution

Assuming you have everything wired up correctly with your project reference to the dll and a using statement in your code file.

BugReports reader = new BugReports();

That line only gets an instance of your BugReports class, in order to make it do some work you need to call your method.

reader.userRoleDropDown();

I'm not sure why you are returning the SqlDataReader reader that you've already closed, it is no longer any use. Also you are selecting data by calling reader = userRoleComm.ExecuteReader(); but all the work is commented out, not sure if that is intentional.

Edit:

You may be better off using a SQLDataAdapter since your UI controls won't be visible to your class, and you can't access the data in the SQLDataReader after it's closed.

public DataSet userRoleDropDown()
{
    string connectionSrting = ConfigurationManager.ConnectionStrings["BugReports"].ConnectionString;        
    string queryString = "SELECT UserRoleID, UserRoleName FROM userRoles";

   using (SqlConnection connection = new SqlConnection(connectionSrting))
   {
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand( queryString, connection);
      adapter.Fill(dataset);
      return dataset;
   }
} 

Then you can do what ever you like with the selected data from your application.

More info on the important classes used here: SqlDataAdapter DataSet

OTHER TIPS

If you have built in an assembly, you should go to the ASP.Net Application and then add a reference to the assembly [dll file] and then add a using statement like the one below

namespace CustomLibrary;
{
    public class BugReports
    {
    }    
}

In the asp.net aspx.cs file,

using CustomLibrary;

BugReports reader = new BugReports();

Post here your understandings or any other updates on the implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top