locked
convert NULL string to Decimal RRS feed

  • Question

  • User644139582 posted

    I tried this

    Dim value As String
    Dim returnValue As Decimal

    returnValue = Convert.ToDecimal(value) 

    But it didn't work because the string value was NULL.

    Help please?!?!?!?!

    Tuesday, March 30, 2010 2:38 PM

Answers

  • User-952121411 posted

     I think you want to inject my code in the following line below:

    pDocumentPrice.Value = IIf(Decimal.Parse(returnValue) = 0, System.DBNull.Value, Decimal.Parse(MyDecimalValue))
    


     

    A couple of assumptions here:

    1. I picked this line of code, because you were talking about decimal and currency, so I believe this the applicable line of code
    2. I assume 'returnValue' is a variable defined somewhere or being passed into the method; as a word of advice - 'returnValue' is not a real good name for a variable (not descriptive), and I would only see that as a half way decent name for a isolated routine, where 'returnValue' was just that; the single value being returned from a function.  A better name might be something like 'DocumentPrice'.

    Lastly, if your the variable named 'returnValue' is already of type 'Decimal' than you do not need the code for 'Decimal.Parse'.  That would only be needed, if your type was a String (i.e. if it was also a Session variable).

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 1, 2010 9:28 AM

All replies

  • User-1360095595 posted

     The MSDN documentation states:

    "A Decimal number equivalent to the value of value.

    -or-

    Zero if value is a null reference (Nothing in Visual Basic)."

    Are you getting zero?

    Tuesday, March 30, 2010 4:21 PM
  • User188494879 posted

    Well always check if the string is null and then do the conversion.

    Somthing like this.

    Dim value As String
    Dim returnValue As Decimal

    If Not(String.IsNullOrEmpty(value))

    returnValue = Convert.ToDecimal(value)

     

    Tuesday, March 30, 2010 4:22 PM
  • User644139582 posted

     Really quick though.....I would like for it to have the option of being NULL if the client leaves it blank. Does that make sense? If I update the database using Management Studio, entering '' for the money field it converts it to 0.0000, instead of NULL. The client really needs to be able to leave the field blank.

    Thanks!!

    Tuesday, March 30, 2010 5:25 PM
  • User-1360095595 posted

    I don't know about VB, I'm a VB dud.  But in C#, you'd use a nullable decimal.  Check if that's available in VB. 

    Tuesday, March 30, 2010 7:09 PM
  • User-952121411 posted

    Yes, here would be an example of checking a parameter value before adding it to a stored procedure, and it will insert NULL into the database if your value is 0.0000 as you indicated:

    Dim cmd As New SqlCommand
    'Buid SqlCommand object with parameters to pass to stored procedure
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "MyStoredProcedureName"
    'If the decimal value is '0' insert a NULL value into the database:
    'The code reads "If the value of MyDecimalValue is zero, then add the 'NULL' parameter, otherwise use the non-zero decimal value provided
    cmd.Parameters.AddWithValue("@MyMoneyField", IIf(MyDecimalValue = 0, System.DBNull.Value, MyDecimalValue))


    Hope this helps! Smile

    Wednesday, March 31, 2010 11:23 AM
  • User644139582 posted

    This looks like what I want. I just need a little help with it.   My code behind looks like this....

    Dim Myconnection As SqlConnectionDim

    MyCommand As SqlCommand

    Dim MySQL As String

     

    Try
                Myconnection = New SqlConnection("Data Source=;Initial Catalog=llcagent;User ID=;Password=")
                MySQL = "INSERT INTO tblStateDocuments (StateDocument, StateID, agencyid, IsPublic, DocumentPrice, DocExt, DocFile, Notes, LocalAgentID) VALUES (@StateDocument, @StateID, @agencyid, @IsPublic, @DocumentPrice, @DocExt, @DocFile, @Notes, @LocalAgentID)"
                MyCommand = New SqlCommand(MySQL, Myconnection)


                Dim pStateDocument As New SqlParameter("@StateDocument", SqlDbType.VarChar, 50)
                Dim pStateID As New SqlParameter("@StateID", SqlDbType.Int)
                Dim pAgencyID As New SqlParameter("@AgencyID", SqlDbType.Int)
                Dim pDocumentPrice As New SqlParameter("@DocumentPrice", SqlDbType.Money)
                Dim pIsPublic As New SqlParameter("@IsPublic", SqlDbType.VarChar, 50)
                Dim pNotes As New SqlParameter("@Notes", SqlDbType.VarChar, 100)
                Dim pDocExt As New SqlParameter("@DocExt", SqlDbType.VarChar, 50)
                Dim pDocFile As New SqlParameter("@DocFile", SqlDbType.VarChar, 50)
                Dim pLocalAgentID As New SqlParameter("@LocalAgentID", SqlDbType.VarChar, 50)


    With MyCommand.Parameters
                    .Add(pStateDocument)
                    .Add(pStateID)
                    .Add(pAgencyID)
                    .Add(pDocumentPrice)
                    .Add(pIsPublic)
                    .Add(pNotes)
                    .Add(pDocExt)
                    .Add(pDocFile)
                    .Add(pLocalAgentID)


                End With

                pStateDocument.Value = Session("documentname")
                pStateID.Value = Session("sid")
                pAgencyID.Value = Session("xsid")
                pDocumentPrice.Value = returnValue
                pIsPublic.Value = VarIsVisible
                pNotes.Value = Session("notes")
                pDocExt.Value = Session("FileExt")
                pDocFile.Value = Session("adjustedfilename")
                pLocalAgentID.Value = Session("LocalAgentID")

                MyCommand.Connection.Open()
                MyCommand.ExecuteNonQuery()
                MyCommand.Connection.Close()

     

    I need to break apart your code for vb huh?

    Wednesday, March 31, 2010 12:33 PM
  • User-952121411 posted

     I think you want to inject my code in the following line below:

    pDocumentPrice.Value = IIf(Decimal.Parse(returnValue) = 0, System.DBNull.Value, Decimal.Parse(MyDecimalValue))
    


     

    A couple of assumptions here:

    1. I picked this line of code, because you were talking about decimal and currency, so I believe this the applicable line of code
    2. I assume 'returnValue' is a variable defined somewhere or being passed into the method; as a word of advice - 'returnValue' is not a real good name for a variable (not descriptive), and I would only see that as a half way decent name for a isolated routine, where 'returnValue' was just that; the single value being returned from a function.  A better name might be something like 'DocumentPrice'.

    Lastly, if your the variable named 'returnValue' is already of type 'Decimal' than you do not need the code for 'Decimal.Parse'.  That would only be needed, if your type was a String (i.e. if it was also a Session variable).

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 1, 2010 9:28 AM
  • User644139582 posted

    I love how you broke this down for me. Very clear and concise. And the best thing is that it does exactly as it should.

    Thank you! 

    Thursday, April 1, 2010 11:37 AM