Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

how to i pass null value to decimal column in formview

it is a sql server. i am using fromview control ,object datasource ,. it is a 2way binding ie bind("custid")
getting this error
Input string was not in a correct format.
Exception:  is not a valid value for Decimal.]
i get this error before the insert procedure get invoked. where i can default a value like -1 before i get this error

pls help
thanks


0
eoswin
Asked:
eoswin
  • 6
  • 5
1 Solution
 
eoswinAuthor Commented:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        DataObjectTypeName="BizzLayer.ServiceReference.users"
        InsertMethod="InsertUser" OldValuesParameterFormatString="original_{0}"
        SelectMethod="GetUsers" TypeName="BizzLayer.UserDataProvider"
        >
    </asp:ObjectDataSource>
0
 
brutaldevCommented:
Not sure what your code looks like so it will be difficult to assist with a solution there. The error you are getting looks like a SQL error though.

Once again pulling at straws without code to look at but you could wrap the parameter like this:
 
NULLIF(@MyNumber, '')

Open in new window

or double wrap it to get a default value:
 
CONVERT(decimal, ISNULL(NULLIF(@MyNumber, ''), '-1'))

Open in new window


Since you are using a object data source, you could change your object code so that the property that you are binding to is a nullable type (decimal?), then you could simply use MyNumber ?? -1;

If none of this is up your alley then post some of the class code that you are binding to and how you are inserting the record into the DB and I can assist from there.
0
 
brutaldevCommented:
What does the signature of InsertUser look like? Also what is the data type of the property that the binding is made to (with name)?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
eoswinAuthor Commented:
public static void InsertUser(users v_user)
        {
            v_user.created_by = vrefuser.userid;
            context.AddObject("users", v_user);
            context.SaveChanges();
        }

i am using wcf dataservice..

but i get the error,before this fn get invoked
0
 
eoswinAuthor Commented:
the datatype for the custid is decimal
0
 
brutaldevCommented:
Right, kinda makes sense now how this could get to the DB as a string...

Since you can't change the WCF service (maybe) to better deal with incoming data just change your code to deal with it. Set a breakpoint on the line "v_user.created_by = vrefuser.userid;" and check what the value of vrefuser.custid is. Is it a number that you expect? Is it nullable and have a value of null?
0
 
eoswinAuthor Commented:
i just removed the line "v_user.created_by = vrefuser.userid .. still getting the error.

 , the error comes before this procedure get invoked
0
 
brutaldevCommented:
Hold on, don't remove that line. I just wanted you to have a look at what the value is that is getting passed onto the service which then fails to perform the insert.

You more than likely just need to set the custid on the v_user object:
 
public static void InsertUser(users v_user)
{
  v_user.created_by = vrefuser.userid;
  // Make sure you are sending the service something valid.
  v_user.custid = vrefuser.custid;
  context.AddObject("users", v_user);
  context.SaveChanges(); 
}

Open in new window


This could also be that when loading the page you are getting a NULL value (or a string) back from your service and it is trying to bind it to the Decimal custid property. You need to make sure that to and ffrom all data sources you have a valid number. Are you casting to Decimal anywhere or using the Convert functions when getting or setting data on that object?
0
 
eoswinAuthor Commented:
<asp:TextBox ID="custidTextBox" runat="server" MaxLength="10"
                            Text='<%# Bind("custid") %>' />

this is the place exactly getting error... is there anyway. i can bind to -1 instead of null
0
 
eoswinAuthor Commented:
sure..
i changed to
public static void InsertUser(users v_user)
{
  context.AddObject("users", v_user);
  context.SaveChanges();
}

0
 
brutaldevCommented:
OK, so the error is happening on rendering the page, getting there :)

This would be the Bind method getting evaluated. The custid property is a decimal and you must be getting null back from your service. You can go the long route of ensuring that data cannot be null from your data source or you can deterministically handle it.

My sugggestion would be to change your BizzLayer.ServiceReference.users class to support a null value for custid by making the type nullable. Not sure exactly what your code looks like but to support null easily it should look something like this:
 
private decimal? _custid;
public decimal? custid
{
  set
  {
    // Now it can be set to null preventing Bind from throwing that error.
    _custid = value;
  }

  get
  {
    // If the value is null then just return a default of -1.
    return _custid ?? -1;
  }
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now