0

I have a BindingSource like so:

BindingSource.DataSource = DataSet.Tables("TableName")

My BindingSource.PositionChanged event is working great, but stops firing after this following code is run, and I'm not sure why:

'I'm trying to send my updates and then retreive any updates using this (very poorly written, I'm sure) code here.
Using DataAdapter As New MySqlDataAdapter("SELECT * FROM MyGreatTable", MySQLConn)
    Dim myCommandBuilder As New MySqlCommandBuilder(DataAdapter)
    CType(BindingSource.Current, DataRowView)("myTime") = Now
    BindingSource.EndEdit()
    DataAdapter.Update(DataSet.Tables("TableName"))
    DataSet.Tables("TableName").Rows.Clear()
    DataAdapter.Fill(DS.Tables("TableName"))
    myDateTime = Convert.ToDateTime(DataSet.Tables("TableName").Compute("Max(myTime)", Nothing))
End Using

Does anyone know why this is happening?

Edit: Okay so as per Plutonix, I've removed DataSet.Tables("TableName").Rows.Clear(). I'm running into a different error now... enter image description here

lolikols
  • 87
  • 1
  • 5
  • 24
  • 2
    You have `DataSet.Tables("TableName").Rows.Clear()` and `DataAdapter.Fill(DS.Tables("TableName"))`, so two different `DataSets`. I'm guessing that has something to do with your issue. This is why you **NEED** to debug! Don't just read the code because you'll see what you expect to see. Debug the code and look at the DATA as the code executes. – jmcilhinney Apr 27 '18 at 13:17
  • I....don't know how to do that :( but I can definitely google how! – lolikols Apr 27 '18 at 13:23
  • @lolikols Here's a [good start](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-overview). – 41686d6564 stands w. Palestine Apr 27 '18 at 13:33
  • Oh, wow that's really cool! Okay so I created a break point, but I'm not even actually sure what I'm supposed to be looking for. If I'm reading this right, I simply clear my datatable, and then fill it back up again with the DataAdapter. – lolikols Apr 27 '18 at 13:51
  • 1
    Why do you think you need to clear the DataTables/DataSet? – Ňɏssa Pøngjǣrdenlarp Apr 27 '18 at 14:11
  • Hey @Plutonix. Well...so that code the idea is that I add the current time to that table so that I can later reference that time if I need to to check for when it was last edited. It ends the edit and updates the the table with not only the date change, but any other changes that have been made to the bindingsource row. So now...I want to fetch any updates that have been made from other users from the table and fill my DataSet.Tables("TableName") again with the new information. So wouldn't I have to clear that first so that it doesnt like...double up on information? – lolikols Apr 27 '18 at 14:27
  • Not at all. First, I am not sure what you are talking about with the time/date of the last *table* change. That seems rather meaningless - a `LastUpdatedDate` column on the other hand is very very common. If you read the Intellisense summary for `DataAdapter.Fill` you'd see: *Adds or refreshes rows in the....* So, no it wont double up. If you open the Object Browser (see view menu) you can look up all those summaries as well as the overrides – Ňɏssa Pøngjǣrdenlarp Apr 27 '18 at 14:42
  • @Plutonix Ah sorry, that column is a lastupdateddate column. I'm just very bad at wording things. I do have a question though. I've gotten rid of the .clear. But now at the line DataAdapter.Fill(DataSet.Tables("TableName")), I'm getting an exception. I've posted it in the original post. Do you know why this is happening? – lolikols Apr 27 '18 at 14:59
  • A bit of that code is inside out. A DataAdapter is not (usually) something you (re) create over and over. If you take the time to set it up, you want to hold onto the same one so you can `Update` and `Fill` on it all day without writing any SQL. Almost the same with the CommandBuilder - use it at the outset to configure the DA then let it go out of scope but no need to create a new one as long as you hold onto the DA. It is also not clear where that code is, looks like a cellLeave type event – Ňɏssa Pøngjǣrdenlarp Apr 27 '18 at 15:42
  • @Plutonix Ah I see! Alright, I'll do some googling on how to fix that error. And I'll definitely do as you suggest as far as the DataAdapter and CommandBuilder goes. Thank you! – lolikols Apr 27 '18 at 15:46
  • **[Searching values via a datagridview](https://stackoverflow.com/q/33701262/1070452)** – Ňɏssa Pøngjǣrdenlarp Apr 27 '18 at 15:52
  • Thanks @Plutonix! I'm going to give that read through! I really appreciate your help! – lolikols Apr 27 '18 at 16:17

0 Answers0