Using ViewState In ASP.NET

December 21, 2011 Leave a comment

For almost all web application I have developed, I prefer using ViewState other than Session if possible as some of you know. Session will store in memory for hold session (while you are viewing the page in the web application) whereas ViewState will store in memory just for the current page you are viewing and it is depend on you, the developers, for method you prefer.

So to use viewstate, simply you just assign the data to the viewstate by key as below:

ViewState[“key1″]=”Value”;

To Remove keys from ViewState, simply call remove method from ViewState as below:

ViewState.Remove(viewstate key);

To remove all viewstate, simply call clear() method as below:

ViewState.Clear();

To access the data that store in viewstate, simply call it as below:

Object objData=ViewState[viewstate key];//you can convert/cast the data return from viewstate to other datatype if it is compatible.

Note: Data store in ViewState (Session also) must be serializable.

That’s it. Try and test it yourself.

Happy Programming!!!

Advertisements
Categories: ASP.NET Tags:

Passing value to crystal report parameter at runtime

December 21, 2011 2 comments

In this post I will show you to pass value to crystal report parameter at runtime and bind it to crystal report viewer.

Suppose we have crystal report file as show below:

Crystal report parameter field

We create a parameter field called pol_no

Create parameter field dialog

In ASP.NET we create a report document, bind parameters value and show in crystal report viewer

using CrystalDecisions.Shared;

using CrystalDecisions.CrystalReports.Engine;

private void ShowReport()

{

ReportDocument rdoc=new ReportDocument();

rdoc.Load(path to crystal report file here);

//If your report capture data from the back-end, you can set the datasource

rdoc.SetDataSource(Datasource for crystal report file here);

rdoc.SetParameterValue(“pol_no”,”TEST VALUE”);//”pol_no” is the crystal  report parameter field and “TEST VALUE” is the value we want to pass to

//Here because we have a crystal report viewer object we can bind ReportDocument to the report viewer

rptViewer.ReportSource=rdoc;

}

That’s it and also note that you can pass the data to parameter field base on parameter field datatype as shown in the above picture.

Happy Programming!!!

PASSING TABLE VALUE PARAMETER TO SQL STORE PROCEDURE USING ASP.NET

December 21, 2011 Leave a comment

Very often, you will need to insert multiple records in a single round-trip (round-trip refer to the number of concurrent open connection to the database).

In early release of Sql server 2008, you will need to write the code to insert one record at a time or generate insert sql statement for all records.

with SQL server 2008 and later, you can now use table-value parameter to pass to store procedure as normal parameter. This makes us easier to write the code. In this tutorial I used SQL Server 2008 R2 and VS 2010.

Let’s start from our back-end (Database->table: IHCP_TBL_USER_ROLE):

User Name Role_id
borith Admin
borith OPT
borith HR

– Create user defined table datatype

USE [BORITH]
GO
CREATE TYPE [dbo].[IHCP_T_USER_ROLE] AS TABLE(
[USER_NAME] [varchar](20) NOT NULL,
[ROLE_ID] [varchar](10) NOT NULL,
PRIMARY KEY CLUSTERED
(
[USER_NAME] ASC,
[ROLE_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
GO

– Create store procedure to insert the data

USE [BORITH]
GO
CREATE PROCEDURE IHCP_SP_USER_ROLE_ADD
@user_role_data IHCP_T_USER_ROLE READONLY
AS
BEGIN TRAN
BEGIN TRY
INSERT INTO IHCP_TBL_USER_ROLE(USER_NAME,ROLE_ID)
SELECT USER_NAME,ROLE_ID FROM @user_role_data
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK
RAISERROR(‘ERROR OCCURRED!’,16,1)
END CATCH

– In Visual Studio, create a function to insert to data (you can write the code to insert the data whatever as you want)

using System.Data;

using System.Data.SqlClient;

private bool InsertUserRole(DataTable user_role_data)

{

SqlConnection con=null;

SqlCommand cmd=null;

try

{

con=new SqlConnection(connectionString here);

con.open();

cmd=new SqlCommand();

cmd.CommandType=CommandType.StoreProcedure;

cmd.CommandText=”IHCP_SP_USER_ROLE_ADD”;

cmd.Parameters.Add(“@user_role_data”,SqlDbType.Structured).Value=user_role_data;

cmd.Connection=con;

cmd.ExecuteNonQuery();

return true;

}

catch(Exception ex)

{

throw ex;

}

finally

{

cmd.Dispose();//Release memory capture by cmd object

con.Close(); //Close the connection

con.Dispose();//Release memory capture by con object

}

}

Note: user_role_data DataTable we passed via above function must conform to the user defined table datatype (number of column and datatype).

That’s it. Try to test yourself.

Happy programming!!!

Categories: ASP.NET, SQL Server Tags: , ,

Returning multiple table from SQL server using store procedure and asp.net

November 14, 2011 1 comment

Welcome to my post and this is my first post regarding to tutorial.

I am not sure which version of SQL Server and ASP.NEt that support this feature but in this writing I use SQL Server 2008 R2 with Visual Studio 2010 SP1. This scenario is very suite with one-many data relationship retrieval. Let’s start!

Suppose I have a database as follow:

Table TBL_USER
user_name user_pwd
borith borith
Table TBL_USER_ROLE
user_name role
borith ADMIN
borith CS
borith UW
borith SL

To retrieve all data regarding to user “borith”, in old fashion you use:

SELECT * FROM TBL_USER WHERE USER_NAME=’borith’

and if you want to check user’s role you use:

SELECT * FROM TBL_USER_ROLE WHERE USER_NAME=’borith’

Let’s see the features provided by ADO.NET.

//In Visual Studio, create a function that returns DataSet object

using System.Data;

using System.Data.SqlClient;

private DataSet GetUserDataByUserName(string user_name)

{

SqlConnection con=null;

SqlCommand cmd=null;

SqlDataAdapter da=null;

try

{

con=new SqlConnection(connectionString here);

con.open();

cmd=new SqlCommand();

cmd.CommandType=CommandType.StoreProcedure;

cmd.CommandText=”GETUSERDATABYUSERNAME”;

cmd.Parameters.Add(“@user_name”,SqlDbType.Varchar,20).Value=user_name;

cmd.Connection=con;

da=new SqlDataAdapter(cmd);

DataSet ds=new DataSet();

da.Fill(ds);

return ds;

}

catch(Exception ex)

{

throw ex;

}

finally

{

cmd.Dispose();//Release memory capture by cmd object

da.Dispose();//Release memory capture by da object

con.Close(); //Close the connection

con.Dispose();//Release memory capture by con object

}

}

//In SQL Server, create a store procedure

CREATE PROCEDURE GETUSERDATABYUSERNAME

@user_name varchar(20)

AS

BEGIN

SELECT USER_NAME,USER_PWD FROM TBL_USER WHERE USER_NAME=@user_name

SELECT USER_NAME,ROLE FROM TBL_USER_ROLE WHERE USER_NAME=@user_name

END

//To call the function use the following code:

DataSet ds=GetUserDataByUserName(“borith”);

//Note: by default, when returning multiple table, SQL server will named the table name with TABLE1,TABLE2… but you can of course use index if you want and I recommend this method.

//To access the data

DataTable dtUser=ds.Tables[0];

DataTable dtUserRole=ds.Tables[1];

//By using index instead of name you must be sure the order of data return. In this case, the data from TBL_USER comes first

So you can now use the data to check what ever you want.

Happy programming!!!

Why don’t you learn exchange

October 14, 2011 Leave a comment

I used to take an interview with a company. They asked me about system administration, programming and exchange server. So pity am I as at that time I am not familiar with it and I failed for that interview. Start from that I spent around 2 weeks to learn it when I had my own laptop (exchange server 2010 requirement is very high so I have never tested it before). Now I can use it like configure and administration.

Comparing exchange with my mail server I use currently (MailEnable), exchange has so many features than MailEnable has and I do want to implement it for my own but this should be cost so much.

Anyway, I am proud of myself that can learn it so far.

Categories: Exchange Server 2010 Tags:

ធ្វើកូនកម្មវិធី

October 7, 2011 2 comments

គិតយូរឭទៅអាណិតប្រពន្ធណាស់​មួយថ្ងៃឭឃើញអង្គុយតែបូកលុយ។​ធ្វើកូនកម្មវិធីមួយអោយចុះកុំអោយពិបាកទៀត។​ធ្វើដូចខ្ញុំល្អទេ?​

Categories: Learning Tags: , , ,

Migrate to SharePoint

September 6, 2011 1 comment

Very soon my company will migrate local intranet to sharepoint and I, a member in the team who handle this is trying very hard to catch it. So anyone who has experiences with SharePoint, please share me regarding to webpart development and administration.

Categories: SharePoint