7 Replies - 1329 Views - Last Post: 04 December 2011 - 04:58 PM Rate Topic: -----

#1 ShanaValor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-January 11

Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:26 PM

The project is to use a transaction to update something. Since I run a dnd game I used it to update levels of a character. Selecting the character works perfectly fine. Validation of the level works fine. Actually trying to update it gives me the error "Error: conversion from string "" to type Double is invalid". This is getting frustrating because I cannot find the involved string nor do I have a double anywhere in the PROJECT let alone in the code.

I have tried adding a global variable to the transaction form to set the level for update in the update statement, changing the level type and that's about all I could think of. I don't know where to begin looking for the error's source unfortunately.

Involved code for the form is as follows.

Chara.vb:
Public Class Chara
    Private p_charName As String
    Private p_charID As Integer 'unneeded?
    Private p_playerName As String
    Private p_charRace As String
    Private p_charClass As String
    Private p_charAlignment As String
    Private p_charLevel As Integer

    Public Property CharID() As Integer
        Get
            Return p_charID
        End Get
        Set(ByVal value As Integer)
            p_charID = value
        End Set
    End Property

    Public Property CharName() As String
        Get
            Return p_charName
        End Get
        Set(ByVal value As String)
            p_charName = value
        End Set
    End Property

    Public Property CharRace() As String
        Get
            Return p_charRace
        End Get
        Set(ByVal value As String)
            p_charRace = value
        End Set
    End Property

    Public Property CharAlignment() As String
        Get
            Return p_charAlignment
        End Get
        Set(ByVal value As String)
            p_charAlignment = value
        End Set
    End Property

    Public Property PlayerName() As String
        Get
            Return p_playerName
        End Get
        Set(ByVal value As String)
            p_playerName = value
        End Set
    End Property

    Public Property CharClass() As String
        Get
            Return p_charClass
        End Get
        Set(ByVal value As String)
            p_charClass = value
        End Set
    End Property

    Public Property CharLevel() As Integer
        Get
            Return p_charLevel
        End Get
        Set(ByVal value As Integer)
            p_charLevel = value
        End Set
    End Property

End Class


TransactionClass.vb
Imports System.Data.OleDb
Public Class TransactionClass
    Public Shared clevel As Integer
    Public Shared Function getCharName(ByVal charName As String) As Chara
        Dim name As New Chara
        Dim connection As New OleDb.OleDbConnection
        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & CurDir() & "\DndDatabase.mdb"
        Dim selectStatement As String = "select * FROM CharTable WHERE CharName = @CharName"
        Dim selectCommand As New OleDb.OleDbCommand(selectStatement, connection)
        selectCommand.Parameters.AddWithValue("@CharName", charName)

        Try
            connection.Open()
            Dim reader As OleDb.OleDbDataReader = selectCommand.ExecuteReader(Commandbehavior.SingleRow)
            If reader.Read Then
                name.CharName = reader("CharName").ToString
                name.PlayerName = reader("PlayerName").ToString
                name.CharClass = reader("CharClass").ToString
                name.CharLevel = reader("CharLvl").ToString
            Else
                name = Nothing
            End If
            reader.Close()
        Catch ex As OleDbException
            MessageBox.Show("Error:" & ex.Message)
        Finally
            connection.Close()
        End Try

        Return name
    End Function

    Public Shared Function updateLevel(ByVal fromChara As Chara, ByVal level As Integer) As Boolean
        Dim connection As New OleDb.OleDbConnection
        connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " & CurDir() & "\DndDatabase.mdb"
        Dim levelTransaction As OleDb.OleDbTransaction = Nothing
        Dim fromCommand As New OleDb.OleDbCommand
        fromCommand.Connection = connection
        fromCommand.CommandText = "update CharTable Set CharLvl = 'clevel'  where CharName = @CharName and CharLvl = @CharLvl"
        fromCommand.Parameters.AddWithValue("@CharName", fromChara.CharName)
        fromCommand.Parameters.AddWithValue("@CharLvl", clevel)

        Try
            connection.Open()
            levelTransaction = connection.BeginTransaction
            fromCommand.Transaction = levelTransaction
            Dim count As Integer = fromCommand.ExecuteNonQuery
            If count > TransactionForm.CharLvlTextBox.Text Then
                levelTransaction.Commit()
                Return True
            Else
                levelTransaction.Rollback()
                Return False
            End If
        Catch ex As Exception
            If levelTransaction IsNot Nothing Then
                levelTransaction.Rollback()
            End If
            Throw ex
        Finally
            connection.Close()
        End Try

    End Function
End Class


TransactionForm.vb
Imports System.Data.OleDb
Public Class TransactionForm
    Dim fromChara As Chara
    Dim toChara As Chara

    Private Sub CharTableBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.CharTableBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DndDatabaseDataSet)

    End Sub

    Private Sub TransactionForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DndDatabaseDataSet.CharTable' table. You can move, or remove it, as needed.
        Me.CharTableTableAdapter.Fill(Me.DndDatabaseDataSet.CharTable)

    End Sub


    Private Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        TransactionClass.clevel = updateTextBox.Text
        If updateTextBox.TextLength > 0 Then
            If fromChara IsNot Nothing Then
                Dim charLevel As Integer = updateTextBox.Text
                If charLevel > fromChara.CharLevel Then
                    Try
                        If TransactionClass.updateLevel(fromChara, charLevel) Then
                            Dim message As String = "Character level for " & fromChara.CharName & " has been updated to " & fromChara.CharLevel & "."
                            MessageBox.Show(message, "Update complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            Me.ClearControls()
                        End If
                    Catch ex As Exception
                        MessageBox.Show("Error:" & ex.Message)
                    End Try
                Else
                    MessageBox.Show("Update cannot be less than or equal to the current level", "Data Entry Error")
                End If
            Else
                MessageBox.Show("Select a Character", "Data Entry Error")
            End If
        Else
            MessageBox.Show("Enter a level to  update to.")
        End If

    End Sub

    Private Sub ClearControls()
        CharNameTextBox.Text = ""
        PlayerNameTextBox.Text = ""
        CharClassTextBox.Text = ""
        CharLvlTextBox.Text = ""
        updateTextBox.Text = ""
    End Sub

    Private Sub SelectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectButton.Click
        If Validator.IsPresent(CharNameTextBox) Then
            fromChara = TransactionClass.getCharName(CharNameTextBox.Text)
            If fromChara IsNot Nothing Then
                PlayerNameTextBox.Text = fromChara.PlayerName
                CharClassTextBox.Text = fromChara.CharClass
                CharLvlTextBox.Text = fromChara.CharLevel
            Else
                MessageBox.Show("No character found with that name.", "Character not found")
            End If
        End If
    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Error: conversion from string "" to type Double is invalid

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:31 PM

Do you know where the error is? Where is it in your code, that is.
Was This Post Helpful? 0
  • +
  • -

#3 ShanaValor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-January 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:33 PM

Given that it only happens when I click the update button, I'd say within the update button's code or the updatelevel function
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:40 PM

The error could be here:
count > TransactionForm.CharLvlTextBox.Text 

If you don't have any text in the TextBox control, it will attempt to still convert it to a Double, and if the String is "" then it will throw that error. Try putting text into that TextBox and see if it errors out.
Was This Post Helpful? 1
  • +
  • -

#5 ShanaValor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-January 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:47 PM

Thank you, that eliminated that error. Now I just have to figure out why it's not updating.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,060
  • Joined: 02-July 08

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:48 PM

To help catch errors you can use Try/Catch block or set breakpoints and follow the code when you pause and go line by line. You should be using Double.TryParse here for code protection.
Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 02:49 PM

Me.ClearControls()? I'm not too familiar with that function, however it seems strange.
Was This Post Helpful? 0
  • +
  • -

#8 ShanaValor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 17-January 11

Re: Error: conversion from string "" to type Double is invalid

Posted 04 December 2011 - 04:58 PM

the Me.ClearControls() is a custom sub that clears the text boxes
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1