?
Solved

Put data from database into a ASP table

Posted on 2007-10-09
7
Medium Priority
?
294 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:develc
  • 5
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 375 total points
ID: 20046399
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
 
LVL 1

Author Comment

by:develc
ID: 20046777
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
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 375 total points
ID: 20046793
 

 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20046798
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
 
LVL 1

Author Comment

by:develc
ID: 20046873
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
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20046963
You are storing the serverlise to session ??

Session['name'] = value

in this way you can save the value to the server
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 375 total points
ID: 20046969
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question