0

I have a Dataset from the database which i want to display. My aim is to display a month's data on an aspx page using VS 2005 with 5 days per each row. I have written the code like this but i am confused with the i and j. This code displays nothing.

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
    Table table = new Table();
    table.ID = "Table1";

    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    TextBox tb1 = new TextBox();
    TextBox tb2 = new TextBox();

    // I am not sure what i and j should be here to display 5 per each row..
    for (int i = 0; i < 5; i++)
    {
        if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)
        {

             tb1.ID = "txtDateRow" + x + "Col" + j;
             tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
             tb2.ID = "txtDetails" + x + "Col" + j;
             tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);
             table.Rows.Add(row);
        }
    }
    Panel1.Controls.Add(table);
}

If someone could help me solve this, i really appreciate it. Thanks a lot.

10
  • 1
    Where is "j" declared/initialized? Commented Jan 30, 2012 at 20:16
  • Does it display "nothing" or does it display a table element with no rows? The two are very different outcomes. Commented Jan 30, 2012 at 20:17
  • Why is table being added to both Page.Form.Controls and Panel1.Controls? Commented Jan 30, 2012 at 20:20
  • I just edited what j is. Sorry for the confusion! Commented Jan 30, 2012 at 20:22
  • @Ram: The edited version is invalid code and isn't going to compile. Can you share the actual code that you're using? Commented Jan 30, 2012 at 20:25

3 Answers 3

1

row.Controls.Add(cell) is missing. Bcoz of this textbox controls are not added to the table and you are not able to see anything. Add this line and it will help you see. Later you can think about i & j.

Sign up to request clarification or add additional context in comments.

Comments

0

You have a loop with a counter, namely, i but you test for j==0 what is j? Is j initalized somewhere? Is j even declared? If not, are you sure you don't want to test for i == 0 ?

That is the reason you are not getting any results in the for loop. The code:

 tb1.ID = "txtDateRow" + x + "Col" + j;
             tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
             tb2.ID = "txtDetails" + x + "Col" + j;
             tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);
             table.Rows.Add(row);

Is inside a condition:

if (j == 0)

The question is does this condition ever happen? If so you need to post more code. Debug your code and set breakpoints and watch how it runs. Per your edit you posted:

if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)

This is not even valid, I think you need to pick up a book on C# syntax before you tackle this problem.

Comments

0

I'm not sure what 'x' is supposed to be in your code. I think the general structure should be something like this but the code below will add the same thing for each column:

if (ds != null && ds.Tables[0].Rows.Count > 0)
{
    Table table = new Table();
    table.ID = "Table1";

    // j is the row index
    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
    {
        TableRow row = new TableRow();

        // i is the column index
        for (int i = 0; i < 5; i++)
        {
             TableCell cell = new TableCell();
             TextBox tb1 = new TextBox();
             TextBox tb2 = new TextBox();

             tb1.ID = "txtDateRow" + j + "Col" + i;
             tb1.Text = ds.Tables[0].Rows[j]["Date"].ToString();
             tb2.ID = "txtDetails" + j + "Col" + i;
             tb2.Text = ds.Tables[0].Rows[j]["AmountSold"].ToString();
             cell.Controls.Add(tb1);
             cell.Controls.Add(tb2);

             row.Cells.Add(cell);
        }

        table.Rows.Add(row);
    }
    Panel1.Controls.Add(table);
}

2 Comments

just being nitpicky, most people like to use i then j. Since alphabetically i does come before j. So i should handle rows while j handles columns. But again this is just preferance.
@JonH, I agree, I would normally do the same, I was just being a bit lazy here - I took the original code and moved it around a bit so I kept the same variables. Might also help make it clear to the OP that he had the loops the wrong way round. In fact, in this situation I would probably call the loop variables row and column anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.