Community discussion forum
How to copy a datatable into an array?
-
Hi ,
How to copy a datatable into an array? Is retrival of data from array is much faster than from the Datatable?
-
could u tell me the no of fields in ur Datatable...
-
Hi Alagan..
The datatable may contain any no. of columns(that is specified in the query). I need to copy dynamically as we copy a recordset into array in ASP
aryObject=rsObject.Getrows()
-
Can You tell How to read the data from a particular cell of EXCEL using C# Code
-
Hai ,,
This is mathi again... i've tried in two ways to copy a datatable in to an array... The code is as follows
Sub copyarray()
Dim array As ArrayList = New ArrayList(100)
For i As Int16 = 0 To YourDatatable.Rows.Count - 1
array.Add(YourDatatable.Rows(i))
MsgBox(CType(array.Item(i), DataRow).Item("Fieldname").ToString)
Next
End Sub
Sub copyArray1()
Dim array As Array = System.Array.CreateInstance(GetType(Object), 100)
For i As Int16 = 0 To YourDatatable.Rows.Count - 1
array.SetValue(CType(YourDatatable.Rows(i), Object), i)
Next
For i As Int16 = 0 To YourDatatable.Rows.Count - 1
MsgBox(CType(array.GetValue(i), DataRow).Item("Fieldname").ToString)
NextEnd Sub
meet u again...
Mathi..
-
I am a vb rather than c# programmer,
but have you considered the function SPLIT()
it returns a comformant array from a string and a specified separator
so turn table into strings and use split to do the leg work the opposite functio is JOIN I think
WALK thru data join fields into a large string with a suitable separator and join strings with a record separator then do split for each record to generate the required array
-
here is the code to add to columns to array and retreive it.I hope it will be usefull.
OleDbCommand cmd = new OleDbCommand("SELECT * FROM tblAuthors",conn);
this.conn .Open ();
OleDbDataReader rsAutors = cmd.ExecuteReader();
ArrayList Authors = new ArrayList();while(rsAutors.Read())
{
Authors.Add (new AddValue
(rsAutors.GetString(1),rsAutors.GetInt32 (0)));
}
rsAutors.Close();
this.conn.Close();public
class AddValue{
public string m_Display; public long m_Value; public AddValue(string Display, long Value){
m_Display = Display;
m_Value = Value;
}
public string Display{
get { return m_Display; }}
public long Value{
get { return m_Value; }}
}
-
Hi,
private void CopyDataTableToArray(DataTable copyDT)
{
ArrayList dataArray = new ArrayList();
foreach (DataRow dtRow in copyDT.Rows)
{
dataArray.Add(dtRow);
}
}
but, Is this did you want? Do you want entire row into an Array ?
Regards, Ram
-
hi, lets say i have a data table with 4 columns and a row like this
S.no. name age occupation
1 xyz 20 abc
now i want to store each element of this row in an array. lets say test[4].
then test[1] = 1 , test[2] = xyz , test[3] = 20 , test[4] = abc.
how do i do this? any help i welcomed :-)
thanks.
-
Hi,
simple way is to create arralylist for each column present in data table
ArrayList column1 = new ArrayList(); ArrayList column2 = new ArrayList();ArrayList column3 = new ArrayList(); ArrayList column4 = new ArrayList();foreach (DataRow dtRow in copyDT.Rows){
column1.Add(dtRow[0].ToString());
//Cast to sepecific data type if neededcolumn2.Add(dtRow[1]);
column3.Add(dtRow[2]);
column4.Add(dtRow[3]);
}
-
thanks a lot dude :-) that worked perfectly.
-
hi,
actually im making an online examination tool and i'm kinda new to c# . i would like to generate random numbers. for ex. if i have y = 20 then i would like to generate random numbers from 0 to 19 and in such a way that if one number has been generated once, it doesn't get generated again. i was able to generate the random numbers but they were gettin repeated.
thanks.
-
Hi,
You didn't mention how many random numbers you want at any time.
you have to use different seed to genearate different random numbers by this avoids reapeating the numbers.
example:
Random rndNo;int seed = 0;rndNo =
new Random(seed);Console.WriteLine(rndNo.Next(20).ToString()); //20 Max valueseed = 1; //change seed
rndNo = new Random(seed); Console.WriteLine(rndNo.Next(20).ToString()); -
hi,
ok the thing is that i have a database with lets say 20 questions each with a serial number from 1 to 20. now i want to randomize the order in which the questions appear based on their serial no. so if random number 4 is generated then 4th question will be displayed. and i dont want the questions to repeat. i hope this clears the situation.
-
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
-
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array with your numbers or objects or questions or what ever and empty the temp
(4)use your simple randomness to select 2 indices (it does not matter if they are the same)
(5)now swap the contents of the array aboutusing the 2 indices and the temp structure... in whatever language you like a->t b->a t->b
(6)do this a whole lot of times (more than the number of random entries)
(7)now output the array in order and you will be presented with an absolutely complete set of things in a random order with absolutely no repetition
if you are just wanting to randomise the question order I would simple put the questions in the array as strings
hope this helps
-
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)swap the
-
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)
-
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)swap the
contents -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)swap the
contents of the -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)swap the
contents of -
The last reply is correct ... when random numbers are generated a seed value must be used as random numbers are really just a list of previously randomised numbers for use by the programmer.
even if you use a different seed value you will not ensure unique random numbers from a set of possible numbers. you will always have to check and see that a number has not been chosen. and if it has re select a number.
this method may have you thrashing about trying to get the last available number in a set, currently not selected. ie the number 19 has not been selected so far and you already have all the rest, but must keep picking and selecting until it is.
Try the following:
(1) create an array that will hold your numbers or object or questions or what ever.
(2) create a temp structure to hold 1 instance of the number or object or question or what ever.
(3)fill the array and empty the temp
(4)use your simple randomness to select 2 indices (it
doesnot matter if they are the same)
(5)swap
Post a reply
Related discussion
-
How to Export Datagridview contents to Excel
by BarbaMariolino (8 replies)
-
Speech Recognition
by jitvinder (153 replies)
-
Socket Programming in C# - Part 1
by lespero (24 replies)
-
Creating a Windows Service in VB.NET
by davidvanr (108 replies)
-
Multithreading in VB.NET
by catchsyed (5 replies)
Quick links
Recent activity
- Ezequiel Gonzalez replied to Concurrency violation: the ...
- kim wooki replied to I wanna developer in UK .
- kim wooki replied to I wanna developer in UK .
- nancy smith replied to How to convert mts to mov o...
- nancy smith replied to How to play MTS/M2TS with q...
- scofield sofia replied to Convert 3GP to video for Mac?
Enter your message below
Sign in or Join us (it's free).