Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15571

MYSQL data adapter update data table via datagridview from another form

$
0
0
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.

Viewing all articles
Browse latest Browse all 15571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>