473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Error when adding array to datatable

a
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.
}
Nov 17 '05 #1
2 5492
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});
}
On Fri, 06 May 2005 19:01:01 +0200, a <a@discussions.microsoft.com> wrote:
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.
}


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
a
Morten:
Thank you very much for the help. I didn't know how to write that.
Paul
--------------------------------------------------------------------------

"Morten Wennevik" wrote:
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});
}
On Fri, 06 May 2005 19:01:01 +0200, a <a@discussions.microsoft.com> wrote:
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.
}


--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: tshad | last post by:
I am getting the error: **************************************************************************** Array does not have that many dimensions. Description: An unhandled exception occurred during...
6
by: Dacuna | last post by:
I have a dataset that I created programmatically and bind to a datagrid. When I add a row and I .show the form I get an error "Error creating window handle" This only happens if I have the code...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
3
by: diesel | last post by:
Ok, once again I'm at my wits' end with a VB.Net problem. I have a button which opens a wizard that allows the user to enter a new record to a table. I have a datatable called dtItems that...
9
by: cc | last post by:
Hi, I having created a simple WebService (in VS 2005) with just one WebMethod as follows : public DataTable GetProducts() { DataTable objDataTable = null; // code for filling up the...
0
by: Anish G | last post by:
Hi All, I am getting the below given error while running my application in live server. In my local machine, its working fine. Please help me as it is very urgent for me. Exception from...
63
by: Kapteyn's Star | last post by:
Hi newsgroup The program here given is refused by GCC with a error i cannot understand. It says rnd00.c: In function ‘main’: rnd00.c:26: error: expected expression before ‘]’ token ...
8
by: jehugaleahsa | last post by:
Hello: We wrote an entire application where we add our DataRows to our DataTables immediately. However, we have to shut off our constraints to do this. We would like to use detached DataRows to...
3
by: jehugaleahsa | last post by:
Hello: I am binding a DataGridView with a BindingList<T>, where T is a custom business object that implements INotifyPropertyChanged. When you bind a DataGridView to a DataTable, it has this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.