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 C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, February 17, 2009

Using Private Assembly in C# VS2005

This article is intended to beginner/medium level programmer.

Before we continue, let me explain first about the term "assembly" in C#. An assembly is simply a project that you have compiled either into a portable executable or DLL file. If you are using Visual Studio 2005 IDE to create your project, you will notice there are so many project templates that you can choose from. The templates range from Console applications, Windows application, Mobile applications and Web applications.

A project can only be compiled into an EXE assembly if it has the Main method, or else it will be a DLL assembly only.

The assemblies are further categorized into two types - private or shared assemblies. In this article, I will focus on the Private Assembly.

A private assembly may have the extension EXE or DLL. It is termed "private" because the assembly is not shared. Meaning that if you make a reference to a private assembly in your new project, you have to copy the private assembly to the bin folder of your new project. If you are using the VS2005 IDE, copying is done automatically when you Add Reference to the private assembly in the new project. Hence, every time we make a reference to a private assembly, we make a copy of that assembly in the new project.

To understand how to use a private assembly in C#, let we do the following steps to create a class library:
  1. Open VS2005 and create a new project. Make sure you select Visual C# --> Windows --> Class Library.

  2. Named the project as Converter. After a while, the code window appears.

  3. Rename the Class1.cs to Convert.cs in the Solution Explorer window.

  4. Add two methods to the class. One to convert temperature from Celcius to Fahrenheit and another to convert from Fahrenheit to Celcius.
  5. Click the Build menu and choose Build Converter to compile the project.

Assuming that you have created your project in the Visual Studio 2005 default folder, the assembly "Converter.dll" can be found in the folder My Documents --> Visual Studio 2005 --> Projects --> Converter --> bin --> debug/release.

Now let's try to use the created assembly in our new project. Follow the steps below to start a Windows application:

  1. In Visual Studio 2005 IDE, start a new project. From the "New Project" dialog box, select Visual C# --> Windows --> Windows Application template.

  2. Add controls to the form as shown.

  3. Named the control respectively as follows:
    radioButton1 --> Name: rbnCelciusToFahrenheit, Text: Celcius To Fahrenheit
  4. radioButton2 --> Name: rbnFahrenheitToCelcius, Text: Fahrenheit To Celcius
    label1 --> Name: lblInstruction, Text: (blank)
    label2 --> Name: lblResult, Text: (blank)
    textbox1 --> Name: txtTemperature, Text: (blank)
    button1 --> Name: btnCalculate, Text: Calculate

  5. Right click "References" in the Solution Explorer window and select "Add Reference...". Click the Browse tab in the Add Reference dialog box. Navigate to the folder containing "Converter.dll" private assembly that you have created earlier. After clicking OK, you should see the name Converter appears under the References list. If you click the Converter, you notice Copy Local = true in the Properties window.

  6. Double click the btnCalculate. VS2005 IDE takes you to the code window and gives you the event handler named btnCalculate_Click. Add the code in the event handler as follows:

  7. Get back to the Form1 (design). Then, double click the rbnCelciusToFahrenheit and rbnFahrenheitToCelcius radio buttons. You will get the "
    rbnCelciusToFahrenheit_CheckedChanged" and "
    rbnFahrenheitToCelcius_CheckedChanged" respectively. Add codes to the both event handlers as shown:
  8. Now, run your program. Notice that even though the method for converting the temperature is not in the new project, we still be able to execute the code because we have added reference to the Converter assembly.

From the above practice, you see how easy it is to reuse codes that we have done earlier in other project.

Tuesday, September 16, 2008

Using MasterPage Template In ASP.Net 2005 Application

Introduction

A Master Page in Visual Studio 2005 is just like a template. You can place banners, menus, images and etc on the master page. You can also include codes (functions) on the master page. You can include more than one master page in any asp.net 2005 web application.

Once you have the master page, you can generate the content page (aspx page) which is linked to the master page. This way, you can have several aspx pages (content pages) with a consistent look. In other words, all content pages generated using a master page, will have the same appearance as the master page. For example, if you have placed a banner at the top and navigation menu on the left side of the master page, all content pages will also have the banner at the top and menu on the left side.

How To Add Master Page To ASP.Net Project?
Adding a master page to an ASP.NET project is very easy. You just need to do the following steps:
  1. Right-click on the project name in the Solution Explorer window.
  2. Select Add New Item... from the pop up menu.
  3. Select Master Page from the Add New Item dialog box. If necessary, change the name and the language for the master page. Then click the Add button. You will see a master page is displayed either in the Source or Design view.
  4. Design you master page. For example, you can add an HTML table on the master page. After that, you can arrange the the ContentPlaceHolder control, images, buttons and other controls nicely within the HTML table. You may look at the image below for an example.

How To Add Content Page (aspx page) Using A Master Page?
To add a content page to your asp.net project using a particular master page is relatively easy. All you need to do are:
  1. Open the particular master page that you want to use to generate the master page.
  2. Right-click anywhere within the Design or Source view window and select Add Content Page from the pop up menu that appears.
  3. Notice the asp.net content control in the generated aspx page. This is where you place your content such as text, iframe, images and etc for your content page.
Remember that we use master page to generate a consistent aspx page. Though you cannot set a master page as the start page of your website, you still need to include it in your asp.net web site. This is because the page that is generated using the master page has a link to the master page.

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.