Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Label/write.response question...

Options
  • 24-03-2009 11:54pm
    #1
    Closed Accounts Posts: 7,097 ✭✭✭


    I've a problem with a datareader I'm using to read a few rows from a DB....

    SqlDataReader DataReader;
    DataReader = cmdy.ExecuteReader();


    if (DataReader.HasRows)
    {

    while (DataReader.Read())
    {

    Label9.Text= string.Format(DataReader["eq_make"].ToString() + "<br>");
    //Response.Write(DataReader["eq_model"].ToString() + "<br>");

    }

    Where I've commented out the Response.Write code above, and use the Label9.Text code to try to display the data results on my aspx page, I only get one record from the DB. But if I comment out the Label9.Text line and use the Response.Write solution, I get all the data I'm looking for, but I want to use the Label9.Text way of doing it so I can easily format the data font, etc, using my Visual Web developer Express...

    Any ideas???


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Quite easy this one

    Label9.Text= string.Format(DataReader["eq_make"].ToString() + "<br>");

    You are assigning the value of Label9 each time, rather than appending

    So

    Label9.Text += string.Format(DataReader["eq_make"].ToString() + "<br>");

    to append the values... Also you dont have to use the br tag you can use the carriage return in the string object.

    Additionally, you could just bind this to a repeater to make life easier for yourself


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    This is great Ginger, thank's a mil for that. The only problem I have now is that I'm using this solution for a shopping a cart so when I add items to the cart now (I'm using the SessionID as the identifier in my database and this datareader is to search the table eveytime an item is added to the table and return all the rows with the current SessionID), so the datareader is handled in the protected void Button1_Click(object sender, EventArgs e) event.

    So when I ad an item now, the Label is appended as I want but on my aspx page, the results from the previous datareader event are staying on the page...

    So say I ad an apple to the cart, I get back a shopping cart saying:

    Apple

    Then if I add an orange, I get back a cart saying

    Apple
    Apple
    Orange

    And if I add a pear, I get back:

    Apple
    Apple
    Orange
    Apple
    Apple
    Orange
    Pear

    Is there a way I can kind of just return the current query to the aspx page, instead of having the previous datareader results as well???

    I've tried resetting the label by doing Label.Text = null within the while loop but that brings me back to my original problem which was just one row returned at a time???

    :confused::confused::confused:


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Oki doki

    What do you need to do before you enter your while loop? Clear the previous results :)

    So

    if (DataReader.HasRows)
    {
    Label9.Text = "";
    while (DataReader.Read())
    {
    Label9.Text += string.Format(DataReader["eq_make"].ToString() + "<br>");
    }


  • Closed Accounts Posts: 7,097 ✭✭✭Darragh29


    You are a wizard my friend! I was messing around with Label9.Text = null; but had it in the wrong place.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    No problemo


  • Advertisement
Advertisement