Put data from database into a ASP table

Hi,
tried to Put data from a database into a asp:table.
(database stuff works, there are more than one datasets in my database-table)

I tried this:

   TableCell cell = new TableCell();
   TableRow row = new TableRow();
   string m_id = null, mt_name0 = null, mt_start0 = null, mt_end0 = null, mt_comment0 = null, mt_user0 = null;

  while(GET_Server_reader.Read())
  {
   m_id=GET_Server_reader["m_id"].ToString();
   mt_name0=GET_Server_reader["MName"].ToString();
   mt_start0=GET_Server_reader["Anfangszeit"].ToString();
   mt_end0=GET_Server_reader["Endzeit"].ToString();
   mt_comment0=GET_Server_reader["Kommentar"].ToString();
   mt_user0=GET_Server_reader["user"].ToString();

   cell.Text=mt_name0;
   row.Cells.Add(cell);

   cell.Text=mt_start0;
   row.Cells.Add(cell);

   cell.Text=mt_end0;
   row.Cells.Add(cell);

   cell.Text=mt_comment0;
   row.Cells.Add(cell);

   cell.Text=mt_user0;
   row.Cells.Add(cell);
 
   Table1.Rows.Add(row);
   row.Dispose();
  }
  Table1.DataBind();


Table1 is defined in the aspx-file like this:
       <asp:Table runat="server" ID="Table1" BackColor="#eeeeee">
        <asp:TableRow>
         <asp:TableCell><center>Server</center></asp:TableCell>
         <asp:TableCell><center>Maintenance</center></asp:TableCell>
         <asp:TableCell><center>Start</center></asp:TableCell>
         <asp:TableCell><center>End</center></asp:TableCell>
         <asp:TableCell><center>User</center></asp:TableCell>
         <asp:TableCell><center>Comment</center></asp:TableCell>
        </asp:TableRow>
       </asp:Table>

All I get is the first cell in the second row of the table (the first cell of the data-row).

Q1: Can anyone help me finding out how to fill the table correctly?
Q2: Can anyone show me how to add other elements (like CheckBoxList) to a table cell?
LVL 1
develcAsked:
Who is Participating?
 
Pratima PharandeCommented:
Try this


   
   string m_id = null, mt_name0 = null, mt_start0 = null, mt_end0 = null, mt_comment0 = null, mt_user0 = null;

  while(GET_Server_reader.Read())
  {
   m_id=GET_Server_reader["m_id"].ToString();
   mt_name0=GET_Server_reader["MName"].ToString();
   mt_start0=GET_Server_reader["Anfangszeit"].ToString();
   mt_end0=GET_Server_reader["Endzeit"].ToString();
   mt_comment0=GET_Server_reader["Kommentar"].ToString();
   mt_user0=GET_Server_reader["user"].ToString();
TableRow row = new TableRow();
   TableCell cell = new TableCell();
   cell.Text=mt_name0;
   row.Cells.Add(cell);

TableCell cell1 = new TableCell();
   cell1.Text=mt_start0;
   row.Cells.Add(cell1);

TableCell cell2 = new TableCell();
   cell2.Text=mt_end0;
   row.Cells.Add(cell2);

TableCell cell3 = new TableCell();
   cell3.Text=mt_comment0;
   row.Cells.Add(cell3);
TableCell cell4 = new TableCell();
   cell4.Text=mt_user0;
   row.Cells.Add(cell4);
 
   Table1.Rows.Add(row);
   row.Dispose();
  }
  Table1.DataBind();
0
 
develcAuthor Commented:
That was a good point to start at. I optimized your solution and added a checkbox in the first cell of each row. I gave each of them an ID:

TableCell cell;
  TableRow row;
  ListItem item;
  c=0;

  while (GET_Server_reader.Read())
  {
   show_text = GET_Server_reader["server"].ToString();
   m_id=GET_Server_reader["m_id"].ToString();
   mt_name0=GET_Server_reader["MName"].ToString();
   mt_start0=GET_Server_reader["Anfangszeit"].ToString();
   mt_end0=GET_Server_reader["Endzeit"].ToString();
   mt_comment0=GET_Server_reader["Kommentar"].ToString();
   mt_user0=GET_Server_reader["user"].ToString();

   //Zellen Befüllen:
   row = new TableRow();
   item = new ListItem("", m_id);
   ServerList.Items.Add(item);

   for (int count = 0; count <= 6; count++)
   {
    cell = new TableCell();

    if (count == 0)
    {
     checkbox = new CheckBox();
     checkbox.Text = "";
     checkbox.ToolTip = "Select an item";
     checkbox.ID = m_id;
     cell.Controls.Add(checkbox);
    }

    if (count == 1) { cell.Text = mt_name0; }
    if (count == 2) { cell.Text = mt_name0; }
    if (count == 3) { cell.Text = mt_start0; }
    if(count==4){cell.Text = mt_end0;}
    if(count==5){cell.Text = mt_user0;}
    if(count==6){cell.Text = mt_comment0;}

    row.Cells.Add(cell);
    cell.Dispose();
   }
   row.BorderColor = System.Drawing.Color.Gold;
   row.BorderWidth = 0;
   if (Convert.ToBoolean(GET_Server_reader["flagu"]))
   {
    row.ForeColor = System.Drawing.Color.FromArgb(30, 100, 5);
    aktiv++;
   }
   else
   {
    inaktiv++;
   }
   Table1.Rows.Add(row);
   row.Dispose();
   c++;
  }
  Table1.DataBind();


I need to find out if the box is checked or not.
Therfore I tried this:
 
  ArrayList DelList = new ArrayList();
  for (i = 0; i <= ServerList.Items.Count - 1; i++)
  {
   m_id = ServerList.Items[i].Value.ToString();
   checkbox.FindControl(m_id);
   if (checkbox.Checked)
   {
    item = new ListItem(m_id);
    DelList.Add(item);
   }
  }

The DelList is empty (I checked some boxes while testing). Where is the error?
0
 
Pratima PharandeCommented:
 

 ArrayList DelList = new ArrayList();
  for (i = 0; i <= ServerList.Items.Count - 1; i++)
  {
   m_id = ServerList.Items[i].Value.ToString();
checkbox = new CheckBox();  
 checkbox = me.FindControl(m_id);
   if (checkbox.Checked)
   {
    item = new ListItem(m_id);
    DelList.Add(item);
   }
  }
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Pratima PharandeCommented:
are you able to fill the table correctly now ?

m_id  is your checkbox name right ??
then you need to find it on your page and then check
0
 
develcAuthor Commented:
m_id is a string containing an unique ID read from the database.
The ID of a checkbox is the ID from the database.

I found the problem: the ServerList, generated before pressing the Button on the Page, becomes empty.

Could you give me a short example how a sessions work (just with an int variable or something easy), so that the ServerList stays filled?
0
 
Pratima PharandeCommented:
You are storing the serverlise to session ??

Session['name'] = value

in this way you can save the value to the server
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.