none
Saving data to an underlying table using bound textboxs

    질문

  • Hello,

    I have a form that has 5 textboxes bound to a table in a SQLServerCE dB on it. I used the Datasources window to put them on the form, so I have the navigation and add, delete, and save buttons at the top of the form. The save button's handler has the following code that was automatically generated:

            Me.Validate()
            Me.TblBirdsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.BirdDbDataSet)

    The data on the form are not saved in the table. I tried putting Me.BirdDbDataSet.AcceptChanges() before the Update - it made no difference. I moved it to after, no difference. The handler for the Add button is empty. I'm using VB Express 2010. How do I save a new record to the underlying table?

    Thanks,

    Ralph

    2012년 3월 5일 월요일 오전 6:32

답변

  • Hi Ralph65,
    Thank you for the project. I see.
    In your project, after you save data to the database, the local database file will be copied to Output directory (..\Bin\debug) and the copied database is the one get updated.
    To check it, please do the following things.
    Open the project, add a new record and click save button.
    Select “Show All Files” button on upper left corner of Solution Explorer, and navigate to bin/Debug folder. 
    Double click the TestBirdDB.sdf file and a new connection showed in the Server Explorer windows.
    Find the table,refresh it and click Show Table Data and you will find the modified data.

    If you still have any doubt and concern about this issue, please let me know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • 답변으로 표시됨 Ralph65 2012년 3월 19일 월요일 오후 12:18
    2012년 3월 15일 목요일 오전 3:31
    중재자

모든 응답

  • Hi Ralph65,
    The update code is correct. 
    Could you please try the following things?
    Is there any error message?
    Debug the code, check if the value in the BirdDbDataSet is changed after you call BindingSource.EndEdit() method?
    Check whether the updatecommand of the adapter is correct.
    If everything is ok, could you please give a demo to reproduce this issue?
    In addition, if the database file is added into the project, then the modified file will locate at the output folder (usually the folder is bin\Debug).
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 3월 7일 수요일 오전 2:25
    중재자
  • Hi, Bob,

    There are no error messages. I'm not really sure how to check the BirdDbDatsSet, What I did was, after I clicked the save button,, I stopped the program, then went to the Data Sources pain, right clicked the field I tried to write to, and it was blank. How can I check it with the program running?

    It saves the record if I click the add record button, enter my data then click save. This make no sense to me as the handler for the add record button is empty.

    How can I give a demo?

    Sorry for so many questions.

    Thanks,

    Ralph65

    2012년 3월 7일 수요일 오전 5:36
  • Hi Ralph65,
    To check the BirdDbDatsSet, you need to know how to debug in Visual Studio, see Debugging in Visual Studio and http://blogs.msdn.com/b/jaredpar/archive/2010/02/19/flattening-class-hierarchies-when-debugging-c.aspx.
    It will add the record because the code set BindingNavigator.AddNewItem Property in the designer. When set, the AddNewItem property generates the Click event handler for the Add New button. This event handler calls the AddNewmethod when the Add New button is clicked at run time. For more details, see http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingnavigator.addnewitem.aspx.
    Anyway, Could you please follow the steps below?
    First, rebuild the application.
    Second, run the application, edit a record, and then click save button.
    Third, click View-> Server Explorer, find the data table under the Data Connections, right click the data table, click refresh, and then click show Table data, and check if the record is changed.
    If it doesn’t change, I will write a demo to show how to do this.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 3월 7일 수요일 오전 8:17
    중재자
  • Hi, Bob,

    I set a breakpoint after the EndEdit() method, typed ?BirdDbDataSet.tblBirds.Rows(0).Item(0) in the Immediate window, and got:

    {"There is no row at position 0."}
        _HResult: -2146233080:
        _message: "There is no row at position 0."
        Data: {System.Collections.ListDictionaryInternal}
        HelpLink: Nothing
        HResult: -2146233080
        InnerException: Nothing
        IsTransient: False
        Message: "There is no row at position 0."
        Source: "System.Data"
        StackTrace: "   at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)     at System.Data.DataRowCollection.get_Item(Int32 index)"
    TargetSite: {NodePath GetNodeByIndex(Int32)}

    Ralph


    • 편집됨 Ralph65 2012년 3월 12일 월요일 오전 5:08
    2012년 3월 12일 월요일 오전 5:04
  • Hi, Bob,

    I clicked the add new button, entered data, clicked the save button. Then I went to Data Sources, refreshed the table, clicked Preview data, and there was nothing there.


    Ralph

    2012년 3월 12일 월요일 오전 5:30
  • Hi Ralph65,
    Did you click preview button in the Preview Data dialog?
    I want to ask this again, is the database located in the SQL Server or it is add into the project?
    Anyway, please download the sample here . After add new data, you can find the added data in the Nwind.mdb in the bin\debug folder.
    If this does not help, could you please share your code with us?
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 3월 12일 월요일 오전 6:36
    중재자
  • Hi, Bob

    I built the SQLServerCE dB from within the project, and when asked if I want to copy it into the project, I answered Yes.

    Thank you for the example, but it's in C#, and I do not really know C#. How can I upload the zipped project where you can DL it?

    Thanks,

    Ralph65

    2012년 3월 12일 월요일 오후 10:57
  • Hi Ralph65,
    You can upload it to SkyDrive and don’t forget set Shared with to Everyone(public).
    If you add the database file into the project, you can find the Database in the output folder(by default, it is bin/debug or bin/release). You can find the added data in that Database.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    2012년 3월 14일 수요일 오전 7:22
    중재자
  • Hi, Bob,

    Here's the link to my project.  VB Bound Controls Test

    Thanks,

    Ralph

    2012년 3월 14일 수요일 오후 10:25
  • Hi Ralph65,
    Thank you for the project. I see.
    In your project, after you save data to the database, the local database file will be copied to Output directory (..\Bin\debug) and the copied database is the one get updated.
    To check it, please do the following things.
    Open the project, add a new record and click save button.
    Select “Show All Files” button on upper left corner of Solution Explorer, and navigate to bin/Debug folder. 
    Double click the TestBirdDB.sdf file and a new connection showed in the Server Explorer windows.
    Find the table,refresh it and click Show Table Data and you will find the modified data.

    If you still have any doubt and concern about this issue, please let me know.
    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    • 답변으로 표시됨 Ralph65 2012년 3월 19일 월요일 오후 12:18
    2012년 3월 15일 목요일 오전 3:31
    중재자