473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

can't get data from table

I'm very, very new to .Net but have been doing C/C++ for years. I'm
having a great deal of difficulty with something that should be simple.
Here's what I'm trying to do....

Display three columns of data in a table with a checkbox control on
each table row.

When the button is clicked I want to iterate the table and see which
ones are checked so that I can delete them.

I get the table headers but not the table data. You can see my attempt
in Button1_Click().
Thanks in advance.

-rm


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
private SqlConnection myConnection;

private void DisplayCurrentConfig()
{
int[] iWidth = new int[4] {5,260,125,125};
string[] sHeaders = new string[4]
{"Delete","Client","Domain","Route"};
string sql = "select null, CustomerName, Domain, Route, cast([Key]
as varchar(12)) as [Key] from mytable order by CustomerName";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
TableRow headerRow = new TableRow();
bool bHeaderDone = false;
while (myReader.Read())
{
TableRow r = new TableRow();
for (int i = 0; i < 4; i++)
{
TableCell c = new TableCell();
if (i == 4)
c.Visible = false;
c.Width = iWidth[i];

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders[i];
header.Font.Bold = true;
header.BackColor = Color.Red;
header.HorizontalAlign = HorizontalAlign.Left;
headerRow.Cells.Add(header);
}
if (i != 0)
{
if (!myReader.IsDBNull(i))
c.Controls.Add(new LiteralControl(myReader.GetString(i)));
else
c.Controls.Add(new LiteralControl("*ALL*"));
}
else
{
c.Controls.Add(new CheckBox());
}
r.Cells.Add(c);
}
bHeaderDone = true;
Table1.Rows.Add(r);
}
myReader.Close();
Table1.Rows.AddAt(0, headerRow);
}

private void Page_Load(object sender, System.EventArgs e)
{
string myConnString = "Persist Security
Info=False;database=DafDb;server=myhostsql;Connect Timeout=5000;User
ID=user1;Pwd=pass1";
myConnection = new SqlConnection(myConnString);
myConnection.Open();
DisplayCurrentConfig();
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
System.Collections.IEnumerator iEnum = Table1.Rows.GetEnumerator();
while (iEnum.MoveNext())
{
TableRow row = (TableRow) iEnum.Current;
TableCellCollection cellEnum = row.Cells;
System.Collections.IEnumerator ee2 = cellEnum.GetEnumerator();
while (ee2.MoveNext())
{
TableCell tc = (TableCell) ee2.Current;
TextBox1.Text = tc.Text;
}
}
}
}
}

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 101; LEFT: 184px; POSITION:
absolute; TOP: 168px" runat="server"
Width="728px" Height="136px"></asp:Table>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 1040px;
POSITION: absolute; TOP: 328px"
runat="server" Text="Button"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 216px;
POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>

Apr 18 '06 #1
2 1888

Ok.. I'll put you on the right track.

Looping over <table> <tr> and <td> elements isn't the best way .....

What you want to do .. is use one of the built in controls..... and loop
over the elements inside that control.

By googling "asp:repeater" "FindControl" . I found a few articles.

http://www.experts-exchange.com/Prog..._21387438.html

there's one I found. ... which is a person talking about a problem.

Repeater, DataGrid and DataList are the 3 (1.1) controls you can bind too.

Give that a whirl.

...

PS.
You don't want to do that <table><tr> element writing. That's left over
thought patterns from ASP
response.write "<table>"
response.write "<tr>"
etc etc.

Your best bet would be to REMOVE that from your thought patterns as quickly
as you can with asp.net


<rm*****@yahoo.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
I'm very, very new to .Net but have been doing C/C++ for years. I'm
having a great deal of difficulty with something that should be simple.
Here's what I'm trying to do....

Display three columns of data in a table with a checkbox control on
each table row.

When the button is clicked I want to iterate the table and see which
ones are checked so that I can delete them.

I get the table headers but not the table data. You can see my attempt
in Button1_Click().
Thanks in advance.

-rm


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
private SqlConnection myConnection;

private void DisplayCurrentConfig()
{
int[] iWidth = new int[4] {5,260,125,125};
string[] sHeaders = new string[4]
{"Delete","Client","Domain","Route"};
string sql = "select null, CustomerName, Domain, Route, cast([Key]
as varchar(12)) as [Key] from mytable order by CustomerName";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
TableRow headerRow = new TableRow();
bool bHeaderDone = false;
while (myReader.Read())
{
TableRow r = new TableRow();
for (int i = 0; i < 4; i++)
{
TableCell c = new TableCell();
if (i == 4)
c.Visible = false;
c.Width = iWidth[i];

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders[i];
header.Font.Bold = true;
header.BackColor = Color.Red;
header.HorizontalAlign = HorizontalAlign.Left;
headerRow.Cells.Add(header);
}
if (i != 0)
{
if (!myReader.IsDBNull(i))
c.Controls.Add(new LiteralControl(myReader.GetString(i)));
else
c.Controls.Add(new LiteralControl("*ALL*"));
}
else
{
c.Controls.Add(new CheckBox());
}
r.Cells.Add(c);
}
bHeaderDone = true;
Table1.Rows.Add(r);
}
myReader.Close();
Table1.Rows.AddAt(0, headerRow);
}

private void Page_Load(object sender, System.EventArgs e)
{
string myConnString = "Persist Security
Info=False;database=DafDb;server=myhostsql;Connect Timeout=5000;User
ID=user1;Pwd=pass1";
myConnection = new SqlConnection(myConnString);
myConnection.Open();
DisplayCurrentConfig();
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
System.Collections.IEnumerator iEnum = Table1.Rows.GetEnumerator();
while (iEnum.MoveNext())
{
TableRow row = (TableRow) iEnum.Current;
TableCellCollection cellEnum = row.Cells;
System.Collections.IEnumerator ee2 = cellEnum.GetEnumerator();
while (ee2.MoveNext())
{
TableCell tc = (TableCell) ee2.Current;
TextBox1.Text = tc.Text;
}
}
}
}
}

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 101; LEFT: 184px; POSITION:
absolute; TOP: 168px" runat="server"
Width="728px" Height="136px"></asp:Table>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 1040px;
POSITION: absolute; TOP: 328px"
runat="server" Text="Button"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 216px;
POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>

Apr 19 '06 #2
http://www.openmymind.net/databinding/

Theres a sweet tutorial.


<rm*****@yahoo.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
I'm very, very new to .Net but have been doing C/C++ for years. I'm
having a great deal of difficulty with something that should be simple.
Here's what I'm trying to do....

Display three columns of data in a table with a checkbox control on
each table row.

When the button is clicked I want to iterate the table and see which
ones are checked so that I can delete them.

I get the table headers but not the table data. You can see my attempt
in Button1_Click().
Thanks in advance.

-rm


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
private SqlConnection myConnection;

private void DisplayCurrentConfig()
{
int[] iWidth = new int[4] {5,260,125,125};
string[] sHeaders = new string[4]
{"Delete","Client","Domain","Route"};
string sql = "select null, CustomerName, Domain, Route, cast([Key]
as varchar(12)) as [Key] from mytable order by CustomerName";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
SqlDataReader myReader = myCommand.ExecuteReader();
TableRow headerRow = new TableRow();
bool bHeaderDone = false;
while (myReader.Read())
{
TableRow r = new TableRow();
for (int i = 0; i < 4; i++)
{
TableCell c = new TableCell();
if (i == 4)
c.Visible = false;
c.Width = iWidth[i];

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders[i];
header.Font.Bold = true;
header.BackColor = Color.Red;
header.HorizontalAlign = HorizontalAlign.Left;
headerRow.Cells.Add(header);
}
if (i != 0)
{
if (!myReader.IsDBNull(i))
c.Controls.Add(new LiteralControl(myReader.GetString(i)));
else
c.Controls.Add(new LiteralControl("*ALL*"));
}
else
{
c.Controls.Add(new CheckBox());
}
r.Cells.Add(c);
}
bHeaderDone = true;
Table1.Rows.Add(r);
}
myReader.Close();
Table1.Rows.AddAt(0, headerRow);
}

private void Page_Load(object sender, System.EventArgs e)
{
string myConnString = "Persist Security
Info=False;database=DafDb;server=myhostsql;Connect Timeout=5000;User
ID=user1;Pwd=pass1";
myConnection = new SqlConnection(myConnString);
myConnection.Open();
DisplayCurrentConfig();
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
System.Collections.IEnumerator iEnum = Table1.Rows.GetEnumerator();
while (iEnum.MoveNext())
{
TableRow row = (TableRow) iEnum.Current;
TableCellCollection cellEnum = row.Cells;
System.Collections.IEnumerator ee2 = cellEnum.GetEnumerator();
while (ee2.MoveNext())
{
TableCell tc = (TableCell) ee2.Current;
TextBox1.Text = tc.Text;
}
}
}
}
}

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 101; LEFT: 184px; POSITION:
absolute; TOP: 168px" runat="server"
Width="728px" Height="136px"></asp:Table>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 1040px;
POSITION: absolute; TOP: 328px"
runat="server" Text="Button"></asp:Button>
<asp:TextBox id="TextBox1" style="Z-INDEX: 103; LEFT: 216px;
POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
</form>
</body>
</HTML>

Apr 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
13
by: nyt | last post by:
I have a problem of number and text field. I got the database file(mdb) that contains many combo boxes used and its list values are created by "value list" For eg field Field name= 'furniture'...
8
by: Radx | last post by:
Here in my web application, I have a data entry page with serval controls. Some of the controls have autopostback is set true. But the problem is when two or more people are entering data at the...
1
by: vasilip | last post by:
I'm testing out db2 for a project I'm starting that requires proper xml support and I can't seem to get both xml and spatial data to work well in the same table. Once having created a table...
1
by: terryspanky | last post by:
----------------------Below are all the codes don't have errors---- The only problem I have is when I Delete, I'ts not deleting the subject that I click. I want to use the above codes to modify the...
10
by: rcamarda | last post by:
I have created a table that contains buckets to hold activitives of enrollment for each of our admissions officer for each day of an enrollment session. I have an UPDATE that builds rolling totals...
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
1
by: Man4ish | last post by:
Hi, How Eventlistner can be used with rendred combo box. I got one example of combobox in table as follows . /* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.