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..
Finally got it working…. Here is my solution (may not be pretty, but it works).
Then your Controller:
Finally View:
If you have a better solution, I am all ears… or eyes in this case.