Add array to a datatable

G

Guest

Hi:

I'm trying to add rows to a datatable from an array, but due to lack of
brain power, am unable to make this work. I'm being told that I don't have
enough columns (I only want one column at this point.) Anyone have any ideas?

Thanks
====================================================

private void button1_Click(object sender, System.EventArgs e)
{
WebRequest req =
WebRequest.Create("http://www.sec.gov/Archives/edgar/daily-index/" +
"company." + "20041222" + ".idx"); // Create the Request object
WebResponse response = req.GetResponse(); //Create
the Response object
Stream stream = response.GetResponseStream(); //Create a
Stream
StreamReader sr = new StreamReader(stream); //Open the file in a
stream reader

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read
From A File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
string AllData1 = sr.ReadToEnd(); //Read the rest
of the data in the file.
string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split
off each row at the Carriage Return/Line Feed

result.Tables.Add("MyNewTable"); //Add DataTable to hold the
DataSet
result.Tables["MyNewTable"].Columns.Add("Data"); //Add a
single column
result.Tables["MyNewTable"].Rows.Add(rows); //Add the
rows to the DataTable/DataSet

for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable.
}
 
M

Miha Markic [MVP C#]

Hi,

You have to insert a row at a time, something like:
foreach (string row in rows)
{
result.Tables["MyNewTable"].Rows.Add(row)
}

also, this code is wrong:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

it won't remove *first* 8 rows, if you want to remove them you should do:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

a said:
Hi:

I'm trying to add rows to a datatable from an array, but due to lack of
brain power, am unable to make this work. I'm being told that I don't have
enough columns (I only want one column at this point.) Anyone have any
ideas?

Thanks
====================================================

private void button1_Click(object sender, System.EventArgs e)
{
WebRequest req =
WebRequest.Create("http://www.sec.gov/Archives/edgar/daily-index/" +
"company." + "20041222" + ".idx"); // Create the Request object
WebResponse response = req.GetResponse(); //Create
the Response object
Stream stream = response.GetResponseStream(); //Create a
Stream
StreamReader sr = new StreamReader(stream); //Open the file in a
stream reader

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read
From A File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
string AllData1 = sr.ReadToEnd(); //Read the rest
of the data in the file.
string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split
off each row at the Carriage Return/Line Feed

result.Tables.Add("MyNewTable"); //Add DataTable to hold the
DataSet
result.Tables["MyNewTable"].Columns.Add("Data"); //Add a
single column
result.Tables["MyNewTable"].Rows.Add(rows); //Add the
rows to the DataTable/DataSet

for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable.
}
 
G

Guest

Miha: Thanks, but I get the error that "Argument"1": cannot convert from
'string' to 'System.Data.DataRow' when I change the code to doing a loop as
you suggest...what now?

Paul
-------------------------------------------------------------

Miha Markic said:
Hi,

You have to insert a row at a time, something like:
foreach (string row in rows)
{
result.Tables["MyNewTable"].Rows.Add(row)
}

also, this code is wrong:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

it won't remove *first* 8 rows, if you want to remove them you should do:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

a said:
Hi:

I'm trying to add rows to a datatable from an array, but due to lack of
brain power, am unable to make this work. I'm being told that I don't have
enough columns (I only want one column at this point.) Anyone have any
ideas?

Thanks
====================================================

private void button1_Click(object sender, System.EventArgs e)
{
WebRequest req =
WebRequest.Create("http://www.sec.gov/Archives/edgar/daily-index/" +
"company." + "20041222" + ".idx"); // Create the Request object
WebResponse response = req.GetResponse(); //Create
the Response object
Stream stream = response.GetResponseStream(); //Create a
Stream
StreamReader sr = new StreamReader(stream); //Open the file in a
stream reader

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read
From A File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
string AllData1 = sr.ReadToEnd(); //Read the rest
of the data in the file.
string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split
off each row at the Carriage Return/Line Feed

result.Tables.Add("MyNewTable"); //Add DataTable to hold the
DataSet
result.Tables["MyNewTable"].Columns.Add("Data"); //Add a
single column
result.Tables["MyNewTable"].Rows.Add(rows); //Add the
rows to the DataTable/DataSet

for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable.
}
 
M

Miha Markic [MVP C#]

Hi,

Ah, sorry, it should be:
result.Tables["MyNewTable"].Rows.Add(new object[]{row});

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

a said:
Miha: Thanks, but I get the error that "Argument"1": cannot convert from
'string' to 'System.Data.DataRow' when I change the code to doing a loop
as
you suggest...what now?

Paul
-------------------------------------------------------------

Miha Markic said:
Hi,

You have to insert a row at a time, something like:
foreach (string row in rows)
{
result.Tables["MyNewTable"].Rows.Add(row)
}

also, this code is wrong:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

it won't remove *first* 8 rows, if you want to remove them you should do:
for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(0);
}

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info

a said:
Hi:

I'm trying to add rows to a datatable from an array, but due to lack of
brain power, am unable to make this work. I'm being told that I don't
have
enough columns (I only want one column at this point.) Anyone have any
ideas?

Thanks
====================================================

private void button1_Click(object sender, System.EventArgs e)
{
WebRequest req =
WebRequest.Create("http://www.sec.gov/Archives/edgar/daily-index/" +
"company." + "20041222" + ".idx"); // Create the Request object
WebResponse response = req.GetResponse(); //Create
the Response object
Stream stream = response.GetResponseStream(); //Create
a
Stream
StreamReader sr = new StreamReader(stream); //Open the file in a
stream reader

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read
From A File instead of a webrequest

DataSet result = new DataSet(); //The DataSet to Return
string AllData1 = sr.ReadToEnd(); //Read the rest
of the data in the file.
string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split
off each row at the Carriage Return/Line Feed

result.Tables.Add("MyNewTable"); //Add DataTable to hold the
DataSet
result.Tables["MyNewTable"].Columns.Add("Data"); //Add a
single column
result.Tables["MyNewTable"].Rows.Add(rows); //Add the
rows to the DataTable/DataSet

for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(i); //Remove first 8 rows
from the DataTable/DataSet
}

dataGrid1.SetDataBinding(result, "MyNewTable"); //Binds DataGrid to
DataSet,displaying datatable.
}
 

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

Top