Hi, You guys/gals are my only hope here. Please stay with me here I am fairly new to .NET and Databases
I am trying to take some game data that lies on a file on the web and update it into an MySQL database. The first part is complete and I have all the player data in an instance of the PlayerData class.
I have an open DB connection that connects and fills the datatable for the dataset and works good.
I had then planned on Loop around each Player object, firstly using the row.find to see if a entry with the primary key of the users name already exists in the datatable.
If it was null return I wanted to add a new datarow to the datatable, or if it returned an existing row to then edit that data instead.
However it works to update an existing row, but throws and "Object Reference not set to an instance of an object" error if the row doesnt exist in the DataTable.
My Questions are:
1) Why Is This Happening and how can I get around it?
I think its because I am checking for row.isnull when the object=nothing, but .NET wont accept If Row = Nothing).
2) Is this the correct way?
If not how is a better way to do this with Minimal code.
Here is the sample of my code :-(
(dont laugh I am a beginner)
I am trying to take some game data that lies on a file on the web and update it into an MySQL database. The first part is complete and I have all the player data in an instance of the PlayerData class.
I have an open DB connection that connects and fills the datatable for the dataset and works good.
I had then planned on Loop around each Player object, firstly using the row.find to see if a entry with the primary key of the users name already exists in the datatable.
If it was null return I wanted to add a new datarow to the datatable, or if it returned an existing row to then edit that data instead.
However it works to update an existing row, but throws and "Object Reference not set to an instance of an object" error if the row doesnt exist in the DataTable.
My Questions are:
1) Why Is This Happening and how can I get around it?
I think its because I am checking for row.isnull when the object=nothing, but .NET wont accept If Row = Nothing).
2) Is this the correct way?
If not how is a better way to do this with Minimal code.
Here is the sample of my code :-(
(dont laugh I am a beginner)
Code:
Dim Player As PlayerData
Set Database Fill Details
Dim Hyperiums As New DataSet
Dim dbAdp As New OdbcDataAdapter("", Me.Connection)
dbAdp.SelectCommand.CommandText = "SELECT * From PlayerData"
Generate Basic SQL Statements
Dim CommandBuilder As Odbc.OdbcCommandBuilder = New OdbcCommandBuilder(dbAdp)
dbAdp.DeleteCommand = CommandBuilder.GetDeleteCommand
dbAdp.InsertCommand = CommandBuilder.GetInsertCommand
dbAdp.UpdateCommand = CommandBuilder.GetUpdateCommand
Get Current Data From DB
dbAdp.Fill(Hyperiums, "PlayerData")
Dim Row As DataRow
Dim PK(0) As DataColumn
Dim Update As Boolean Stores wether an Update or Insert
With Hyperiums.Tables("PlayerData")
Set Primary Keys
PK(0) = .Columns.Item("Name")
.PrimaryKey = PK
For Each Player In PlayerObj
Row = .Rows.Find(Player.Name) Check For Primary Key Match
If Row.IsNull("Name") Then
Update = False Set That It Is An New Addition To The Data
Row = .NewRow
Else
Update = True Set That It Is An Update To Existing Data
Row.BeginEdit()
End If
Set Row Values
Row.Item("Name") = Player.Name
Row.Item("InfluenceRank") = Player.InfluenceRank
Row.Item("InfluenceScore") = Player.InfluenceScore
Row.Item("HypRank") = Player.HypRank
Row.Item("IDRRank") = Player.IDRRank
Row.Item("IDRSCore") = Player.IDRScore
If Update = True Then
Row.EndEdit()
Else
.Rows.Add(Row)
End If
Next Player
End With
dbAdp.Update(Hyperiums, "PlayerData")
dbAdp = Nothing
End Sub