Error when adding array to datatable

G

Guest

I get the error that "Argument"1": cannot convert from
'string' to 'System.Data.DataRow' at the foreach loop to add rows to the
DataTable...any ideas how to fix this?

Paul

==================================================

private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Text=null; //Clear
the RichTextBox

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
DataSet result = new DataSet(); //The DataSet
to Return

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read From A
File instead of a webreques
//---------------------------------------------------------------------------------------------------
string AllData1 = sr.ReadToEnd(); //Read the rest of
the data in the file.

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

string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split off each
row at the Carriage Return/Line Feed

foreach(string r in rows)
{
result.Tables["MyNewTable"].Rows.Add(r); //Add the row to the
DataTable/DataSet
}

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

//---------------------------------------------------------------------------------------------------

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

Morten Wennevik

Hi a,

As the error says you cannot add String as DataRow. You need to pass in a DataRow or an array of objects, one for each column.


foreach(string r in rows)
{
DataRow row = result.Tables["MyNewTable"].NewRow();
row[0] = r;
result.Tables["MyNewTable"].Rows.Add(row);
}

Alternately you could do

foreach(string r in rows)
{
result.Tables["MyNewTable"].Rows.Add(new string[]{r});
}


I get the error that "Argument"1": cannot convert from
'string' to 'System.Data.DataRow' at the foreach loop to add rows to the
DataTable...any ideas how to fix this?

Paul

==================================================

private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Text=null; //Clear
the RichTextBox

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
DataSet result = new DataSet(); //The DataSet
to Return

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read From A
File instead of a webrequest
//---------------------------------------------------------------------------------------------------
string AllData1 = sr.ReadToEnd(); //Read the rest of
the data in the file.

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

string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split off each
row at the Carriage Return/Line Feed

foreach(string r in rows)
{
result.Tables["MyNewTable"].Rows.Add(r); //Add the row to the
DataTable/DataSet
}

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

//---------------------------------------------------------------------------------------------------

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

Guest

Morten:
Thank you very much for the help. I didn't know how to write that.
Paul
--------------------------------------------------------------------------

Morten Wennevik said:
Hi a,

As the error says you cannot add String as DataRow. You need to pass in a DataRow or an array of objects, one for each column.


foreach(string r in rows)
{
DataRow row = result.Tables["MyNewTable"].NewRow();
row[0] = r;
result.Tables["MyNewTable"].Rows.Add(row);
}

Alternately you could do

foreach(string r in rows)
{
result.Tables["MyNewTable"].Rows.Add(new string[]{r});
}


I get the error that "Argument"1": cannot convert from
'string' to 'System.Data.DataRow' at the foreach loop to add rows to the
DataTable...any ideas how to fix this?

Paul

==================================================

private void button1_Click(object sender, System.EventArgs e)
{
richTextBox1.Text=null; //Clear
the RichTextBox

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
DataSet result = new DataSet(); //The DataSet
to Return

//StreamReader sr = new StreamReader("c:\\test.txt"); //Read From A
File instead of a webrequest
//---------------------------------------------------------------------------------------------------
string AllData1 = sr.ReadToEnd(); //Read the rest of
the data in the file.

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

string[] rows = AllData1.Split("\r\n".ToCharArray()); //Split off each
row at the Carriage Return/Line Feed

foreach(string r in rows)
{
result.Tables["MyNewTable"].Rows.Add(r); //Add the row to the
DataTable/DataSet
}

for (int i = 1; i <= 8; i++)
{
result.Tables["MyNewTable"].Rows.RemoveAt(0); //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