Generally, when the environment permits, I will consolidate all my data access methods into functionally separate classes, for example, admin data access methods will be in the AdminData class. By standardizing on the method signature, you can easily make self updating user controls.
Suppose you have several pick lists to maintain in SQL 2000, or any other data source. Rather than putting several DropDownLists on the page, you can encapsulate that functionality into a user control and reuse it.
Create a method with the following signature in the data access class:
c#------------------------------------
public DataSet UpdateMethodName(string connectionString, DataSet ds, ArrayList additionalParms)
{
//This method will update the database.
}
VB.NET------------------------------------
Public Function UpdateRoleList(ByVal connectionString As String, ByVal ds As DataSet, ByVal additionalParms As ArrayList) As DataSet
'This method will update the database.
End Function
The update method should only require a connection string and the dataset to update. If you need any additional parameters, pass them in as an array list and then handle them as appropriate.
Create the following public properties in your user control, as well as the public properties (not shown) for the connection string and the dataset that will be updated:
c#------------------------------------
public string UpdateClassAndMethodName
{
get
{
if(ViewState["UpdateMethodName"] != null)
{
return (string)ViewState["UpdateMethodName"];
}
else
return "";
}
set
{
ViewState["UpdateMethodName"] = value;
}
}
/*
'Use the below ArrayList to specify additional parameters, then handle them as appropriate.
'Specifying additional properties is optional. Just test for a null ArrayList in the update method, proceed accordingly.
*/
public ArrayList AdditionalUpdateMethodParameters
{
get
{
if(ViewState["AdditionalUpdateMethodParameters"] != null)
{
return (ArrayList)ViewState["AdditionalUpdateMethodParameters"];
}
else
return (ArrayList)null;
}
set
{
ViewState["AdditionalUpdateMethodParameters"] = value;
}
}
VB.NET------------------------------------
Public Property UpdateClassAndMethodName() As String
Get
If Not ViewState("UpdateMethodName") = Nothing Then
Return CStr(ViewState("UpdateMethodName"))
Else
Return ""
End If
End Get
Set(ByVal Value As String)
ViewState("UpdateMethodName") = Value
End Set
End Property
'Use the below ArrayList to specify additional parameters, then handle them as appropriate.
'Specifying additional properties is optional. Just test for a null ArrayList in the update method, proceed accordingly.
Public Property AdditionalUpdateMethodParameters() As ArrayList
Get
If Not ViewState("AdditionalUpdateMethodParameters") = Nothing Then
Return CType(ViewState("AdditionalUpdateMethodParameters"), ArrayList)
Else
Return CType(Nothing, ArrayList)
End If
End Get
Set(ByVal Value As ArrayList)
ViewState("UpdateMethodProperties") = Value
End Set
End Property
Create the following method and call it from the “save“ or “update“ button click event:
c#------------------------------------
private void DataSaveChanges()
{
if(UpdateClassAndMethodName != "")
{
string [] fqName = UpdateClassAndMethodName.Split(new Char[] {'.'});
System.Reflection.Assembly assem;
object obj;
string fqText;
Type updateType;
MethodInfo updateMethod;
assem = System.Reflection.Assembly.GetCallingAssembly();
fqText = assem.FullName.Split(new Char[] {','})[0] + "." + fqName[0];
updateType = System.Type.GetType(fqText);
updateMethod = updateType.GetMethod(fqName[1]);
obj = Activator.CreateInstance(updateType);
Object [] updateParams = new object[3];
updateParams[0] = "Connection String Here";
updateParams[1] = "Data Set Here";
updateParams[2] = AdditionalUpdateMethodParameters;
//Make the call
//You will need to cast this call if the update method's return is not void. for example, the call would be:
//DataSet returnedSet = (DataSet)updateMethod.Invoke(obj, updateParams);
updateMethod.Invoke(obj, updateParams);
}
}
VB.NET------------------------------------
Private Sub DataSaveChanges()
If Not Me.UpdateClassAndMethodName = "" Then
Dim fqName() As String = Me.UpdateClassAndMethodName.Split(New Char() {"."})
Dim obj As Object
Dim assem As System.Reflection.Assembly
Dim fqText As String
Dim updateType As Type
Dim updateMethod As MethodInfo
assem = System.Reflection.Assembly.GetCallingAssembly()
fqText = assem.FullName.Split(New Char() {","})(0) & "." & fqName(0)
updateType = System.Type.GetType(fqText)
updateMethod = updateType.GetMethod(fqName(1))
obj = Activator.CreateInstance(updateType)
Dim updateParams(2) As Object
updateParams(0) = SessionManager.GetSessionManager()
updateParams(1) = cDataSet
updateParams(2) = AdditionalUpdateMethodParameters
//Make the call
//You will need to cast this call if the update method's return is not void. for example, the call would be:
//returnedSet as DataSet = CType(updateMethod.Invoke(obj, updateParams), DataSet)
updateMethod.Invoke(obj, updateParams)
End If
End Sub
To set up the page this control will reside on, build the project and drag the control onto the form, next add the following code to the page load, or to your configuration method for the page:
c#------------------------------------
MyCustomControl myControl = (MyCustomControl)this.FindControl("MyCustomControl1");
myControl.UpdateClassAndMethodName = "ClassName.MethodName";
//set the addtional public properties for the connection string and the dataset.
//No need to set the AdditionalUpdateMethodParameters property, it will be null if not used.
VB.NET------------------------------------
Dim myControl as MyCustomControl = Ctype(Me.FindControl("MyCustomControl1"), MyCustomControl)
myControl.UpdateClassAndMethodName = "ClassName.MethodName"
'set the addtional public properties for the connection string and the dataset.
'No need to set the AdditionalUpdateMethodParameters property, it will be null if not used.
Once you've completed building the control, you will only need to wire up the public properties on the parent form and create the update method. This should save you a lot of time and will give you great joy because you will not have to rewrite the control's code again (maybe just a tweak or two)!
I had all the keywords color coded, but my session timed out... that will teach me to write a long blog in the web editor. :)