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

  • Home
  • SEARCH
  • 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 4574600
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:56:44+00:00 2026-05-21T19:56:44+00:00

I’m refactoring some code for a colleague. The program in question accesses a SQL

  • 0

I’m refactoring some code for a colleague. The program in question accesses a SQL database and uses stored procedures to pull different datasets. There is a datacontext setup in Visual Studio, so all of these stored procedures can be accessed through the designer.cs file. The results look like they are returned as differently typed objects (ie, StoredProcAResult, StoredProcBResult) etc…

Anyway, I am not allowed to change the SQL database at all, but I can mess with the ASPX and CS files. there are currently three files which perform almost identical functions (displaying a set of data). Each page calls a different stored procedure – so, say ViewNewProducts, ViewAgedProducts, ViewExpiredProducts. The view is identical, so I want to roll these three pages into one.

If I could touch the SQL, I’d rewrite the stored proc to accept parameters and return the proper data. However, I can’t – so I’ve made one ASPX/CS file, have a variable set to the dataset the user needs (New, Aged, or Expired) and now find myself dealing with this line:

Utility.EnhanceGrid(dataBoundItem.Cells, dataSet.salesperson, dataSet.sku);

The problem is setting the dataSet. The original individual files just used something like:

var dataSet = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;

But now I don’t know which stored proc is going to be called until the user selects.

I tried to use a switch statement, but I can’t declare the var as null… I can’t type the variable… is there a way to use a string value to set the type of the var?

I have a workaround with this code, but I hate repeating myself and this feels awfully sloppy…any advice would be appreciated.

switch (usersSelection())
{
    case "New":
        ViewNewProductsResult newResult = (INVENTORY.ViewNewProductsResult)dataBoundItem.DataItem;
        Utility.EnhanceGrid(dataBoundItem.Cells, newResult.salesperson, newResult.sku);
        break;
    case "Aged":
        ViewAgedProductsResult agedResult = (INVENTORY.ViewAgedProductsResult)dataBoundItem.DataItem;
        Utility.EnhanceGrid(dataBoundItem.Cells, agedResult.salesperson, agedResult.sku);
        break;
    case "Expired":
        ViewExpiredProductsResult expiredResult = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;
        Utility.EnhanceGrid(dataBoundItem.Cells, expiredResult.salesperson, expiredResult.sku);
        break;
} 
  • 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-21T19:56:45+00:00Added an answer on May 21, 2026 at 7:56 pm

    If using .NET Framework 4, you can use the dynamic type. So your code would like this:

    dynamic dataSet = null;
    
    switch (usersSelection())
    {
        case "New":
            dataSet = (INVENTORY.ViewNewProductsResult)dataBoundItem.DataItem;
            break;
        case "Aged":
            dataSet = (INVENTORY.ViewAgedProductsResult)dataBoundItem.DataItem;
            break;
        case "Expired":
            dataSet = (INVENTORY.ViewExpiredProductsResult)dataBoundItem.DataItem;
            break;
    } 
    
    if (dataSet != null)
    {
        Utility.EnhanceGrid(dataBoundItem.Cells, dataSet.salesperson, dataSet.sku);
    }
    

    The actual type would be determined at runtime but since each type has salesperson and sku it should work.

    More on dynamic here MSDN Dynamic Type

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

Sidebar

Related Questions

No related questions found

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.