Groups | Blog | Home
all groups > dotnet ado.net > may 2007 >

dotnet ado.net : [.NET1.1] datarow and itemarray



Freddyboy
5/30/2007 8:55:04 AM
hi,

I have an issue with datarow and itemarray.
In a datatable I have followong datas :
OPS 014E
OPS <NULL>

I perform a foreach on each datarow like this :
foreach(DataRow dr in aTable.Rows)
{
object[] o = dr.ItemArray
string s1 =
(o[0] != DBNull.Value) ? Convert.ToString(o[0]) : string.Empty;
string s2 =
(o[1] != DBNull.Value) ? Convert.ToString(o[1]) : string.Empty;

}

During my second loop, the value for s2 is 014E and not null
It's strange.

Do you have an idea to solve my problem ?

thanks for your help

best regards

Freddyboy

Manish Bafna
5/30/2007 10:53:01 PM
Hi,
I think your code should work fine.Because below sample code is working
perfect in my VS IDE.A cleaner way to code would be to use foreach for
looping through dr.ItemArray.Below code is working:
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn();
DataColumn col2 = new DataColumn();
col1.DataType = typeof(System.String);
col1.ColumnName ="First";
col2.DataType = typeof(System.String);
col2.ColumnName ="Second";
dt.Columns.Add(col1);
dt.Columns.Add(col2);

DataRow dr1 = dt.NewRow();
DataRow dr2 = dt.NewRow();
dr1["First"] = "manish";
dr1["Second"] = "10";
dt.Rows.Add(dr1);
dr2["First"] = "sanjay";
dr2["Second"] = "";
dt.Rows.Add(dr2);
dt.AcceptChanges();
foreach(DataRow dr in dt.Rows)
{
object[] o = dr.ItemArray;
string s1 = (o[0] != DBNull.Value) ? Convert.ToString(o[0]) :
string.Empty;
string s2 = (o[1] != DBNull.Value) ? Convert.ToString(o[1]) :
string.Empty;
MessageBox.Show(s1);
MessageBox.Show(s2);

}
dataGrid1.DataSource = dt;
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.



[quoted text, click to view]
AddThis Social Bookmark Button