locked
Updating SQL Database on second form, Please Help!! RRS feed

  • Question

  •  

    Hi all

     

    I am fairly new to vb express edition. I am currently working on a project for baggage handling.

     

    I have an SQL database and 2 forms for my project; one form for departure and one for arrivals.

     

    My SQL database is in Details view in first form (Departure form) and in DataGrid view in second form (Arrivals form)

     

    When i Start Debugging it shows the first form (Departure Form) and i can update the database with no worries, when i press the Arrivals_Button to show the second form (Arrivals Form) the details are all there, but, when i go back to first form again and update it more and then go back to second form, the details are not updated there.

     

    PROBLEM:

    When i run program and i update database in the first form, details i entered do NOT appear in the 2nd form. Even when i press save button on the binding navigator.

     

    Example; when i add new record into the database and press save in first form, it will not show up in the second form until i re-debug my program (by stopping debugging and pressing F5 again).

     

    Details:

    My First Form is the main one, it has a button that i press to open the Second form.

     

    Button code is below :

    Code Snippet

    Private Sub Arrival_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Arrival_Button.Click

     

    SecondForm.Show()

    Me.SendToBack()

    SecondForm.BringToFront()

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    End Sub

     

     

     

    Form 2 Load is:

    Code Snippet

     

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    Me.PassengerTableTableAdapter.Fill(Me.PassengerDatabaseDataSet.PassengerTable)

     

     

     

    Form 1 Save on binding navigator:

     

    Code Snippet

    Private Sub PassengerTableBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PassengerTableBindingNavigatorSaveItem.Click

    Try

    Me.Validate()

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    MsgBox("Update Successful")

    Catch ex As Exception

    MsgBox("update Failed")

    End Try

    End Sub

     

     

     

    I was told to provide as much info as i can in order to get help, so i hope i provided enough information, let me know if you have any further questions.

     

    I would REALLY appreciate any help.

    Thanks in advance

     

    Regards

    Rami Elbeltagi


     

     

     

     

     

    Tuesday, September 4, 2007 9:22 AM

Answers

  • The thing is, your showing form2 then updating from form1 so you wont see the details as its already got the "old" details loaded in form2 when you are showing it.

    remember, dont use default instances like Form1.BringToFront(), be sure to pass a reference/instance of the existing instance of the form/object in question otherwise you will have unexpected results and behavior.

     

    Going back to the original question

     

    you can however you would need to raise an event from form1 so form2 can listen and load the details and populate again

     

    Thing is, you have already loaded form2 and in that formload event? you have already populated/fetched data so when you update data from form1, its not going to automatically update form2 - you need to specifically get the data again, by a button or by some event where form2 will recieve from form1.

    Whilst your form2 is open, it will NOT automatically update data. you need to manually do this.

     

     

    in addition, lets change the logic of the code slightly:

     

    Private Sub Arrival_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Arrival_Button.Click

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    SecondForm.Show()

    SecondForm.Update()

    Me.SendToBack()

    SecondForm.BringToFront()

     

    End Sub

     

     

    Update first, then show :-)

    Tuesday, September 4, 2007 9:14 PM
  •  

    What you said in the first sentence - if you close form2, then reopen it, yes it will get the data again (if on your form2 load event you have code to get the data)

     

    So, as stated previously, implement your code like so.....

     

    'form2:

     

    'global

     

    private mainForm as Form1 = nothing

     

    public sub new (byval originalForm as Form1)

     

    InitializeComponent()

    Me.mainForm = originalForm

     

    'then get your data here, however you are getting data, or on the Form_Load event for form2 - preferrably on the form_load event for form2, get your data

     

    end sub

     

     

     

    from form1:

     

    'update data as you are doing on that button then.....

    Dim secondForm as Form2(Me)

    secondForm.ShowDialog() 'use a modal dialog rather than sending one back and one forward, unless you want to access both forms at the same time, then do that.

     

     

    so now from Form2, to access Form1, use the instance mainForm to access Form1's public properties, methods and events, example:

     

    Me.mainForm.BringToFront()

     

    Does this help?

    Tuesday, September 4, 2007 10:14 PM

All replies

  • Here is a question.

     

    Are you expecting the second form to automatically update the details when the second form is showing? If so - it won't happen, you need to re-fetch the data. Is this what you are doing?

    What happens if you close the second form then re-open it and get the data again - does it show the new data?

     

    Tuesday, September 4, 2007 1:09 PM
  • Hi Ahmed Smile

     

    Yes, I am trying to update details on second form (while openend) when i amend (delete/add/change) details in the first form.

     

    Is that impossible?! Surely there is code to implement closing the whole form and re-opening it.. right?!

     

    I have :

     

    Code Snippet
    Dim SecondForm As New Form2

     

     

    at top of Public class form1

     

    and

    Code Snippet

     

    Private Sub Arrival_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Arrival_Button.Click

    SecondForm.Show()

    SecondForm.Update()

    Me.SendToBack()

    SecondForm.BringToFront()

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    End Sub

     

     

    To show second form when Arrival_Button is pressed.

     

    To go back to 1st form i click on the Departure_Button:

     

    Code Snippet

    Private Sub Departure_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Departure_Button.Click

    Me.SendToBack()

    Form1.BringToFront()

    End Sub

     

     

    So can i update details on second form (while openend) when i amend (delete/add/change) details in the first form?!

     

    I appreciate your help.

     

    Rami

    Tuesday, September 4, 2007 8:58 PM
  • The thing is, your showing form2 then updating from form1 so you wont see the details as its already got the "old" details loaded in form2 when you are showing it.

    remember, dont use default instances like Form1.BringToFront(), be sure to pass a reference/instance of the existing instance of the form/object in question otherwise you will have unexpected results and behavior.

     

    Going back to the original question

     

    you can however you would need to raise an event from form1 so form2 can listen and load the details and populate again

     

    Thing is, you have already loaded form2 and in that formload event? you have already populated/fetched data so when you update data from form1, its not going to automatically update form2 - you need to specifically get the data again, by a button or by some event where form2 will recieve from form1.

    Whilst your form2 is open, it will NOT automatically update data. you need to manually do this.

     

     

    in addition, lets change the logic of the code slightly:

     

    Private Sub Arrival_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Arrival_Button.Click

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    SecondForm.Show()

    SecondForm.Update()

    Me.SendToBack()

    SecondForm.BringToFront()

     

    End Sub

     

     

    Update first, then show :-)

    Tuesday, September 4, 2007 9:14 PM
  •  ahmedilyas wrote:

    remember, dont use default instances like Form1.BringToFront(), be sure to pass a reference/instance of the existing instance of the form/object in question otherwise you will have unexpected results and behavior.

     

     

    Please remember i am pretty new to this and not the best programmer, so How do you do this?! I thought using form1.bringtofront() was all good?! what problems will arise?!

     

     

     

     

     ahmedilyas wrote:

    The thing is, your showing form2 then updating from form1 so you wont see the details as its already got the "old" details loaded in form2 when you are showing it.

     

    Going back to the original question

     

    you can however you would need to raise an event from form1 so form2 can listen and load the details and populate again

     

     

    Please tell me how?!

     

    Kind Regards

    Rami

    Tuesday, September 4, 2007 9:27 PM
  • Sure, lets go through with this :-)

     

    Default instances:

     

    Form1.Show()

     

    you shouldnt do that as you will have unpredicted results. For example, if you have a public variable and you had several instances of that form, and you decide to do:

     

    Form1.MyVariable = 3

     

    it will set ALL the instances of form1's MyVariable value to "3" than whatever their current values were. You don't want that and its bad practice. You should reference the instance you are interacting with. So, in Form2, have a private global variable which will hold form1's instance, and add a constructor to accept that parameter. Example:

     

    'form2:

     

    'global:

     

    Private mainForm as Form1 = Nothing

     

    public sub new (Byval originalForm as Form1)

     

       InitializeComponent() 'this is required

       Me.mainForm = originalForm

       'now access the mainForm from anywhere within this class (Form2): Me.mainForm

     

    end sub

     

     

     

    That's the correct way of doing things. What would be advised is not to have, in this case, both forms shown but make form2 a modal dialog, so the previous form is not accessible until form2 is closed. To do this, from form1:

     

    Dim secondForm as new Form2()

    secondForm.ShowDialog()

     

     

    This would stop form1 from executing until form2 has been closed.

     

     

    Now, in regards to the second portion, you are better to have a button to "refresh" (refetch data) the data, so its re-loaded when the user presses the button.

     

    Does this help?

    Tuesday, September 4, 2007 9:37 PM
  • Its helping Smile

     

    What if i close form 2 everytime i look at form1, would that work?! or will i get unpredicted results?!

     

    So what im thinking is having (in 2nd form):

     

    Code Snippet

    Private Sub Departure_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Departure_Button.Click

    Me.Close()

    'Me.SendToBack()

    Form1.BringToFront()

    End Sub

     

     

     

    And this in first form:

     

    Code Snippet

    Private Sub Arrival_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Arrival_Button.Click

    Dim SecondForm As New Form2

    Me.PassengerTableBindingSource.EndEdit()

    Me.PassengerTableTableAdapter.Update(Me.PassengerDatabaseDataSet.PassengerTable)

    SecondForm.Show()

    SecondForm.Update()

    Me.SendToBack()

    SecondForm.BringToFront()

    End Sub

     

     

     

    Now how would i implement that so it doesn't give unpredicted results?!

    Tuesday, September 4, 2007 9:58 PM
  •  

    What you said in the first sentence - if you close form2, then reopen it, yes it will get the data again (if on your form2 load event you have code to get the data)

     

    So, as stated previously, implement your code like so.....

     

    'form2:

     

    'global

     

    private mainForm as Form1 = nothing

     

    public sub new (byval originalForm as Form1)

     

    InitializeComponent()

    Me.mainForm = originalForm

     

    'then get your data here, however you are getting data, or on the Form_Load event for form2 - preferrably on the form_load event for form2, get your data

     

    end sub

     

     

     

    from form1:

     

    'update data as you are doing on that button then.....

    Dim secondForm as Form2(Me)

    secondForm.ShowDialog() 'use a modal dialog rather than sending one back and one forward, unless you want to access both forms at the same time, then do that.

     

     

    so now from Form2, to access Form1, use the instance mainForm to access Form1's public properties, methods and events, example:

     

    Me.mainForm.BringToFront()

     

    Does this help?

    Tuesday, September 4, 2007 10:14 PM
  • Yes thank you very much Smile much appreciated.

     

    Rami

     

    Tuesday, September 4, 2007 10:31 PM
  • anytime, glad I could help in some way :-)

     

    Tuesday, September 4, 2007 10:37 PM