Results 1 to 4 of 4

Thread: [RESOLVED] MYSQL data adapter update data table via datagridview from another form

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Location
    Brisbane
    Posts
    5

    Resolved [RESOLVED] MYSQL data adapter update data table via datagridview from another form

    Hi All, my first post as beginner, apologies for any mistakes!

    I have searched a lot for this but cannot get it to work as desired.

    In Form A I can use the update method from datagridview without problems as follows:

    Relative Code Only..

    '//Load DTG

    Try
    con = New MySqlConnection(cs)
    If Not con.State = ConnectionState.Open Then
    con.Open()
    End If
    cmd = New MySqlCommand("SELECT * FROM `" & name & "`", con)
    da2 = New MySqlDataAdapter(cmd)
    dt2 = New DataTable
    da2.Fill(dt2)
    cb2 = New MySqlCommandBuilder(da2)
    datagridview.DataSource = dt2

    Catch ex As Exception
    etc....

    '//Here

    I do a lot of math changes to datagridview from various sources.

    '//Update Database

    (using previewkeydowneventargs, if enter pressed)
    da2.Update(dt2)

    To here it all works fine.



    I then open Form B from Form A (one form on per screen so both visible).
    Then make a number of updates directly to Database that is the source of datagridview on Form A.

    Now I want to reload the datagridview from the database, perform more math changes to the datagridview and
    then use the da2.Update(dt2) method again.

    The difference being that I want to do this from Form B. I almost get it all done however but am getting an error saying:

    System.NullReferenceException: 'Object reference not set to an instance of an object.' da2 was Nothing.
    on the last line of code in sub.

    Silly part is, if I press the button directly on Form A that calls same sub I am getting this error from, it works fine?

    Please can someone explain what I am doing wrong. it appears to be relative to the fact Form A is not in focus but??????

    Note, the sub in question does reload the dtg first using method at the top before changes are made made and finally update method line error.

    Thank you in advance.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: MYSQL data adapter update data table via datagridview from another form

    First welcome to the forums. Secondly, in the future, when posting code, it is highly recommended you use [code][/code] or [highlight=XX][/highlight] tags around any code that is posted. (if you add a language to the highllight in place of the XX, it will color code it too)... it preservs the look and feel of the code, more importantly, the indenting.

    Then make a number of updates directly to Database that is the source of datagridview on Form A.

    Now I want to reload the datagridview from the database, perform more math changes to the datagridview and
    then use the da2.Update(dt2) method again.
    Now for your problem... you're most assuredly going about it the wrong way. Your second form reallly shouldn't be interacting with the database. It should be collecting information from the user. It should then be sent back to the calling form, and then used to update the database there. This is the single source of data rule. You don't show how you're calling the above code, but my guess is that it is on Form A and you'r calling it like this from Form B: FormA.DoSomething .... What's happening is you're calling the default instance of the form in that case... which may or may not be the same instance that is currently being shown/used by the user. This has a few side effects.

    FormA should open FormB.... FormB should then collect hte data and send it back to FormA (or FormA should get it from FormB, depending on how you set it up)... FormA should then add/edit the data in the ddatasource, and .Update the database. Now, because the DGV is bound to that same datasource, updating the DS will update the grid and there's no need ot "relaod" it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2020
    Location
    Brisbane
    Posts
    5

    Re: MYSQL data adapter update data table via datagridview from another form

    Thanks for the advice.

    On a quick side note, does it always take 7 hours for a new thread to be add to this website?
    Is this because I'm in Australia and they are checked or moderated in the US first?


    There are a number of reasons I am doing it this way, but that's not really relative to my question.

    I'm not sure if your answer means the same but I did figure out my problem during those 7 hours.

    In Form A when Opening Form B, I needed to specify me as the owner.
    Code:
    FormB.Owner = Me
    FormB.Show()


    Then in Form B when calling sub in Form A.
    Code:
    Dim f1 As FormA = TryCast(Me.Owner, FormA)
    f1.runUpdateSub()
    Now it works fine.

    Thanks Again.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: MYSQL data adapter update data table via datagridview from another form

    Quote Originally Posted by DimMeAsLearner View Post
    On a quick side note, does it always take 7 hours for a new thread to be add to this website?
    Is this because I'm in Australia and they are checked or moderated in the US first?
    Our automatic spam prevention measures mean that some new members need to wait for their first few posts to be manually approved, and unfortunately the system decided you belong to that group.

    The time it takes for posts to become visible will vary based on unpredictable factors, but thankfully you won't have to put up with it for much longer - after a few more posts you will no longer have to wait.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width