Welcome To My Blog

This blog contains posts that may benefit ordinary visitors or programmers (particularly .NET programmer).
To view posts related to particular subjects, click the link under Labels.
Showing posts with label VS2005. Show all posts
Showing posts with label VS2005. Show all posts

Tuesday, August 4, 2009

Opening A Web Application From VS2005


Welcome back... In this article I would like to tell how to open a web application that you have copied from somebody else from Visual Studio 2005. I am assuming that you have a running Internet Information Services (IIS) in your system.

Simply follow the steps below:
  1. Copy the folder containing the web application to the \Inetpub\wwwroot folder. (This is the default folder for IIS.)Note: Actually, you may copy the application folder to any folder in any drive but it will be easier if you copy it to the default folder used by IIS.
  2. Open IIS. In IIS, expand the Default website folder until you see the application folder you just copied. (It may appear with yellow folder icon.) Right click the folder name and choose Properties from the context menu that appears. Click the Create button from the dialog box followed by the OK button.

    The yellow folder icon changed to an opened package icon as shown below.
  3. Open Visual Studio 2005. In VS2005, open the File menu and choose Open Web Site. In the Open web site dialog box, click the Local IIS tab and select the web site you want to open followed by the Open button.

Sunday, September 14, 2008

How To Define SQL Parameters In C# (VS2005)

Hi there...

This post is dedicated to programmers (beginners) who want to use SQL statements when coding in C#. Before I continue, let me ask you something. Do you know what a parameter in an SQL statement is?

A parameter is just like a placeholder or variable where you define the location for the actual value in the SQL statement. Since it works like a variable, the value can be changed anytime during the SQL execution. Examples are as follow:

SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;

UPDATE Employees SET FirstName=@FirstName WHERE EmployeeID = @EmployeeID;

DELETE Employees WHERE EmployeeID = @EmployeeID;

INSERT INTO Employees (EmployeeID, FirstName, LastName) VALUES (@EmployeeID, @FirstName, @LastName);

In the above examples, @EmployeeID, @FirstName and @LastName are called parameters. A parameter represents a value that you need to set (or assigned) before you execute the particular SQL statement that contains it.

The question now is how are we going to do it in C#? Let's assume you want to use a SELECT statement in the SqlCommand object.

You got to follow the steps below:
  1. Define an SQL Connection object, example:
    SqlConnection conNorthwind = new SqlConnection("server=localhost;uid=sa;pwd=;database=northwind;");
  2. Define an SQL Command object, example:
    SqlCommand cmdEmployees = new SqlCommand("SELECT * FROM Employees WHERE EmployeeID = @EmployeeID", conNorthwind);
  3. Define a parameter and pass the value from a textbox to it. Then, add it to the SqlCommand parameters collection, example:
    SqlParameter paraEmployeeID = new SqlParameter("@EmployeeID", txtEmployeeID.Text);
    cmdEmployees.Parameters.Add(paraEmployeeID);
  4. Open the SqlConnection, example:
    conNorthwind.Open();
  5. Now you may pass the command object to an SqlDataReader or SqlDataAdapter.
    To pass it to an SqlDataReader, you may do the following:
    SqlDataReader drEmployees = cmdEmployees.ExecuteReader();
    To pass it to an SqlDataAdapter, do the following:
    SqlDataAdapter daEmployees = new SqlDataAdapter(cmdEmployees);
  6. If you pass the command object to an SqlDataReader using the ExecuteReader method, you can display the retrieved rows directly in a GridView (web control) using the following code:
    GridView1.DataSource = drEmployees;
    GridView1.DataBind();
  7. If you pass the command object to an SqlDataAdapter, you have to define a dataset also since a DataAdapter can only work with a DataSet. Assuming that you want to display the result in the same web control (GridView). You may write the following codes:
    Dataset dsNorthwind = new DataSet();
    //"Employees" is just a datatable name. It can be replaced with any name.
    daEmployees.Fill(dsNorthwind, "Employees");
    GridView1.DataSource = dsNorthwind.Tables["Employees"];
    GridView1.DataBind();
  8. Finally, close the connection as follows:
    conNorthwind.Close();
Notes: If you are using an SqlDataAdapter, it is not necessary to open and close the connection explicitly. This is because the SqlDataAdapter can open and close the connection implicitly.