0

I am having a schedule print from a database and using a loop. I have done this in asp but I am changing to c# asp.net and having troubles.

First I print the schedule headers

time|court|court|court

based on the number of courts then it prints the games. Next ff the current records date is different the last date it will print the date over the entire table row. Then it checks to see if the time is of the current record is the same as the last if it is not it prints the time and then the game record if it is it just prints the game record.

My problem is I am declaring the TableRow in the time if statment so when I try to use it in another statment it is out of scope. If I take the tablerow outside of the if statement it doesn't print right.

Here is what I have.

for (int i = 0; i < GameCount; i++) 
    { 
        DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"]; 
        string ndate = currentdatetime.ToString("MM/dd/yyy"); 
        string ntime = currentdatetime.ToString("HH:mm"); 
        string nextdate = currentdatetime.ToString("MM/dd/yyy"); 
        if (i + 1 != GameCount) 
        { 
            DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"]; 
            nextdate = nextdatetime.ToString("MM/dd/yyy"); 
        } 
        string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString(); 
        string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString(); 
        //check to see if date is current 
        if (LastDate != ndate) 
        { 
            TableRow daterow = new TableRow(); 
            TableCell datecell = new TableCell(); 
            datecell.ColumnSpan = 7; 
            datecell.Controls.Add(new LiteralControl(ndate)); 
            daterow.Cells.Add(datecell); 
            ScheduleTable.Rows.Add(daterow); 
            LastDate = ndate; 
        } 
        //print the games 



        if (currentdatetime != LastDateTime)
        {
            TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
            timecell.Controls.Add(new LiteralControl(ntime));
            gamerow.Cells.Add(timecell);

            if (i + 1 != GameCount & ndate != nextdate)
            {
                ScheduleTable.Rows.Add(gamerow);
            }
        }//check to see if next game is part of the current row  
        else
        {
            TableCell gamecell = new TableCell();
            gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
            gamerow.Cells.Add(gamecell);
        } 


    } 

I can also post what I currently have in asp if that would help... you can go to www.swgc.ca/volleyball/2011/schedules.asp to see what I am trying to accomplish.

Thanks

1
  • try to shorten this code, for the sake of readability. Commented Apr 17, 2012 at 12:01

2 Answers 2

1

Change your last bit to:

        TableRow gamerow = new TableRow();

        if (currentdatetime != LastDateTime)
        {

            TableCell timecell = new TableCell();
            timecell.Controls.Add(new LiteralControl(ntime));
            gamerow.Cells.Add(timecell);


        }//check to see if next game is part of the current row  
        else
        {
            TableCell gamecell = new TableCell();
            gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
            gamerow.Cells.Add(gamecell);
        } 

        if (i + 1 != GameCount & ndate != nextdate)
         {
                ScheduleTable.Rows.Add(gamerow);
         }

And I'd strongly recommend looking at gridviews and repeater/list controls, as this is what they are for.

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

1 Comment

Gridview does, but the list/repeater controls allow you some flexibility and will be more maintainable as you go forwards.
0

The easiest solution would be to pull your instantiation outside of the for loop. Try this (untested code):

    TableRow gamerow = new TableRow();
    TableCell timecell = new TableCell();
    TableCell gamecell = new TableCell();
    TableRow daterow = new TableRow();
    TableCell datecell = new TableCell();
    for (int i = 0; i < GameCount; i++)
    {
        DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
        string ndate = currentdatetime.ToString("MM/dd/yyy");
        string ntime = currentdatetime.ToString("HH:mm");
        string nextdate = currentdatetime.ToString("MM/dd/yyy");
        if (i + 1 != GameCount)
        {
            DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
            nextdate = nextdatetime.ToString("MM/dd/yyy");
        }
        string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
        string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
        //check to see if date is current 
        if (LastDate != ndate)
        {
             daterow = new TableRow();
             datecell = new TableCell();
            datecell.ColumnSpan = 7;
            datecell.Controls.Add(new LiteralControl(ndate));
            daterow.Cells.Add(datecell);
            ScheduleTable.Rows.Add(daterow);
            LastDate = ndate;
        }
        //print the games 



        if (currentdatetime != LastDateTime)
        {
             gamerow = new TableRow();
             timecell = new TableCell();
            timecell.Controls.Add(new LiteralControl(ntime));
            gamerow.Cells.Add(timecell);

            if (i + 1 != GameCount & ndate != nextdate)
            {
                ScheduleTable.Rows.Add(gamerow);
            }
        }//check to see if next game is part of the current row  
        else
        {
             gamecell = new TableCell();
            gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
            gamerow.Cells.Add(gamecell);
        }

This is a non-optimized answer for your question. I feel like there is probably a better OO way to achieve your goal, but didn't want to answer a question you didn't ask.

4 Comments

URG!! I tried that and just assumed it hadn't worked... but when someone else said it I figured "Maybe there is something wrong with another part. Turns out the & in my other if statement should be and ||. However.... how would you consider optimizing this????
I'd start by getting the SQL to print out similar to how you want to display: SELECT [datetime], STUFF((SELECT ', ' + [teamA] + ' vs. ' +[teamB] FROM Schedule WHERE (datetime = Results.datetime) FOR XML PATH ('')),1,2,'') AS NameValues FROM Schedule Results GROUP BY datetime
And then I'd use these results to perform what you've already got, except, instead of looping through the games, you are looping through the times. You'd check for different dates, and add the date header. Then add the timerows/gamerows. The more I look at it I don't think your solution is unoptimized...it's just hard to read. That appears to be the nature of the 'Table' structure more so than the approach you are using.

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.