What's wrong with this statement???

Last post 05-11-2008 11:22 PM by Bo Chen – MSFT. 5 replies.

Sort Posts:

  • What's wrong with this statement???

    05-06-2008, 3:21 PM
    • Loading...
    • ltrain2015
    • Joined on 11-20-2004, 8:46 PM
    • Posts 10

    cmdSQL.Parameters.Add("@SlitWidth", System.Data.SqlDbType.Decimal).Value = IIf(slitwidth = "", System.DBNull.Value, cdec(slitwidth))

    When I run the application, I get the following error:

    Conversion from string "" to type 'Decimal' is not valid

    I can't see where the problem is with this.. 

    Thanks for any help!

  • Re: What's wrong with this statement???

    05-06-2008, 3:58 PM
    Answer
    • Loading...
    • ramblor
    • Joined on 03-13-2008, 10:03 AM
    • Posts 656

    I think it's because IIf doesn't shortcut the evaluation, i.e. even when slitwidth = "" it still tries to evaluate CDec(slitwidth) thus throwing the exception. You might have to add another line before to convert slitwidth to Nothing if it's an empty string (though I'm not a VB.Net programmer so there may be a better way of fixing it).

    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
  • Re: What's wrong with this statement???

    05-07-2008, 2:48 AM
    Answer
    • Loading...
    • elegantkvc
    • Joined on 07-30-2007, 4:20 AM
    • Posts 72

    hi, 

    If slidwidth is amer string like "hello" you cannto convert into it to decimal anyway..even in the case of using norma If  statement too.

    whats the datatype for slitwidth ?

    Cheers mate

    vijaySmile

    vijay
  • Re: What's wrong with this statement???

    05-08-2008, 12:27 PM
    • Loading...
    • ltrain2015
    • Joined on 11-20-2004, 8:46 PM
    • Posts 10

    elegantkvc:

    hi, 

    If slidwidth is amer string like "hello" you cannto convert into it to decimal anyway..even in the case of using norma If  statement too.

    whats the datatype for slitwidth ?

    Cheers mate

    vijaySmile

     It is a string.

     So how can I get the value in the text box to be used as a parameter in a stored procedure.  The textbox value is a string right?

  • Re: What's wrong with this statement???

    05-09-2008, 2:59 AM
    Answer
    • Loading...
    • elegantkvc
    • Joined on 07-30-2007, 4:20 AM
    • Posts 72

    hi,

     if slitwidth value is a decimal value means,you can try

     

    IIF(Not(slitwidth  = ""),cdec(slitwidth),System.DBNull.Value)

    You cant get the string value as input and compare it using IIf statemtn

     

    vijay
  • Re: What's wrong with this statement???

    05-11-2008, 11:22 PM

    IIF(Not(slitwidth  = ""),cdec(slitwidth),System.DBNull.Value) or IIF(Not(slitwidth  = ""),Convert.ToDecimal(slitwidth),System.DBNull.Value)

    provided that slitwidth is a number-based variable. Otherwise you will get an run-time error(object cannot be cast to decimal).

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (6 items)