petershaw8
asked on
dataset.fieldbyname().asstring
In Delphi, you use dataset to run a query, like select * from atable.
then you can use dataset.fieldbyname('AFiel dName').as String (or as DateTime etc.) to get the field's value.
But I find in C#, you can only use fields' index to get the value rather than using field's name. So if you change your select fields order or insert a new field in the select, you have to chnage a lot of statements.
Is there a way use field's name like Delphi in C#?
then you can use dataset.fieldbyname('AFiel
But I find in C#, you can only use fields' index to get the value rather than using field's name. So if you change your select fields order or insert a new field in the select, you have to chnage a lot of statements.
Is there a way use field's name like Delphi in C#?
dataSet.Tables[0].Rows[nRo wNumber][" ColumnName "].ToStrin g();
string connectionString = "my conn string";
string commandString = "SELECT * FROM mytable";
OdbcDataAdapter adapter = new OdbcDataAdapter(commandStr ing,connec tionString );
DataSet data = new DataSet();
adapter.Fill (data);
// Loop for each record in the dataset
foreach( DataRow row in data.Tables[0].Rows )
{
mycombobox.Items.Add( row["myColumnName"] );
}
Puts all the fields into a dropdown box. Dunno if that helps any. =] Same thing as above almost.
- Joe
string commandString = "SELECT * FROM mytable";
OdbcDataAdapter adapter = new OdbcDataAdapter(commandStr
DataSet data = new DataSet();
adapter.Fill (data);
// Loop for each record in the dataset
foreach( DataRow row in data.Tables[0].Rows )
{
mycombobox.Items.Add( row["myColumnName"] );
}
Puts all the fields into a dropdown box. Dunno if that helps any. =] Same thing as above almost.
- Joe
ASKER
For tomasX2 solution, I am not sure if it works for a little bit complicated sql statement, like
select a.aCol1,
b.bCol1,
(select cCol2 from cTablen where cCol1 = aCol1) as cCol2
from aTable a, bTable b
In this case, I guess dataSet.Tables[0] regers aTable, dataSet.Tables[1] refers bTable, but how about cCol2?
For Joe's solution, Does the row["myColumnName"] always returns a string? and if the field is type of DateTime, then you need convert it fromstring to datetime?
Peter
select a.aCol1,
b.bCol1,
(select cCol2 from cTablen where cCol1 = aCol1) as cCol2
from aTable a, bTable b
In this case, I guess dataSet.Tables[0] regers aTable, dataSet.Tables[1] refers bTable, but how about cCol2?
For Joe's solution, Does the row["myColumnName"] always returns a string? and if the field is type of DateTime, then you need convert it fromstring to datetime?
Peter
The row[index] returns an object, so you can cast it to it's original value.
- Joe
- Joe
Here's a little form in ASP.NET you can test with. Shows how to interact with object values from the row[index] feature ... I was bored. =]
- Joe
Page:
|------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
<%@ Page language="c#" Codebehind="WebForm1.aspx. cs" AutoEventWireup="false" Inherits="EE.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri pt">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table align="center">
<tr>
<td><asp:datagrid id="DataGrid1" runat="server" Width="100%"></asp:datagri d></td>
</tr>
<tr>
<td align="center"><asp:button id="GetDateButton" runat="server" Text="Get Long Date"></asp:button><asp:bu tton id="GetTimeButton" runat="server" Text="Get Short Time"></asp:button></td>
</tr>
<tr>
<td><br>
<br>
<asp:Label id="MessageLabel" runat="server" Font-Bold="True"></asp:Lab el>
<br>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropD ownList></ td>
</tr>
</table>
</form>
</body>
</HTML>
Code:
|------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
namespace EE
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected DataTable data;
protected System.Web.UI.WebControls. DataGrid DataGrid1;
protected System.Web.UI.WebControls. Button GetDateButton;
protected System.Web.UI.WebControls. Label MessageLabel;
protected System.Web.UI.WebControls. DropDownLi st DropDownList1;
protected System.Web.UI.WebControls. Button GetTimeButton;
protected DataView dataView;
private void Page_Load(object sender, System.EventArgs e)
{
if (Session["TestData"] == null)
{
BuildTable();
}
else
data = (DataTable)Session["TestDa ta"];
dataView = new DataView(data);
dataView.Sort = "Item";
if (!IsPostBack)
BindList();
}
private void BindList()
{
DataGrid1.DataSource = dataView;
DataGrid1.DataBind();
}
private void BuildTable()
{
data = new DataTable();
data.Columns.Add(new DataColumn("Qty", typeof(string)));
data.Columns.Add(new DataColumn("Item", typeof(string)));
data.Columns.Add(new DataColumn("Price", typeof(string)));
// Can also Use OdbcType.DateTime, SqlDateTime, etc
data.Columns.Add(new DataColumn("Date", typeof(DateTime)));
Session["DL3_ShoppingCart" ] = data;
// first load -- prepopulate with some data
for (int i=1; i<5; i++)
{
DataRow dr = data.NewRow();
dr[0] = ((int)((i%2)+1)).ToString( );
dr[1] = "Item " + i.ToString();
dr[2] = ((double)(1.23 * (i+1))).ToString();
dr[3] = DateTime.Now.AddDays((int) (2*(i+30)) );
data.Rows.Add(dr);
}
}
#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.GetDateButton.Click += new System.EventHandler(this.G etDateButt on_Click);
this.GetTimeButton.Click += new System.EventHandler(this.G etTimeButt on_Click);
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
private void GetDateButton_Click(object sender, System.EventArgs e)
{
try
{
foreach(DataRow row in data.Rows)
{
DateTime myDate = (DateTime) row["date"];
DropDownList1.Items.Add(my Date.ToLon gDateStrin g());
}
}
catch(Exception exc)
{
MessageLabel.Text = exc.Message;
}
}
private void GetTimeButton_Click(object sender, System.EventArgs e)
{
try
{
foreach(DataRow row in data.Rows)
{
DateTime myDate = (DateTime) row["date"];
DropDownList1.Items.Add(my Date.ToSho rtTimeStri ng());
}
}
catch(Exception exc)
{
MessageLabel.Text = exc.Message;
}
}
}
}
- Joe
Page:
|-------------------------
<%@ Page language="c#" Codebehind="WebForm1.aspx.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScri
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table align="center">
<tr>
<td><asp:datagrid id="DataGrid1" runat="server" Width="100%"></asp:datagri
</tr>
<tr>
<td align="center"><asp:button
</tr>
<tr>
<td><br>
<br>
<asp:Label id="MessageLabel" runat="server" Font-Bold="True"></asp:Lab
<br>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropD
</tr>
</table>
</form>
</body>
</HTML>
Code:
|-------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
namespace EE
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected DataTable data;
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected System.Web.UI.WebControls.
protected DataView dataView;
private void Page_Load(object sender, System.EventArgs e)
{
if (Session["TestData"] == null)
{
BuildTable();
}
else
data = (DataTable)Session["TestDa
dataView = new DataView(data);
dataView.Sort = "Item";
if (!IsPostBack)
BindList();
}
private void BindList()
{
DataGrid1.DataSource = dataView;
DataGrid1.DataBind();
}
private void BuildTable()
{
data = new DataTable();
data.Columns.Add(new DataColumn("Qty", typeof(string)));
data.Columns.Add(new DataColumn("Item", typeof(string)));
data.Columns.Add(new DataColumn("Price", typeof(string)));
// Can also Use OdbcType.DateTime, SqlDateTime, etc
data.Columns.Add(new DataColumn("Date", typeof(DateTime)));
Session["DL3_ShoppingCart"
// first load -- prepopulate with some data
for (int i=1; i<5; i++)
{
DataRow dr = data.NewRow();
dr[0] = ((int)((i%2)+1)).ToString(
dr[1] = "Item " + i.ToString();
dr[2] = ((double)(1.23 * (i+1))).ToString();
dr[3] = DateTime.Now.AddDays((int)
data.Rows.Add(dr);
}
}
#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.GetDateButton.Click += new System.EventHandler(this.G
this.GetTimeButton.Click += new System.EventHandler(this.G
this.Load += new System.EventHandler(this.P
}
#endregion
private void GetDateButton_Click(object
{
try
{
foreach(DataRow row in data.Rows)
{
DateTime myDate = (DateTime) row["date"];
DropDownList1.Items.Add(my
}
}
catch(Exception exc)
{
MessageLabel.Text = exc.Message;
}
}
private void GetTimeButton_Click(object
{
try
{
foreach(DataRow row in data.Rows)
{
DateTime myDate = (DateTime) row["date"];
DropDownList1.Items.Add(my
}
}
catch(Exception exc)
{
MessageLabel.Text = exc.Message;
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.