can't get data from table

R

rmandel

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;

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders;
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>
 
S

sloan

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/Pro...Languages/Dot_Net/ASP_DOT_NET/Q_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




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;

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders;
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>
 
S

sloan

http://www.openmymind.net/databinding/

Theres a sweet tutorial.




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;

if (!bHeaderDone)
{
TableHeaderCell header = new TableHeaderCell();
header.Text = sHeaders;
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top