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 7047407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:46:03+00:00 2026-05-28T02:46:03+00:00

I am working on an MVC3 project where we are developing one site for

  • 0

I am working on an MVC3 project where we are developing one site for use by multiple companies. Each company has it’s own database catalog. The site log in information is all stored in a single “Master” database and that database contains the catalog name to use for each user. But, those catalogs are slightly different from each other structure wise. What I am trying to do is setup standard models, but bind the data to those models differently based on the catalog for the user.

public class UserSearchEntityLayer
{
    public class SearchOptionsList
    {
        public virtual string SearchOptionText { get; set; }
        public virtual string SearchOptionValue { get; set; }
    }
}


public class UserSearchDBLayer : UserSearchEntityLayer
{
    DbSet<SearchOptionsList> SearchOptions { get; set; }

    public UserSearchDBLayer(string ClientCode)
    {
        //Connection Strings
        var ClientConn = "Data Source=HelloWorld;Initial Catalog=" + ClientCode + ";Integrated Security=True;Persist Security Info=True";

        //Prep Work
        DataSet SearchOptionsDS = new DataSet();
        SqlConnection cn = null;
        SqlDataAdapter cmd = null;
        SqlDataReader dr = null;
        string SQLSelect = string.Empty;

        //Start Work
        try
        {
            cn = new SqlConnection(ClientConn);
            cn.Open();
            switch (ClientCode)
            {
                case "AAG":
                    //SearchOptions
                    SQLSelect = "SELECT [Report_Level] as 'Value',[Report_Level_Name] as 'Text' FROM [MASTER_REPORTING_LEVELS] Order By 'Value' DESC";
                    cmd = new SqlDataAdapter(SQLSelect, cn);
                    cmd.Fill(SearchOptionsDS);
                    if (SearchOptionsDS.Tables.Count != 0)
                    {
                        if (SearchOptionsDS.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow R in SearchOptionsDS.Tables[0].Rows)
                            {
                                SearchOptions.Add(new SearchOptionsList { SearchOptionText = R["Text"].ToString(), SearchOptionValue = R["Value"].ToString() });
                            }
                        }
                    }
                    SQLSelect = string.Empty;
                    SearchOptionsDS.Dispose();
                    cmd.Dispose();
                    break;
                default:
                    //Do more stuff here
                    break;
            }
        }
        catch 
        {
        }
        finally
        {
    SearchOptions.Add(new SearchOptionsList { SearchOptionText = "States", SearchOptionValue = "States" });
            SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Locations", SearchOptionValue = "Locations" });
            SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Levels", SearchOptionValue = "Levels" });
            SearchOptions.Add(new SearchOptionsList { SearchOptionText = "Name", SearchOptionValue = "Name" });
            if ((dr != null))
            {
                if (!dr.IsClosed)
                    dr.Close();
                dr = null;
            }
            if (cn != null)
            {
                if (cn.State != System.Data.ConnectionState.Closed)
                    cn.Close();
                cn.Dispose();
                cn = null;
            }
            if (cmd != null)
            {
                cmd.Dispose();
                cmd = null;
            }
            if (SQLSelect != null)
                SQLSelect = null;
        }
    }
}

What is the best way to go about doing this? Oh and right now this is tossing me an Object error because SearchOptions is null because nothing is in it for the me to add too..

  • 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-28T02:46:04+00:00Added an answer on May 28, 2026 at 2:46 am

    Finally got it working…. Here is my solution (may not be pretty, but it works).

        public class UserSearchDBLayer : UserSearchEntityLayer
    {
        public IEnumerable<SearchOptionsList> SearchOptions { get; set; }
    
        public UserSearchDBLayer(string ClientCode)
        {
            //Connection Strings
            var ClientConn = "Data Source=HelloWorld;Initial Catalog=" + ClientCode + ";Integrated Security=True;Persist Security Info=True";
    
            //Prep Work
            DataSet SearchOptionsDS = new DataSet();
            SqlConnection cn = null;
            SqlDataAdapter cmd = null;
            SqlDataReader dr = null;
            string SQLSelect = string.Empty;
            //Start Work
            var DataBuilderList = new List<SearchOptionsList>();
            try
            {
                cn = new SqlConnection(ClientConn);
                cn.Open();
                switch (ClientCode)
                {
                    case "AAG":
                        //SearchOptions
                        SQLSelect = "SELECT [Report_Level] as 'Value',[Report_Level_Name] as 'Text' FROM [MASTER_REPORTING_LEVELS] Order By 'Value' DESC";
                        cmd = new SqlDataAdapter(SQLSelect, cn);
                        cmd.Fill(SearchOptionsDS);
                        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Region", SearchOptionValue = "0" });
                        if (SearchOptionsDS.Tables.Count != 0)
                        {
                            if (SearchOptionsDS.Tables[0].Rows.Count > 0)
                            {
                                foreach (DataRow R in SearchOptionsDS.Tables[0].Rows)
                                {
                                    DataBuilderList.Add(new SearchOptionsList { SearchOptionText = R["Text"].ToString(), SearchOptionValue = R["Value"].ToString() });
                                }
                            }
                        }
                        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "States", SearchOptionValue = "States" });
                        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Locations", SearchOptionValue = "Locations" });
                        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Levels", SearchOptionValue = "Levels" });
                        DataBuilderList.Add(new SearchOptionsList { SearchOptionText = "Name", SearchOptionValue = "Name" });
                        SQLSelect = string.Empty;
                        SearchOptionsDS.Dispose();
                        cmd.Dispose();
                        break;
                    default:
                        //Cool Stuff
                        break;
                }
            }
            catch
            {
            }
            finally
            {
                SearchOptions = DataBuilderList;
                if ((dr != null))
                {
                    if (!dr.IsClosed)
                        dr.Close();
                    dr = null;
                }
                if (cn != null)
                {
                    if (cn.State != System.Data.ConnectionState.Closed)
                        cn.Close();
                    cn.Dispose();
                    cn = null;
                }
                if (cmd != null)
                {
                    cmd.Dispose();
                    cmd = null;
                }
                if (SQLSelect != null)
                    SQLSelect = null;
            }
        }
    }
    

    Then your Controller:

    public class TestController : Controller
    {
        public UserSearchDBLayer model = new UserSearchDBLayer("AAG");
        //
        // GET: /Test/
    
        public ActionResult Index()
        {
    
            return View(model);
        }
    
    }
    

    Finally View:

    @model PlayGround.Models.UserSearchDBLayer
    
    @{
    Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
    <title>Index</title>
    </head>
    <body>
    
    @Html.ListBox("Test", new SelectList(Model.SearchOptions, "SearchOptionValue", "SearchOptionText"), new { size = "25" })
    
    </body>
    </html>
    

    If you have a better solution, I am all ears… or eyes in this case.

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

Sidebar

Related Questions

I am working over one my student project over .Net4 and MVC3. Project is
I'm working on a web application project using ASP.NET MVC3 and database in SQL
I'm working on a rather large Asp.net MVC3 project and would like to use
I have an MVC3 project I'm working on that has a View with an
I'm working on a solution that has a Core project with a DbContext (named
Scenario: 2 developers working on the same project (VS2010, C#, MVC3, WinXP) on seperate
I am working on a project which adopted ASP.NET MVC3(Razor) tech. Now, I have
I am currently working on a MVC3 project with Razor. I have switchen on
I am currently working on a MVC3 project in C# and VS2010 using a
I'm starting a new project in MVC3 and whant to use Razor view engine.

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.