I use:
Visual studio 8.0
MySql database
One table:
table :Groups, two fields : groupnb,groupdef
One Datagridview with is link to Mydataset1

Some times a get this MySQL error:

"Connection must be valid and open"

I use first this command :

MyDataset1.Tables("groups").Rows.InsertAt(row, 0)

Then I change the content of some fields in Datagridview1
Then i use this command, see code :

90% of the time goes well,i get no error

How can I avoid getting this error message!

If MyDataset1.GetChanges() Is Nothing Then
            MessageBox.Show("No record are changed")
        Else
            Dim rowsAffected As Integer = myAdapter1.Update(MyDataTable1)
            If rowsAffected = 0 Then
                MessageBox.Show("No record are changed")
            Else
                MessageBox.Show(rowsAffected & " Records are changed")
            End If
End If

Check your Command. You should pass the Connection as an argument. It should look like this:

x = new Command("Select * From x", Connection);

Thanks

Check your Command. You should pass the Connection as an argument. It should look like this:

x = new Command("Select * From x", Connection);

Thanks

No, thats not the problem!!,thanks anyway
I know exactly when the problem occurs!
I now use this code ,exactly in this order
The error message only occurs when I first edit- or delete records and then add a record.
I do it the other way (I first add a record and than edit or delete records) I get no error messages.

myAdapter1.Update(MyDataTable1.Select(Nothing, Nothing, DataViewRowState.Deleted))
myAdapter1.Update(MyDataTable1.Select(Nothing, Nothing, DataViewRowState.ModifiedCurrent))
myAdapter1.Update(MyDataTable1.Select(Nothing, Nothing, DataViewRowState.Added))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.