473,403 Members | 2,366 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,403 software developers and data experts.

Add array to a datatable

a
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.
}
Nov 17 '05 #1
3 12543
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" <a@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com... 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.
}

Nov 17 '05 #2
a
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 [MVP C#]" wrote:
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" <a@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
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.
}


Nov 17 '05 #3
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" <a@discussions.microsoft.com> wrote in message
news:70**********************************@microsof t.com...
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 [MVP C#]" wrote:
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" <a@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
> 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.
> }


Nov 17 '05 #4

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

Similar topics

4
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to...
9
by: Rui Sampainho | last post by:
Hi Is there any command to transfer the contents of a datatable to an array. What I'm looking for is sort of a bulk process, not an iteration with a loop based on a field by field or row by row...
2
by: Carlos K | last post by:
Hello I'm having some difficulty searching a set of rows in a DataRow collection.. This is my question What would be an efficient way to search any column of an DataRow array Let me try to...
5
by: Geoff Jones | last post by:
Hi Suppose we have an array of DataRows e.g. Dim drMyRows() As DataRow = myTable.Rows(0).GetChildRows("PricesCompany") generated via a relationship. The question I have is this. Isn't...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
8
by: Niron kag | last post by:
Hi, I have a loop an in the loop I populate an array of dataRow. My problem is the array keeps its values from previous loop, and its Length gets bigger every loop. My code is: DataRow...
2
by: Niyazi | last post by:
Hi everyone, I have 5 string in my array that I get from MS Access DB. Public mDov(0) as string and I fill with my for loop. The result as shown below mDov(0) = "ABC_01" mDov(1) = "ABC_02"...
6
by: Paulers | last post by:
Hello, I have a string that I am trying to add each char to a datatable row. for example if I have a string that looks like "abcdefg", I would like to break it up into an array of characters...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.