Sometimes, we wanted to return our query result as datatable for easy handling of data [probably making it as a datasource of a control].
Here’s a simple function:
Public Function GetDataTable(ByVal conStr As String, ByVal qryStr As String) As DataTable
Dim dt As DataTable = New DataTable()
Dim con As SqlConnection = New SqlConnection(conStr)Dim adapter As SqlDataAdapter = New SqlDataAdapter(qryStr, con)
Try
‘fill datatable
adapter.Fill(dt)
Catch ex As SqlException
System.Web.HttpContext.Current.Response.Write(”SQL Error!!!”)
Finally
con.Close()
End Try‘return the data table.
Return dtEnd Function
Then, you can use it like this:
myDataGrid.DataSource = GetDataTable(myDbConnection, myQuery)
myDataGrid.DataBind()
Tags: .Net asp .net tutorial • mssql sample script • vb .net sample code
Scenario:
When an application starts, a new instance of object [class] is being created.
Scenario translated to code:
<%@ Application Language=”C#” %>
<%@ Import Namespace=”MyCustomClasses” %><script runat=”server”>
public CustomClass _myClass = null;
void Application_Start(object sender, EventArgs e)
{
if (_myClass == null)
_myClass = new CustomClass();
}</script>
Problem:
1) How to access this very instance in other classes inside the same Solution?
2) How do you access it from an external Solution?
==================================
1) How to access this very instance in other classes inside the same Solution?
We can have two possible solutions to this problem.
First, we use the Application variable and put our object’s instance to it.
When application starts, we use:
void Application_Start(object sender, EventArgs e)
{
if (_myClass == null)
_myClass = new CustomClass();Application["myClass"] = _myClass;
}
And then we access it as:
(CustomClass) Application["myClass"]
We can also use Singleton Design Pattern where we are hiding the constructor of the class. With this, we can be sure that we are only using a single instance of the class within the scope of our application.
using System;
namespace MyCustomClasses
{
public class CustomClass
{private static CustomClass instance;
private static object sync = new object();private CustomClass()
{
// put your current constructor logic here, you will notcie the constuctor is marked private,
// this will stop this class being able to be instantiated directly.
}// ** only provide one copy of this class for the system, synchronized access
public static CustomClass GetInstance()
{
// handle concurrent access.
lock (sync)
{
if (instance == null)
{
instance = new CustomClass(); // this is were the single instance of the class is created
}
return instance;
}
}
We can then access it as:
CustomerClass.GetInstance()
2) How do you access it from an external Solution?
We can add WebService inside our Application and return the class instance inside a webmethod
Using the first approach above:
[WebMethod]
public CustomClass GetMyClassInstance()
{
return ((CustomClass) Application["myClass"]);
}
Using the second approach:
[WebMethod]
public CustomClass GetMyClassInstance()
{
CustomClass.GetInstance();
}
We can then consume this Web Service from a different Solution and call it as:
CustomClass.MyWebService MyWS = new CustomClass.MyWebService
MyWS.GetMyClassInstance()
Tags: c# sample code
Posted by ikaruga as ASP.NET
To assign a role to a new user, we usually use the following code. We call this on the CreatedUser event of the ASP.NET CreateUserWizard Control:
protected void CreatedUser(object sender, EventArgs e) {
Roles.AddUserToRole(cuwRegister.UserName, “Registered User”);
}
The code worked fine for me but not when I customized the CreateUserWizard Control where I encountered this error:
The parameter ‘username’ must not be empty.
Parameter name: username
For some reason, the cuwRegister.UserName gives an empty string thus the error. So I used the FindControl method to access the UserName textbox:
TextBox userName = (TextBox)cuwRegister.CreateUserStep.ContentTemplateContainer.FindControl(”UserName”);
Roles.AddUserToRole(userName.Text, “Registered User”);
To learn how to customize the CreateUserWizard Control, go to MSDN.
Tags: asp .net tutorial
Posted by ikaruga as ASP.NET, dashCommerce 3.0.1
Some tasks are just too complicated for dynamic query building and/or require a greater level of control. To handle this, SubSonic supports stored procedures. Each stored procedure will produce an equivalent static method in the class defined in the configuration file. By default this is SPs. Each method will have one parameter for each stored procedure parameter and return a StoredProcedure object.
SubSonic.StoredProcedure sp = SPs.CustOrderHist(customerID);
If you have created new stored procedures, read my post on how to:
Add New Stored Procedures to dashCommerce
Now that SubSonic recognizes your custom stored procedures, the next question is how to access/get their return value?
Tags: asp .net tutorial
Posted by ikaruga as ASP.NET, dashCommerce 3.0.1
All stored procedures included in the configuration file (Store/App.config) have an equivalent static method in: Store/Models/Generated/StoredProcedures.cs
When I was new to dashCommerce I didn’t figure this out right away.
If you created new stored procedures, you need to update this SubSonic-generated StoredProcedure class to include your new stored procedures. To do so, modify the configuration file, look for the “includeProcedureList” attribute. Add the names of your new stored procedures in the list. Notice this line:
stripSPText=”dashCommerce_Store_”
Tags: asp .net tutorial
.Net asp .net tutorial asp .net tutorial c# sample code CSS sample script database guide java sample code javascript sample code mssql sample script string reverse in C# t-sql telerik sample code user-defined function vb .net sample code visual studio tutorial
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.