0

I have a DataClassLibrary I use for the connectionString and then I also have the C# code behind the ASP page. I want to be able to use While(reader.read()) in my ASP page so that I can get the multiple values the reader returns. How can I go about implementing this? I have provided the code below for my Data Class and the ASP page.

Data Class:

reader = DBHelper.executeQuery(dbConn, sqlString.ToString(), parameters);
        if (reader != null)
        {
            if (reader.Read())
            {
                OrderID = (int)Convert.ToInt32(reader["OrderID"]);
                CaseNum6 = (int)Convert.ToInt32(reader["CaseNum6"]);
                CaseNum9 = (int)Convert.ToInt32(reader["CaseNum9"]);
                Group = (int)Convert.ToInt32(reader["Group"]);
                Completed = (bool)reader["Completed"];
            }
            else
                throw new Exception("No record returned");
            reader.Close();
            reader.Dispose();
            dbConn.Close();
            dbConn.Dispose();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (dbConn != null)
        {
            try { dbConn.Close(); dbConn.Dispose(); }
            catch { }
         }
        if (reader != null)
        {
            try { reader.Close(); reader.Dispose(); }
            catch { }
        }
    }

ASP page to implement on:

LineAData NewDataA = new LineAData();
LineAData NewDataB = new LineAData();
string connString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{

    NewDataA.load(1,3);
    NewDataB.load(2,3);

    L1.Text = NewDataA.CaseNum6.ToString();
    L2.Text = NewDataA.CaseNum9.ToString();
    L7.Text = NewDataA.CaseNum6.ToString();
    L8.Text = NewDataA.CaseNum9.ToString();

    L4.Text = NewDataB.CaseNum6.ToString();
    L5.Text = NewDataB.CaseNum9.ToString();
    L10.Text = NewDataB.CaseNum6.ToString();
    L11.Text = NewDataB.CaseNum9.ToString();
}
Kpt.Khaos
  • 673
  • 3
  • 14
  • 37
  • 1
    `DBHelper` in ASP.NET is [rarely a good idea](http://stackoverflow.com/a/9707060/284240). Is the connection static? – Tim Schmelter Dec 13 '13 at 14:14
  • Also, you might want to look into the using statement http://msdn.microsoft.com/en-us/library/yh598w02.aspx and why "throw ex" throws away your stack trace. – RQDQ Dec 13 '13 at 14:16
  • @TimSchmelter as far as I know yes. I used what a colleague used at one time. If there is a simpler or more effective way that would be awesome. – Kpt.Khaos Dec 13 '13 at 14:16
  • @Kpt.Khaos: a simple and efficient way is to create the connection/command via `using`-statement where you use it. In that way it is ensured that it gets closed immediately after you are finished with it. – Tim Schmelter Dec 13 '13 at 14:21
  • You should get rid of the entire try/catch block, including the `finally`. You should also be implementing `using` blocks for your reader, connection, and command. – John Saunders Dec 14 '13 at 05:57

2 Answers2

0

I did not understand your question well, but as far as i understood. I think you require the multiple values from the reader on your asp page. If this is the requirement then what you can do is, just create one class which will contain all the column name which will be there in the reader.

The method which is there in your DataClassLibrary will return the List.

The below example will help you:

public class User
    {
    public long nUser { get; set; }

    public string cUserID { get; set; }

    public string cName { get; set; }

    public string cPassword { get; set; }
}

 public class cUser
 {
public List<User> GetUsers()
    {
        try
        {
            SqlConnection connection = new SqlConnection(ConnectionString);

            command = new SqlCommand(connection);
            command.CommandType = CommandType.Text;

            List<User> tuserList = new List<User>();

            User tuser = null;

            connection.Open();

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                tuser = new User();

                tuser.nUser = long.Parse(reader["nUser"].ToString());
                tuser.cUserID = reader["cUserID"].ToString();
                tuser.cName = reader["cName"].ToString();
                tuser.cPassword = reader["cPassword"].ToString();

                tuserList.Add(tuser);
            }

            return tuserList;

        }

        catch (SqlException ex)
        {
            return null;
        }

        catch (Exception ex)
        {
            return null;
        }

        finally
        {
            if (reader != null)
            {
                reader.Close();
            }

            if (connection != null)
            {
                connection.Close();
            }
        }

    }
  }

On ASPX page:

just create the object of the class in which this method is there. and get it. Suppose it is there in the cUser class.

    cUser a = new cUser();

    var list = a.GetUsers();

I hope it will help you.. :)

Hitesh
  • 3,508
  • 1
  • 18
  • 24
  • Yes this is exactly what I'm looking for! Pardon my inexperience but can you explain the last part with the ASPX page? I'm still lost how once you have the records like you did above to get the individual one s from the reader in the ASPX page – Kpt.Khaos Dec 13 '13 at 14:28
  • i have done some edit, you just put the above code in the data class library and the below code you just put on your aspx page and add the reference of data class library in your aspx project. That's it, you are done.. ;) Let me know if you need more help. and if the answer is helpful then mark this as correct answer, it will help others too. – Hitesh Dec 13 '13 at 14:33
  • Are you using a way to get rid of DBHelper? – Kpt.Khaos Dec 13 '13 at 15:30
  • Is the top section for dataclass> or ASPX.CS – Kpt.Khaos Dec 13 '13 at 15:44
  • No, i am not getting rid of DBHelper.. I have just provided a simple way to understand this method of getting multiple set of result. If you need anything else, feel free to ask.. :) – Hitesh Dec 14 '13 at 07:23
0

With some tweaks to your DBHelper, you could return the reader from your data (access) class to your web page code-behind, however, you will need to leave the connection open when returning it to the web page code behind, and instead, execute the reader with the option CommandBehavior.CloseConnection which will close the connection when the page closes the reader.

Dal

public IDataReader ExecuteReaderMethod()
{
   // Set up conn and command 
   // ...
   return command.ExecuteReader(CommandBehavior.CloseConnection);
}

ASP Code Behind

using (var reader = MyDal.ExecuteReaderMethod())
{
   while (reader.Read())
   {
       someUIControl.Text = reader.GetString("OrderID");
   }
}

To be honest though, since it seems you need to render the whole page anyway, there probably isn't much point in getting the lazy evaluation benefit of the reader. You might be better off just converting the result to a DataSet or a List<> of custom entities in your DAL and returning this to your web page, and then release the precious DB resources early.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
StuartLC
  • 104,537
  • 17
  • 209
  • 285