locked
Creating a Datatable from a ArrayList

    Question

  • I have a datatable and when i add the rows to it....I click my OK button when i click my OK button I add all the rows to an arraylist...then i loop thorough my arraylist and insert the values into my database.....How would one go about getting those values out of my database back into the arraylist and back into the datatable....I know how to put it in the database but i am kinda cloudy on how I would get it back out and into my datatable....any help with this?????
    Monday, November 13, 2006 11:30 PM

Answers

  • i see thats better explanation.

    well the sqldataAdapter is used with connecting to and updating/filling results to/from the database, sql server.

    dataset/datatable is an in memory storage type object. it holds everything in memory until you close the app of course. you can save the data from this from the dataset into xml and re-load if you like. The reason I was suggesting to store it into sql is because you referred to "database".

    a datatable is just an im memory storage unit really, holds records and information in memory and is not related to a database but rather "mimics" or acts like a mini database in memory. But of course if you are going to save data, save it to either SQL (using the methods mentioned) or by Xml using the dataset, which holds datatables, and re-load them

    from what I understand, you will only add an order to the datatable if the user creates an order. So any orders that are created should be stored in the database (or other form). Of course you will need to remove the order should they cancel it then after all this, at the very end, commit changes to the database (or other form).

    tricky to explain

    Tuesday, November 14, 2006 11:57 AM

All replies

  • well you would use a SqlDataReader to read the values back 1 by 1 and store it into an arraylist. But you are better off using a SqlDataAdapter to fill the dataset/datatable with values and update the database when you change values in the dataset/datatable and avoid the whole arraylist process.
    Monday, November 13, 2006 11:38 PM
  • right but how would i save the values without using the arraylist .... I want to loop through all my rows and add the rows to the arraylist...how would i accomplish that without using the arraylist??
    Monday, November 13, 2006 11:52 PM
  • well you simply bind the dataset to the datagrid. The user then changes the values in the datagrid. The dataset is automatically updated. You then use the dataAdapter's Update command to update the database but you must implement the correct update commands:

    InsertCommand

    DeleteCommand

    UpdateCommand

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=871768&SiteID=1

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=656817&SiteID=1 (read the link to sqldataAdapter and read the msdn document in that link)

     

    even if you say manually changed the values in the datatable, this is ok as when you come to updating the database using the SqlDataAdapter's Update() command, just give it the dataset/datatable and it will automatically update it by comparing the values in the datatable and database, seeing which ones have been flagged in the dataset and update that row/value in the database

    Monday, November 13, 2006 11:56 PM
  • Right will I need to only do this if i save the parent object...this is how it will work i have a main form that has a button...The user clicks the button then a form comes up with the datagrid...the user enters the data in the rows....how does the data in the rows stay in memory??? when they close the form to save the main object how will the sqldataadapter save these changes if the form is close first.....thats why i think i should use the array list to add the rows to the arraylist then loop through that and add them to my insert method.....what do u think
    Tuesday, November 14, 2006 3:12 AM
  • I think its a bad idea on what you are doing - involving other objects and being inefficient when you dont need to be :-)

    you should just update the database when you are say, closing the other form containing the dataset and datagrid (binding it) or on a button called save on that form.

    you shouldnt need to use objects or think of things which can already be done - in this case, the dataset, datagrid and dataAdapter (sql). using these combinations you can easily achieve what you are wanting without using/creating unneeded objects :-)

    Tuesday, November 14, 2006 3:46 AM
  • Why is that a bad idea why would i wanna save details about an object without saving the object itself???? that is a bad idea I think....

    u say "you should just update the database when you are say, closing the other form containing the dataset and datagrid (binding it) or on a button called save on that form."

     

    but if I am not saving the main Parent object why should i save the child object????The objects I am creating are needed they are the Parent objects and the child object I only want to save if i save the parent object

    Tuesday, November 14, 2006 3:55 AM
  • which "parent" object are you referring to and which "child" object are you referring to?

    your original question was referring to on how to retrieve data back from the database, which I explained, and how to insert data from arraylist into the database, which I explained also. Both of these can be done with the dataset and SqlDataAdapter, avoiding the need for an arraylist

    Tuesday, November 14, 2006 3:56 AM
  • Well I have a Order Object(Parent) that can have multiple Order Rates(Child) which are charges...What I meant was why would i want to save my Order Rates if my user does not save the Order...i believe that is wasted space on the DB...What do u think?...Now i know the SqlAdapter and the Dataset object have the commandBuilder() object where i commit to to my dataTable, I wouldnt mind using it but in this case, When the user enters his Order Rates the user will close that form and submit the Order.....so I would need those datarows to stay in memory somehow so they can be saved....and i do not think the SqlDataAdapter and Dataset works in this situatiion....what do u think?
    Tuesday, November 14, 2006 11:50 AM
  • i see thats better explanation.

    well the sqldataAdapter is used with connecting to and updating/filling results to/from the database, sql server.

    dataset/datatable is an in memory storage type object. it holds everything in memory until you close the app of course. you can save the data from this from the dataset into xml and re-load if you like. The reason I was suggesting to store it into sql is because you referred to "database".

    a datatable is just an im memory storage unit really, holds records and information in memory and is not related to a database but rather "mimics" or acts like a mini database in memory. But of course if you are going to save data, save it to either SQL (using the methods mentioned) or by Xml using the dataset, which holds datatables, and re-load them

    from what I understand, you will only add an order to the datatable if the user creates an order. So any orders that are created should be stored in the database (or other form). Of course you will need to remove the order should they cancel it then after all this, at the very end, commit changes to the database (or other form).

    tricky to explain

    Tuesday, November 14, 2006 11:57 AM