We help IT Professionals succeed at work.

Put data from database into a ASP table

develc
develc asked
on
308 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?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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?
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
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

Author

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?
CERTIFIED EXPERT

Commented:
You are storing the serverlise to session ??

Session['name'] = value

in this way you can save the value to the server
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.