'ASP.NET'에 해당되는 글 3건

  1. 2008.01.25 Server.Transfer Vs. Response.Redirect 1
  2. 2008.01.24 Making Database Connections on ASP.NET 2.0
  3. 2008.01.24 Installating ASP.NET 2.0

Server.Transfer Vs. Response.Redirect

Server.Transfer Vs. Response.Redirect
By Karl Moore

If you read a lot of industry magazines and ASP.NET code samples, you may find that, although the majority use Response.Redirect to send the user to another page, some seem to prefer the rather mysterious-sounding Server.Transfer. So, what's the difference?

Well, Response.Redirect simply sends a message down to the browser, telling it to move to another page. So, you may run code like:

Response.Redirect("WebForm2.aspx")

or

Response.Redirect("http://www.karlmoore.com/")

to send the user to another page.

Server.Transfer is similar in that it sends the user to another page with a statement such as Server.Transfer("WebForm2.aspx"). However, the statement has a number of distinct advantages and disadvantages.

Firstly, transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.

But watch out: because the "transfer" process can work on only those sites running on the server, you can't use Server.Transfer to send the user to an external site. Only Response.Redirect can do that.

Secondly, Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.

That's not all: The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.

For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").

This technique is great for wizard-style input forms split over multiple pages. But there's another thing you'll want to watch out for when using the preserveForm parameter. ASP.NET has a bug whereby, in certain situations, an error will occur when attempting to transfer the form and query string values. You'll find this documented at http://support.microsoft.com/default.aspx?id=kb;en-us;Q316920.

The unofficial solution is to set the enableViewStateMac property to True on the page you'll be transferring to, then set it back to False. This records that you want a definitive False value for this property and resolves the bug.

So, in brief: Response.Redirect simply tells the browser to visit another page. Server.Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.

Top Tip: Don't confuse Server.Transfer with Server.Execute, which executes the page and returns the results. It was useful in the past, but, with ASP.NET, it's been replaced with fresher methods of development. Ignore it.

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book, plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

Making Database Connections on ASP.NET 2.0

0. Prolog : My Suggestion

For making connections to various types of database, I suggest using ODBC driver.



1. SQL Server (Mssql)



1) Using ODBC Driver

1: Check & Modify SQL Native Client Configuration


SQL Server Configuration Manager – SQL Native Client Configuration - Client Protocols – Enabled TCP/IP


2: Installing DSN


Start -
관리 도구 - 데이터 원본(ODBC) - 시스템 DSN 추가 – SQL Server 선택 후 설치

(굳이 추가할 필요가 없으며, 연결을 테스트해보는 수단으로 이용할 수 있다.)


3: Configurating Web.config File
: Connection to Local SQL Server Express Using SQL Server ODBC Driver

<connectionStrings>

    <add name="MssqlConnectionString" connectionString="Driver={SQL Server};Server=HOSTNAME\SQLEXPRESS;Database=DB_NAME;Uid=USERID;Pwd=PASSWORD;option=3" providerName="System.Data.Odbc" />
</connectionStrings>



4: C# Code Sample

using System.Data.Odbc;

string ConnString = ConfigurationManager.ConnectionStrings["MssqlConnectionString"].ConnectionString;

OdbcConnection objcon = new OdbcConnection(ConnString);


objcon.Open();

 

OdbcCommand objcmd = new OdbcCommand();

objcmd.Connection = objcon;

objcmd.CommandText = "SELECT Name FROM users WHERE ID = '" + id + "' AND Password = '" + pwd + "'";

objcmd.CommandType = CommandType.Text;

 

OdbcDataReader objdr = objcmd.ExecuteReader();

if (objdr.HasRows) // Get User's Name

{

    if (objdr.Read())

    {

        string name = objdr.GetString(0);

        StatusLabel.Text = name;

    }

 

    objdr.Close();

}

 

objcon.Close();




2) Using SQL Client


1: Check & Modify SQL Server Network Configuration


SQL Server Configuration Manager – SQL Server 2005 Network Configuration - Protocols for SQLEXPRESS – Enabled TCP/IP or Named Pipes(local) - Restart SQL Server(*)

(*): SQL Server Configuration Manager – SQL Server 2005 Services - Click Right Button on SQL Server - Restart


2: Configurating Web.config File
: Connection to Local SQL Server Express Using SQL Client

<connectionStrings>

    <add name="SqlClientConnectionString" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=False; Initial Catalog=DATABASE_NAME; User ID=USER_ID; Password=PASSWORD" providerName="System.Data.SqlClient" />

</connectionStrings>


 
3: C# Code Sample

using System.Data.SqlClient;

string ConnString = ConfigurationManager.ConnectionStrings["SqlClientConnectionString"].ConnectionString;

SqlConnection objcon = new SqlConnection(ConnString);

 

objcon.Open();

 

SqlCommand objcmd = new SqlCommand();

objcmd.Connection = objcon;

objcmd.CommandText = "SELECT Name FROM users WHERE ID = '" + id + "' AND Password = '" + pwd + "'";

objcmd.CommandType = CommandType.Text;

 

SqlDataReader objdr = objcmd.ExecuteReader();

if (objdr.HasRows) // Get User's Name

{

    if (objdr.Read())

    {

        string name = objdr.GetString(0);

        StatusLabel.Text = name;

    }

 

    objdr.Close();

}

 

objcon.Close();


 



2. MySQL
 


1) Using ODBC Driver


1: Installing MyODBC Driver

Download Link: http://dev.mysql.com/downloads/connector/odbc/3.51.html



2: Installing DSN


Start -
관리 도구 - 데이터 원본(ODBC) - 시스템 DSN 추가 - MySQL ODBC 3,51 Driver 선택 후 설치

(굳이 추가할 필요가 없으며, 연결을 테스트해보는 수단으로 이용할 수 있다.)


3. Configurating Web.config File
(Connection to Remote MySQL Database Server)

<connectionStrings>

    <add name="MysqlConnectionString" connectionString="Driver={MySql ODBC 3.51 Driver};Server=SERVER_ADDRESS;Database=DB_NAME;Uid=USERID;Pwd=PASSWORD;option=3" providerName="System.Data.Odbc" />
</connectionStrings>



4: C# Code Sample
Reference: 5. 1-1) 4: C# Code Sample of SQL Server Connection Using ODBC Driver



2) Using MySQL .Net Connector


1: Make a Bin folder in the root directory of your web site.

Path(ex): C:\inetpub\wwwroot\Bin



2: Download & Install MySQL Connector into Bin folder.

Download Link: http://dev.mysql.com/downloads/connector/net/5.0.html



3: C# Code Sample

using MySql.Data.MySqlClient;

MySqlDataAdapter myDataAdapter;

DataSet myDataSet;

 

MySqlConnection myConnection = new MySqlConnection("server=SERVER_ADDRESS; user id=USERID; password=PASSWORD; database=DATABASE_NAME; pooling=false;");

 

string strSQL = "SELECT ID, Name FROM users;";

 

myDataAdapter = new MySqlDataAdapter(strSQL, myConnection);

myDataSet = new DataSet();

myDataAdapter.Fill(myDataSet, "users");

 

MySQLDataGrid.DataSource = myDataSet;

MySQLDataGrid.DataBind();

Installating ASP.NET 2.0

0. ASP.NET 2.0 Requirements

1) IIS (5.1/6.0)
2) .Net Framework 2.0

3* .Net Framework 2.0 SDK
4* Visual Studio 2005

*: Required to be installed on development machines.




1. Installing IIS (Internet Information Services)



1) IIS 5.1 (for Windows XP)

Control Panel – Add/Remove Programs – Add/Remove Windows Components – Check Internet Information Services – Next/Finish

Installing IIS 5.1

[ Installing IIS 5.1 ]




2) IIS 6.0 (for Windows Server 2003)

Control Panel – Add/Remove Programs – Add/Remove Windows Components – Check Web Application Server – Check IIS & ASP.NET - Next/Finish

제어판 - 프로그램 추가/제거 - Windows 구성 요소 추가/제거 - 응용 프로그램 서버 - 인터넷 정보 서비스(IIS) & ASP.NET 체크 - 확인 후 설치

Installing IIS 6.0

[ Installing IIS 6.0 ]





2. Installing .Net Framework 2.0

Download Link: http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&DisplayLang=en


Service Pack 1 Download Link: http://www.microsoft.com/downloads/details.aspx?FamilyID=79bc3b77-e02c-4ad3-aacf-a7633f706ba5&DisplayLang=en


* For developers, installing .Net Fremwork SDK on the development machine

Download Link: http://www.microsoft.com/downloads/details.aspx?FamilyID=FE6F2099-B7B4-4F47-A244-C96D69C35DEC&displaylang=en

 



3. Configurating ASP.NET 2.0 for IIS



1) IIS 5.1

프로그램 관리도구 인터넷 정보 서비스 기본 웹 사이트 속성 – ASP.NET – ASP.NET 버전 : 2.0 확인

Configurating ASP.NET 2.0 for IIS 5.1

[ Configurating ASP.NET 2.0 for IIS 5.1 ]




2) IIS 6.0

1: 프로그램 - 관리도구 - 인터넷 정보 서비스 관리자 - 웹 서비스 확장 - ASP.NET 2.0 허용

Configurating ASP.NET 2.0 for IIS 6.0 - 1/2

[ Configurating ASP.NET 2.0 for IIS 6.0 - 1/2 ]



2: 프로그램 - 관리도구 - 인터넷 정보 서비스 관리자 - 기본 웹 사이트 속성 - ASP.NET – ASP.NET 버전 : 2.0 확인

Configurating ASP.NET 2.0 for IIS 6.0 - 2/2

[ Configurating ASP.NET 2.0 for IIS 6.0 - 2/2 ]





4. Checking ASP.NET Page Loading

1) Make a Start.aspx file in the root directory of your web site.

2) Load the page on your internet browser.

Location (ex): http://localhost/Start.aspx




* Configurating Default Content Page
프로그램 관리도구 인터넷 정보 서비스 기본 웹 사이트 속성 – 문서 - 기본 문서 사용 - Start.aspx, index.aspx 등 추가

사용자 삽입 이미지

[ Configurating Default Content Page ]

prev 1 next