Help with Activator.CreateInstance for DB Provider
I followed the Activator.CreateInstance method but I can't get this core piece to work. I have a MSAccess db provider for testing purposes but keep getting a compiling error. Can someone help me as to what I'm doing wrong?
namespace Company.DAL
{
public abstract class ConfigProvider : DataAccess
{
static private ConfigProvider _instance = null;
static public ConfigProvider Instance
{
get
{
if (_instance == null)
_instance = (ConfigProvider)Activator.CreateInstance(
Type.GetType(Company.DAL.MdbClient.MdbConfig));
return _instance;
}
}
public abstract string GetConfigValue(ConfigXML site, string id, string name);
}
}
Then in the "interchangable" data access layer I have this:
namespace Company.DAL.MdbClient
{
public class MdbConfig : ConfigProvider
{
public override string GetConfigValue(ConfigXML site, string id, string name)
{
try
{
string connString = WebConfigurationManager.ConnectionStrings[site.SettingsTable.ConnectionStringName].ConnectionString;
string sqlCmd = "SELECT Value FROM " + site.SettingsTable.TableName + " WHERE UCASE(id)='" + id.ToUpper() + "' AND UCASE(name)='" + name.ToUpper() + "'";
using (OleDbConnection conn = new OleDbConnection(connString))
{
OleDbCommand cmd = new OleDbCommand(sqlCmd, conn);
conn.Open();
object obj = cmd.ExecuteScalar();
if (obj.Equals(System.DBNull.Value))
return string.Empty;
else
return (string)obj;
}
}
catch
{
return string.Empty;
}
}
}
}
Then in my aspx page I'm calling the method like this:
Response.Write(ConfigProvider.Instance.GetConfigValue(domain, "company", "Address1"));
But I get this error:
Compiler Error Message: CS0119: 'Company.DAL.MdbClient.MdbConfig' is a 'type', which is not valid in the given context
Source Error:
Line 21: if (_instance == null)
Line 22: _instance = (ConfigProvider)Activator.CreateInstance(
Line 23: Type.GetType(Company.DAL.MdbClient.MdbConfig));
Line 24: return _instance;
Line 25: }
Answers (1)
0
write this in codebehind. This reads the data from the database but u can use the readXml method of the dataset and popluate.
Public Function PopulateTitle() As DataSet
Dim cn As New SqlConnection
Dim daPricing As SqlDataAdapter
Dim dsPricing As New DataSet
Dim TempList As New DropDownList
Dim myRow As DataRow
cn = New SqlClient.SqlConnection("ur connection string")
cn.Open()
daPricing = New SqlDataAdapter("SELECT TITLE, TITLE_ID FROM XYZ ORDER BY TITLE", cn)
daPricing.Fill(dsPricing)
PopulateTitle = dsPricing
dsPricing.Dispose()
daPricing.Dispose()
cn.Close()
End Function
write the following in .aspx
<asp:TemplateColumn HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" Text='<%# DataBinder.Eval(Container.DataItem, "TITLE") %>' Runat="server" Width="176px" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlTitle" runat="server" DataSource="<%# PopulateTitle %>" DataTextField="TITLE" DataValueField="SERVICE_ID" OnPreRender="setTitle" Width="176px">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
Method which is highlighted is as follows
Public Sub setTitle(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ddlTitle As DropDownList
ddlTitle = sender
If (Not ddlTitle Is Nothing And strServiceTitle <> "") Then
ddlTitle.ClearSelection()
ddlTitle.Items.FindByText(strServiceTitle).Selected = True
End If
End Sub
strServiceTitle is:
Public Sub edit(ByVal sender As System.Object, ByVal e As DataGridCommandEventArgs)strServiceTitle = CType(e.Item.FindControl("lbltitle"), Label).Text
End Sub

0
Firstly you will have to use the asp:TemplateColumn
Use the itemdatabound event of the datagrid
In this evnet you will have to check for the ItemType if it is ListItemType.EditItem
then you can populate the control of that particular cell.
Hope this helps :)
Anuj