[.net] VB.Net Database Problem

Started by
2 comments, last by TheTroll 15 years, 10 months ago
    
    Public Sub CheckSkills(ByVal usr As User)
        Dim ID As String = FrmSever.PlayersTableAdapter.GetData.Item(usr.UserRow).ID
        If FindSkillRowByOwner(usr.UserName) = -1 Then
            If FindSkillRowByID(ID) = -1 Then
                Dim NewSkill As Server.serverdataDataSet.SkillsRow = FrmSever.ServerdataDataSet.Skills.NewSkillsRow
                NewSkill.ID = ID
                NewSkill.Owner = usr.UserName
                FrmSever.ServerdataDataSet.Skills.AddSkillsRow(NewSkill)
                FrmSever.SkillsTableAdapter.Update(FrmSever.ServerdataDataSet.Skills)
            End If
        End If
        Dim Row As Integer
        Row = FindSkillRowByID(ID)
        If FrmSever.ServerdataDataSet.Skills.Item(Row).MaxStr = Nothing Or FrmSever.ServerdataDataSet.Skills.Item(Row).MaxStr >= 0 Then
            FrmSever.ServerdataDataSet.Skills.Item(Row).MaxStr = ClassData(GetPlayerClass(usr)).BaseStr
            FrmSever.ServerdataDataSet.Skills.Item(Row).Str = ClassData(GetPlayerClass(usr)).BaseStr
        ElseIf FrmSever.ServerdataDataSet.Skills.Item(Row).MaxDef = Nothing Or FrmSever.ServerdataDataSet.Skills.Item(Row).MaxDef >= 0 Then
            FrmSever.ServerdataDataSet.Skills.Item(Row).Def = ClassData(GetPlayerClass(usr)).BaseStr
            FrmSever.ServerdataDataSet.Skills.Item(Row).MaxDef = ClassData(GetPlayerClass(usr)).BaseStr
        End If
    End Sub
That is supposed to check that when a person logs on to the server that all their database information is set up. When I try to log into an account who doesn't have a skills row setup it works fine with the FindSkillsRowByOwner(usr.username) = -1. That works by going through all the rows in the database and seeing if the user owns one of them, and if the user doesn't then i makes it a row. So the row is there but my problem area is this line

 FrmSever.ServerdataDataSet.Skills.Item(Row).MaxStr = Nothing
because it is saying that is DBNull. I made the database in access and it is an integer and I set it to have a default value of 0. When I view the database in access it shows the 0's but when I look at my datagrid on the server interface it shows them as empty. So I am not sure on what to do to fix this, any suggestions would be great.
Advertisement
"Nothing" is not going to be a valid check against the database. If you want to change it to a valid check you want:

If FrmSever.ServerdataDataSet.Skills.Item(Row).MaxStr = DBNull.Value

although if you set it to have a default value of 0 then you shouldn't have to bother checking for a null value.

You might also want to verify that updating the database means you're getting valid data back from the call to FindSkillRowByID.

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

I for when a user logs on and I added a new skilled a new skill before then, it will setup the values in the database to whatever they should be. And then when the user logs on again, its sees the values are setup and leaves them alone. This way all the values are initiated and checked at log in so they don't have to be check later on (database searches take alot of server time, so i figure get it all done at once). Ok so I don't know why it says its not there because the FindSkillRowByOwner is this.



Public Function FindSkillRowByOwner(ByVal Owner As String)
Try
Dim I As Integer = 0
Dim Name As String = FrmSever.SkillsTableAdapter.GetData.Item(I).Owner.ToLower
Do While Name <> Owner.ToLower
I = I + 1
Name = FrmSever.SkillsTableAdapter.GetData.Item(I).Owner.ToLower
Loop
Return I
Catch ex As Exception
Return -1
End Try
End Function



So if the row isn't there then the FrmSever.SkillsTableAdapter.GetData.Item(I).Owner.ToLower line will through an exception sending a value of -1 back. But it is returning 0 because as in my previous post you can see where the row doesn't exist and then it initiates and gives it an ID and Owner. So the row is there but now I got pasted the =nothing problem.



FrmSever.ServerdataDataSet.Skills(Row).Def = ClassData(GetPlayerClass(usr)).BaseDef



So Row has been returned as 0, since it has been verified to exist. BUT! i get an exception saying there is no row 0. So I don't know what is happening, I basically copied the code from my other 3 tables that work just fine using this code.
First of all I almost never code in VB so if I mess up a little syntax cut me some slack. I do know how to work in ADO though.

You are really trying to do a lot of work you don't need to be doing. You are trying to make this a lot harder then it really is.

First you need to make sure that in the Skills table that User is the primary key.

Public Sub CheckSkills(ByVal usr As User)     Dim userSkill As Server.serverdataDataSet.SkillsRow     userSkill = FrmSever.ServerdataDataSet.Skills.Rows.Find(usr)     If userSkill = null then         // add your new skill     Else Then.         // use userSkill to read the data you need.     End ifEnd Sub


That should be much easier to do then what you are doing. It is also a heck of a lot quicker.

theTroll

This topic is closed to new replies.

Advertisement