By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

can't get data from table

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.