Community discussion forum

How to copy a datatable into an array?

Tags: csharp, db India
  • 2 years ago

    Hi ,

    How to copy a datatable into an array? Is retrival of data from array is much faster than from the Datatable?

     

  • 2 years ago

    could u tell me the no of fields in ur Datatable...

  • 2 years ago

    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()

     

  • 2 years ago

    Can You tell How to read the data from a particular cell of EXCEL using C# Code

  • 2 years ago

    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)
            Next














        End Sub

     

    meet u again...

    Mathi..

     

     

     

     

  • 2 years ago

    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

     

  • 2 years ago

    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; }

    }

    }

  • 2 years ago

    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

     

     

  • 1 year ago

    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.

  • 1 year ago

    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 needed

    column2.Add(dtRow[1]);

    column3.Add(dtRow[2]);

    column4.Add(dtRow[3]);

    }

     

  • 1 year ago

    thanks a lot dude :-) that worked perfectly.

  • 1 year ago

    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.

  • 1 year ago

    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 value

    seed = 1; //change seed

    rndNo =
    new Random(seed);

    Console.WriteLine(rndNo.Next(20).ToString());

  • 1 year ago

    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.

  • 1 year ago

    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

  • 1 year ago

    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

  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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

  • 1 year ago

    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)

  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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
  • 1 year ago

    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

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback