Wednesday, February 25, 2009

Refresh DropDownList in server by using ASP.NET

Today I study how to refresh content of a DropDownList (based on user selection of content of another DropDownList). It seems quite easy (no need to use AJAX nor UpdatePanel) and let's summarize procedure as below:

1. Define several DropDownList boxes in a web form, remember to define those control as server control (runat="server").

2. Set "AutoPostBack=true" of those DropDownList boxes.

3. Once content of the ListBox is changed, it will trigger functions of "Page_Load()" and "xxxList_SelectedIndexChanged()".

4. Remember to use "if (!Page.IsPostBack) { ... }" to protect the logic in Page_Load( ).

5. Add logic in "xxxList_SelectedIndexChanged" to refresh content of other DropDownList Box.

Tuesday, February 24, 2009

Usage of User Control for ASP.NET

Today I try something of creating an "User Control" for an ASP.NET based web site, and pass parameter to the control. Below is a summary

1. For an ASP.NET project, select to add “user control” as shown below


2. For the ASCX file, add resource to UI of the control as below


3. For the .CS file related to the user control add proper logic as shown below


4. Drag and drop the “User Control” to a web form which will use the control. Related code as below will be generated automatically
@ Register Src="ctrlCalendar.ascx" TagName="ctrlCalendar" TagPrefix="uc1"

<%@ Register Src="ctrlCalendar.ascx" TagName="ctrlCalendar" TagPrefix="uc1" %>5. Do the following if wants to pass parameter from a web form to the control:
..Add variable such as “public string ButName;” in the control
..Add command as below in the Page_Load of the web form:
CtrlCalendar1.ButName = "From";
CtrlCalendar2.ButName = "To";
..Add command as below in the Page_Load of the user control:
this.Button_1.Value = ButName;

Saturday, February 14, 2009

Sample to use CLR for SQL2005

Below is a summary of steps to create CRL for SQL2005
1. Create a project using Visual Studio 2005 (Create DLL with “User Defined Function” (DLL: SQL_SimpleCLR.dll))
2. Click to add “User-defined function”
3. Build and copy the DLL under “..\binn” of SQL Server
[Sample source code]
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString DispTime(SqlString HashString)
{
// Put your code here
string hashedPasswordString = null;
hashedPasswordString = "Hello";

//return new SqlString("Hello");
return new SqlString(hashedPasswordString);
}
};

4. Execute the following command to register addembly as shown in the following diagram

CREATE ASSEMBLY MyLib FROM
'd:\Microsoft SQL Server\MSSQL.1\MSSQL\binn\SQL_SimpleCLR.dll'
WITH PERMISSION_SET = SAFE

5. Execute the following command to create function associated with the API provided in the class of the assembly (DLL)
CREATE FUNCTION dbo.DispTime
(
@result nvarchar(4000) = ''
)
RETURNS nvarchar(4000)
EXTERNAL NAME [MyLib].[UserDefinedFunctions].[DispTime]


6. Execute the following command to test the function:
sp_configure 'clr enabled', 1
go
reconfigure
go

SELECT dbo.DispTime('mypassword')

Create accounts and assign rights to access SQL

Below is a summary of steps to create account and assign rights to access database (more than 1 database) of SQL Server 2005.

1. Execute commands as below to create login account
CREATE LOGIN deve1
WITH PASSWORD = 'deve1';
USE name_of_dbase1;
CREATE USER deve1 FOR LOGIN deve1;
GO

2. Execute commands as below to assign user account to each database
use name_of_dbase2
exec sp_adduser deve1

use name_of_dbase3

exec sp_adduser deve1

3. Right click name of database, select permission and click to select options as below: