Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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)

 

 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

 

:confused:

  • *Experts*
Posted

You have to check for Nothing, as you expected. The syntax is:

If Row Is Nothing Then

 

The IsNull method checks if the column is null but it only works if you have a row to check.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...