Monday, August 17, 2009

LINQ to Objects Example

With this example you will learn how to write the simple link query to find some data in a simple string array of object.

First create Windows Form Application give any project name. Here I will give the project name as LINQtoObject. Then Design the Form using one Button and One textbox like in the picture below.



Now Do the following coding part



How this code work

First step is to reference the System.Linq namespace, which is done automatically by Visual Studio 2008 when you create project.

Next step is to create some data, which is done in this tutorial declaring the a array named names.

Next part of the program is writing the LINQ query.

var queryResults =from n in names where n.StartsWith(“S”) select n;

This is very similar to SQL commands. Isn't it? So if you are familiar with SQL this will very easy to understand for you.

Now Click on the debug button and click the Run button .The you will see the following result inside the textbox.

Query Results:
================
Sandaruwan
Sameera
Sampath
Sanjeewa
Sachith

Understanding the Coding part


Above LINQ query has four parts. The result variable declaration beginning with var , which is assigned using a query expression consisting of the from clause, the where clause, and the select cause. Let's look at each of these part one by one.

Declaring a Variable for result using var Keyword.

The LINQ query start by declaring a variable to hold the results of the query, which is usually done by declaring a variable with var keyword:

var queryResult=

Specify Data Source: from Clause


The next part of the LINQ query is the from clause, Which specifies the data you are querying.

from n in names

Specify Condition: where Clause

In this part of the LINQ query you specify the condition for your query using the where clause. Which look like this:

where n.StartsWith(“S”)

here I specify that the name string starts with the letter S, But you could specify anything else about the instead. For example , a length greater than 10( where n.Length>10) or containing a Q (where n.Contains("Q")).

Select Items: select Clause

Finally , the select clause specifies which items appear in the result set. The select clause look like this.

select n

Thursday, June 25, 2009

Create Your first Crystal Report Application with C#

Here we are going to create a new Crystal Reports in C# from Employee table in the Sample Database(You can make your own database in SQL sever for this). The Employee Table has four fields (EmpID,EmpName,Post,Address) and we are showing the whole data from Employee table to the C# - Crystal Reports project.

In this application we are going to do three task.
1.Create a Crystal report.
2.Generate a PDF document for the relevant report.
3.Generate an Excel document for the relevant report.

Since I want to show you every steps of developing task I uploaded all the documents to the rapidshare.com.You can download it and read the article.

Download Link :
Link 1 .CristalReport.rar
Link 2 .CristalReport.rar

Create PDF File in C#

A pdf application. Convert the content of the TextBox control into a PDF file. That's it.Here You should add the iTextSharp.dll file to your application.If you search the google you can freely download that dll file.

To add the iTextSharp.dll file you can write click on you project->Add Reference->Browse and then you can select the file from your hard drive.


Here is the interface for the application



Here is the code part for the Application



to view clear image please click on the picture.

Sunday, May 10, 2009

Data Retrieval with stored procedure

Introduction
Data Retrieval with stored procedures is the same (surprise!) as if using standard SQL. You can wrap a DataAdapter around the Command object or you can use a DataReader to fetch the data one row at a time.In this example I used the SqlDataReader.

Page Design


SQL Coding Part



C# coding for Button Event

Saturday, May 9, 2009

How to write Stored procedure to Your Data Insert Application

Introduction:

In This article, you will understand what a stored procedure is and how to call one from SQL Sever Database.

Why Use Stored procedure?

There are several advantages of using stored procedures instead of standard SQL.

1. Stored procedures allow a lot more flexibility offering capabilities such as conditional Logic.

2. Because stored procedure are stored within the DBMS, bandwidth and execution time are reduced.This is because a single stored procedure can execute a complex set of SQL statements.

3. Sql server pre-compiles stored procedures such that they execute optimally.

4. Client developer are abstracted from complex designs.They would simply need to know that stored procedure's name and the type of fata it returns.


Here I used ASP.NET application for demonstrate this tutorial.Also this is same for the C# desktop application too.

Creating Stored procedure:

First You have to create Table name called StudentInfo and insert stored procedure for that.You can see the SQL coding for that in below.


CREATE PROCEDURE [dbo].[tblStudentInfo] - In this code, we are telling SQL Server to create a new stored procedure with the name tblStudentInfo.We Specify the body of the stored procedure after this coding part.

Calling Stored procedure:
Create a new ASP.NET Page like in below and Double click on button and do the following coding.




Coding part


After completing the coding part you can build and Run you project.If you did the all the thing correctly you can insert the data to Table via stored proocedure.

ASP.NET Form with MS SQL database

In this tutorial I'm going to show you how to insert the data to SQL database in ASP.NET.It is very similar to the C# desktop application.You can see web form below .It has four fields.When I click insert button i want to insert the form data into table called StudentInfo.



In here also, I did the all the coding part for the insert button.

Friday, May 8, 2009

Working with DatagridView and SQL Database

This lesson will show you how to display data using .NET DataGridView control, C#.NET.The DataGridView control is a powerful tool and is simple to implement.

First you will need to import the System.Data.SqlClient namespace.The System.Data.Sqlclient namespace contain the sqlCommand and SqlConnection classes that we need in order to connect to our database and to send SQL command to it.

First we will look at our Simple Form design.


Here we will do the coding only for the button click event and here is the coding part for the Button click event.



And Here is final result.