Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6898811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:20:37+00:00 2026-05-27T07:20:37+00:00

I work a ton with our IBM i data and I can’t use EF

  • 0

I work a ton with our IBM i data and I can’t use EF to work with that data. So instead I do something similar to the following:

DataTable dt = new DataTable();
using (iDB2Connection conn = new iDB2Connection(_connString))
{
    using (iDB2Command cmd = new iDB2Command(sb.ToString(), conn))
    {
        conn.Open();
        using (iDB2DataAdapter da = new iDB2DataAdapter(cmd)) { da.Fill(dt); }
        conn.Close();
    }
}

From there I can model the data by doing:

var billHistory = from i in dt.AsEnumerable()
    select new BillHistory
    {
        CustomerId = i.Field<int>("CCUSTN"),
        DueDate = Convert.ToDateTime(i.Field<decimal>("CDUEDT").ToString("0000-00-00")),
        Type = i.Field<string>("CTYPE"),
        RunDate = Convert.ToDateTime(i.Field<decimal>("CDTRUN").ToString("0000-00-00")),
        MeterReadDate = Convert.ToDateTime(i.Field<decimal>("CDREAD").ToString("0000-00-00")),
        PreviousReadDate = Convert.ToDateTime(i.Field<decimal>("CPREVD").ToString("0000-00-00")),
        DaysBetweenReads = i.Field<int>("CDAYS"),
        BillSendDate = Convert.ToDateTime(i.Field<decimal>("CDSENT").ToString("0000-00-00")),
        Meter1Reading = i.Field<int>("CMETR1"),
        Meter2Reading = (i.Field<int>("CMETR2") == 0) ? null : i.Field<int?>("CMETR2"),
        ConsumptionInUnits = i.Field<int>("CCONSU"),
        PreviousReading = i.Field<int>("CPREV1"),
        AverageUnitsOver3Months = i.Field<int>("CAVG3M"),
        LastMonthUnits = i.Field<int>("CUNT1M"),
        PercentChange = i.Field<int>("CPCTCH"),
        IsEstimated = (i.Field<string>("CESTIM") == "Y") ? true : false,
        IsFinalBill = (i.Field<string>("CFINAL") == "Y") ? true : false,
        SprinklerLocation = (i.Field<int>("CSPRLO") == 0) ? null : i.Field<int?>("CSPRLO"),
        SprinklerReading = (i.Field<int>("CSPRMETR") == 0) ? null : i.Field<int?>("CSPRMETR"),
        SprinklerConsumptionInUnits = (i.Field<int>("CSPRCONSU") == 0) ? null : i.Field<int?>("CSPRCONSU"),
        SprinklerPreviousReading = (i.Field<int>("CSPRPREV1") == 0) ? null : i.Field<int?>("CSPRPREV1"),
        SprinklerAverageUnitsOver3Months = (i.Field<int>("CSPRAVG3M") == 0) ? null : i.Field<int?>("CSPRAVG3M"),
        SprinklerLastMonthUnits = (i.Field<int>("CSPRUNT1M") == 0) ? null : i.Field<int?>("CSPRUNT1M"),
        SprinklerPercentChange = (i.Field<int>("CSPRPCT") == 0) ? null : i.Field<int?>("CSPRPCT"),
        PreviousBalance = (i.Field<decimal>("CPREVS") == 0) ? null : i.Field<decimal?>("CPREVS"),
        PaymentAdjustment = (i.Field<decimal>("CPAYAD") == 0) ? null : i.Field<decimal?>("CPAYAD"),
        WaterCost = (i.Field<decimal>("CWATER") == 0) ? null : i.Field<decimal?>("CWATER"),
        SewerCost = (i.Field<decimal>("CSEWER") == 0) ? null : i.Field<decimal?>("CSEWER"),
        RefuseCost = (i.Field<decimal>("CREFUS") == 0) ? null : i.Field<decimal?>("CREFUS"),
        LawnSprinklerCost = (i.Field<decimal>("CLAWNS") == 0) ? null : i.Field<decimal?>("CLAWNS"),
        ACCost = (i.Field<decimal>("CAIR") == 0) ? null : i.Field<decimal?>("CAIR"),
        FireLineCost = (i.Field<decimal>("CFIREL") == 0) ? null : i.Field<decimal?>("CFIREL"),
        HydrantCost = (i.Field<decimal>("CHYDRA") == 0) ? null : i.Field<decimal?>("CHYDRA"),
        StormWaterCost = (i.Field<decimal>("CSTORM") == 0) ? null : i.Field<decimal?>("CSTORM"),
        SpecialBillTaxable = (i.Field<decimal>("CSPECT") == 0) ? null : i.Field<decimal?>("CSPECT"),
        SpecialBillNonTaxable = (i.Field<decimal>("CSPECN") == 0) ? null : i.Field<decimal?>("CSPECN"),
        StateTax = (i.Field<decimal>("CSTTAX") == 0) ? null : i.Field<decimal?>("CSTTAX"),
        RefuseTax = (i.Field<decimal>("CRFTAX") == 0) ? null : i.Field<decimal?>("CRFTAX"),
        StateConnectionFee = (i.Field<decimal>("CSCFEE") == 0) ? null : i.Field<decimal?>("CSCFEE"),
        CityTax = (i.Field<decimal>("CCITAX") == 0) ? null : i.Field<decimal?>("CCITAX"),
        ACHCredit = (i.Field<decimal>("CACHCR") == 0) ? null : i.Field<decimal?>("CACHCR"),
        PaperlessCredit = (i.Field<decimal>("CPPLCR") == 0) ? null : i.Field<decimal?>("CPPLCR"),
        OneTimeDescription = (i.Field<string>("C1XDES") == String.Empty) ? null : i.Field<string>("C1XDES"),
        OneTimeCredit = (i.Field<decimal>("C1XAMT") == 0) ? null : i.Field<decimal?>("C1XAMT"),
        LateFee = (i.Field<decimal>("CLATFE") == 0) ? null : i.Field<decimal?>("CLATFE"),
        NSFFee = (i.Field<decimal>("CNSFFE") == 0) ? null : i.Field<decimal?>("CNSFFE"),
        TotalBill = (i.Field<decimal>("TOTAL_BILL") == 0) ? null : i.Field<decimal?>("TOTAL_BILL")
    };

This is exact copy-paste (sorry for the length). The real problem comes about when I have an InvalidCastException, how do I go about finding the problem? I have figured them out slowly in the past, but I am hoping there is a better way.

Here is the exception detail:

System.InvalidCastException was unhandled
  Message=Specified cast is not valid.
  Source=System.Data.DataSetExtensions
  StackTrace:
       at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
       at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
       at SendToInternet.Model.IbmIDatabase.<GetCustomerHistory>b__11(DataRow i) in C:\Documents and Settings\ispa2\My Documents\Visual Studio 2010\Projects\SendToInternet\SendToInternet\Model\IbmIDatabase.cs:line 514
       at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
       at SendToInternet.Controllers.UtilityBillController.ProcessAllUtilityBills() in C:\Documents and Settings\ispa2\My Documents\Visual Studio 2010\Projects\SendToInternet\SendToInternet\Controllers\UtilityBillController.cs:line 131
       at SendToInternet.Program.Main(String[] args) in C:\Documents and Settings\ispa2\My Documents\Visual Studio 2010\Projects\SendToInternet\SendToInternet\Program.cs:line 59
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Line 514 is var billHistory = from i in dt.AsEnumerable() which isn’t useful. Unless the b__11 is, but I still don’t see any problems in or around there.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:20:37+00:00Added an answer on May 27, 2026 at 7:20 am

    If you look at the text of the InvalidCastException it will include a line number inside it. The line number it mentions is the source of the InvalidCastException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a ton of features in our application that can be described very
I have a ton of data that needs to be processed from lab work.
I have a nightly SSIS process that exports a TON of data from an
Ok, let me preface this by saying that I've read the following: Stackoverflow:how can
So for some research work, I need to analyze a ton of raw movement
Work on a support helpdesk. New tickets come in and records are created in
Work on this small test application to learn threading/locking. I have the following code,
At work we are currently still using JUnit 3 to run our tests. We
At work we are being asked to create XML files to pass data to
Our .NET team works on projects for our company that fall into distinct categories.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.