Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Adding Records to a Dataset

pearsonbepearsonbe Posts: 3Member
I have an issue and a question. My application uses an XML database and is having a problem adding and saving records. A new record is created by getting a new row from the existing dataset and then immediately adding the row to the dataset's table collection and accepting the changes. At this point I would expect the row to have an index/count of n+1; it doesn't and will generate an error if accessed as n+1. Then, since the dataset is bound to a datagrid/datview on the UI, the record at position n+1 is displayed for the user to make changes.

Now the problem. The record displayed is not the new one. I've a few different things including sorting the data to force the dataview to place the new record at the end of the dataset where I expect it. This doesn't work. How do I get this to work? An easier approach would be to make my changes to the new record before it is added to the dataset, but that presents problems due to the architecture of the app.


Comments

  • bmiller1222bmiller1222 Posts: 107Member
    : I have an issue and a question. My application uses an XML database and is having a problem adding and saving records. A new record is created by getting a new row from the existing dataset and then immediately adding the row to the dataset's table collection and accepting the changes. At this point I would expect the row to have an index/count of n+1; it doesn't and will generate an error if accessed as n+1. Then, since the dataset is bound to a datagrid/datview on the UI, the record at position n+1 is displayed for the user to make changes.
    :
    : Now the problem. The record displayed is not the new one. I've a few different things including sorting the data to force the dataview to place the new record at the end of the dataset where I expect it. This doesn't work. How do I get this to work? An easier approach would be to make my changes to the new record before it is added to the dataset, but that presents problems due to the architecture of the app.
    :
    :
    :
    ok, these objects operate on data that is not connected to a data source (initially you need to connect them, but they will not perform changes to a database unless specifically coding the feature). i would recommend a datatable instead. if you add a row to a datatable, changes take place immediately and your datagrid will be updated. if you plan to update a database you must code this also.
  • pearsonbepearsonbe Posts: 3Member
    : : I have an issue and a question. My application uses an XML database and is having a problem adding and saving records. A new record is created by getting a new row from the existing dataset and then immediately adding the row to the dataset's table collection and accepting the changes. At this point I would expect the row to have an index/count of n+1; it doesn't and will generate an error if accessed as n+1. Then, since the dataset is bound to a datagrid/datview on the UI, the record at position n+1 is displayed for the user to make changes.
    : :
    : : Now the problem. The record displayed is not the new one. I've a few different things including sorting the data to force the dataview to place the new record at the end of the dataset where I expect it. This doesn't work. How do I get this to work? An easier approach would be to make my changes to the new record before it is added to the dataset, but that presents problems due to the architecture of the app.
    : :
    : :
    : :
    : ok, these objects operate on data that is not connected to a data source (initially you need to connect them, but they will not perform changes to a database unless specifically coding the feature). i would recommend a datatable instead. if you add a row to a datatable, changes take place immediately and your datagrid will be updated. if you plan to update a database you must code this also.
    :
    Thanks for responding. Correct - the data is not connected to a data source such as as DB; it is kept in a stand-alone dataset. The datagrid has a datatable and the problem is that when I add a row the changes are not taking place immediately. I read that Microsoft recommends making changes, getting the changes into a new dataset, and then merging the changes back to the original dataset. I haven't tried this yet.

    I'll post the code tomorrow.
  • bmiller1222bmiller1222 Posts: 107Member
    [b][red]This message was edited by bmiller1222 at 2005-6-2 19:6:7[/red][/b][hr]
    : : : I have an issue and a question. My application uses an XML database and is having a problem adding and saving records. A new record is created by getting a new row from the existing dataset and then immediately adding the row to the dataset's table collection and accepting the changes. At this point I would expect the row to have an index/count of n+1; it doesn't and will generate an error if accessed as n+1. Then, since the dataset is bound to a datagrid/datview on the UI, the record at position n+1 is displayed for the user to make changes.
    : : :
    : : : Now the problem. The record displayed is not the new one. I've a few different things including sorting the data to force the dataview to place the new record at the end of the dataset where I expect it. This doesn't work. How do I get this to work? An easier approach would be to make my changes to the new record before it is added to the dataset, but that presents problems due to the architecture of the app.
    : : :
    : : :
    : : :
    : : ok, these objects operate on data that is not connected to a data source (initially you need to connect them, but they will not perform changes to a database unless specifically coding the feature). i would recommend a datatable instead. if you add a row to a datatable, changes take place immediately and your datagrid will be updated. if you plan to update a database you must code this also.
    : :
    : Thanks for responding. Correct - the data is not connected to a data source such as as DB; it is kept in a stand-alone dataset. The datagrid has a datatable and the problem is that when I add a row the changes are not taking place immediately. I read that Microsoft recommends making changes, getting the changes into a new dataset, and then merging the changes back to the original dataset. I haven't tried this yet.
    :
    : I'll post the code tomorrow.
    :
    Email me at bmiller1222@gmail.com with the source, im not sure if maybe i am misunderstanding. Anyways, do not do what microsoft recommends, as that approach is quite slow. Just do this when you need to add a row:

    Private Sub AddRow(byref strData1 as string, byref strData2 as string, byref strData3 as string)

    'declare variables
    Dim rData as DataRow

    'create new datarow object
    rData=datatablename.NewRow()

    'set row values
    rData(0)="somedata1"
    rData(1)="somedata2"
    rData(2)="somedata3"

    'add row to datatable
    datatablename.Rows.Add(rData)

    'release resources
    rData=Nothing

    End Sub


    Now when you want to add a row to the datatable, just call this sub and pass the values. If your datagrid's datasource is the datatable, the changes will take place instantly.

  • softrickssoftricks Posts: 24Member
    : I have an issue and a question. My application uses an XML database and is having a problem adding and saving records. A new record is created by getting a new row from the existing dataset and then immediately adding the row to the dataset's table collection and accepting the changes. At this point I would expect the row to have an index/count of n+1; it doesn't and will generate an error if accessed as n+1. Then, since the dataset is bound to a datagrid/datview on the UI, the record at position n+1 is displayed for the user to make changes.
    :
    : Now the problem. The record displayed is not the new one. I've a few different things including sorting the data to force the dataview to place the new record at the end of the dataset where I expect it. This doesn't work. How do I get this to work? An easier approach would be to make my changes to the new record before it is added to the dataset, but that presents problems due to the architecture of the app.
    :
    :
    :
    ---------------------------------------------------------------------
    hi,
    you can find for your request at www.softricks.5u.com

Sign In or Register to comment.