Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Specified cast is not valid
Message
From
23/04/2016 05:17:29
 
 
To
22/04/2016 15:03:02
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01635259
Message ID:
01635306
Views:
51
Likes (1)
>Hi,
>
>I've inherited an ASP.NET application which makes use of DataSets and TableAdapters for displaying/editing data.
>
>I've added in 2 new columns to a table, with all the vales in these columns being null. I've adjusted the DataTable and TableAdpaters accordingly.
>
>Now when I try to access a web page which uses this table, but does not display either of these new fields, I get this error:
>
>
Exception Details: System.InvalidCastException: Specified cast is not valid.
>
>Source Error: 
>
>
>Line 11323:                get {
>Line 11324:                    try {
>Line 11325:                        return ((decimal)(this[this.tablePNRs.ChargeColumn]));
>Line 11326:                    }
>Line 11327:                    catch (global::System.InvalidCastException e) {
>
>Source File: D:\Development\VS2010\RESREPRICER_asp4\RESREPRICERDataSet.Designer.cs    Line: 11325 
>
>Stack Trace: 
>
>
>[InvalidCastException: Specified cast is not valid.]
>   RESREPRICER.Web.PNRsRow.get_Charge() in D:\Development\VS2010\RESREPRICER_asp4\RESREPRICERDataSet.Designer.cs:11325
>
>[StrongTypingException: The value for column 'Charge' in table 'PNRs' is DBNull.]
>   RESREPRICER.Web.PNRsRow.get_Charge() in D:\Development\VS2010\RESREPRICER_asp4\RESREPRICERDataSet.Designer.cs:11328
>   RESREPRICER.Web.PrepareInvoicesEdit.butCreate_Click(Object sender, EventArgs e) in D:\Development\VS2010\RESREPRICER_asp4\PrepareInvoicesEdit.aspx.cs:217
>   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628614
>   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
>   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
>   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
>   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
>   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
>
>What do I need to do to fix this?
>
>This will also lead me on to other issues with passing in nulls to the insert an dupdate of this data, but I'll leave that alone until I understand what's going on here.

In the designer select the 'Charge' column and 'Properties'. Make sure AllowDBNull is True. Check the NullValue - the default is ' Throw Exeception' (which is what you are seeing). Change this to either 'Null' or 'Empty' depending on your requirements. 'Null' will convert DBNull to C# null ; 'Empty' will convert to the default empty value (0 for decimal).
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform