Tuesday, 1 November 2011

LINQ


LINQ is about integrating query operations into the .NET platform in a comprehensive and open manner. It’s also about providing a unified way for you to query across any kind of data that you have in your program, whether it’s relational, objects or XML.
LINQ TO SQL
LINQ to SQL allows .NET developers to write “queries” in their .NET language of choice to retrieve and manipulate data from a SQL Server database.
Here I’m trying to explain LINQ TO SQL by a simple program.Create table as shown  in screenshot.



Add some data to the table as follows.

Create a new windows application and add DataGridView to the Form.
Then add LINQ TO SQL Classes to the project.For this click Project menu and click Add New Item.Then select Data in Categories and LINQ TO SQL Classes in Templates and click Add button.


Then drag the tables from the Server Explorer to the DataClasses1.dbml.



Now we are moving to the code part. Go to the Form Load event of the Form by double clicking the Form. Then add following code.


Run the code.


Monday, 10 October 2011

Crystal Report using C#.NET


This article is about creating Crystal Reports using C#.NET.

Crystal Report is a Reporting Application that can generate reports from various Data Sources like Databases , XML files etc.. The Visual Studio.NET Integrated Development Environment comes with Crystal Reports tools. The Crystal Reports makes it easy to create simple reports.
Here I’m trying to create reports from table Employee.
Create a windows application. Add a button control and crystalReportViewer control to the form. Then our Form will look like this.

 Then add crystal report to the project. For this click Project menu and click add New  Item. Then select Reporting in categories and select CrystalReport in Templates and click add button.



 Then do the following steps.




Add following code to the click event of button.

Run the code by press F5 and click Show button in the form.Our project will look like this.


Thursday, 29 September 2011

Picture Viewer using C#

Here I am trying to create a picture viewer application using C# in Visual studio 2008.Through this application we can view and  can convert images from one type to another.

Create a windows application and name it as PictureViewer.Then add openFileDialog, saveFileDialog,panel, pictureBox,menuStrip and three buttons to the form and name it as I did.Add three menu items and name as Open,Save As and Exit.Then our UI will look like this.


Open
When we click Open in File menu, an openFileDialog will come and we can select the image we want to  open.For this add following code to the click event of Open.


OpenImage() function is shown below.

















Save As
Add the following code to the click event of Save As

Function SaveImage() is shown below.


Function for SaveAsType() is shown below.

Rotate clockwise and anticlockwise
The following is the function for rotating an image.
Add the following code to click event of  Rotate clockwise button.



Add the following code to click event of  Rotate Anticlockwise button.

   
Next
To view the next image in the folder,add the following code to the click event of Next button.
Our project is finished and run the code by pressing F5 and open an image.then our application wil looks like this.




Thursday, 1 September 2011

Validating a textbox control


In this application I’m validating a textbox by allowing only number to enter.
Create a windows application with a textbox control as shown in fig below.







Here what we do is add some code in keyPress event of the textbox control, which  will show a message when we enter a wrong data.Add the following code in the keyPress event of the textbox control









ValidateNumber(e.KeyChar) function is shown below


















Run the project and enter an alphabet,it will show a message box as shown below

Tuesday, 23 August 2011

ADO.Net Insert,Edit and Delete operation in SQL database using C#





 Here I'm creating an application which can Insert,Edit and Delete the data in a SQL database.For this I used Visual studio 2008 and SQL server 2005.


The application consist of two forms,a Main form and a Sub form.When the user clicks the Insert or Edit button in the Main form,which will show the sub form,through that we can insert or edit the data.The inserted or edited data will show on the datagridView in the Main form.
In the database part we have to create a table named "Employee" as shown in fig.

 Add some datas to the table as follows











Next step is to create UI for the application.For this open visual studio2008 and create a new project with the name DB_blog.Then rename the Form1 in the solution explorer by Main.Set the following properties to the form.
MaximizeBox       =  False
startPosition         = centerScreen
Text                     = Main
FormBorderStyle = Fixed3D

Add three buttons control and a datagridview control to the Main form from toolbox.Set the following properties to the controls.

button1                                                                   

 Name = btn_Insert
Text     = Insert
button2                                                                

 Name = btn_Edit
Text     = Edit
button3                                                                       

 Name = btn_Delete
Text     = Delete 
dataGridView1
allowUserToAddrows          = False
allowUserToResizeColumns = False
allowUserToResizeRows      = False
MultiSelect                           = False
ReadOnly                             = True
RowHeadersVisible              = False
selectionMode                      = FullRowSelect

Next step is to bind data source with the datagridview.For this select the datagridview and click the balck arrow on top.Then select choose Data Source and click Add Project Data Source and select the Database and click next.Then do the following steps .




















































Then select the table you want to show in datagridview and click finish.



























Now our UI will like this.
























Now we are going to create Sub form.For this add a new form to the project by clicking project in the menu and click add new item.Then select Windows forms in catagories and Windows form in templates.Then given the name SubForm and click add.Now a a new form will create.Set its Text property to blank.Add two textbox control,two label control and two button control.Set following properties to the controls

label1
Text = Name
label2
Text = Role
textbox1
Name = txt_Name
Modifier = public
textbox2
Name = txt_Role
Modifier = publicbutton1
Name = btn_OK
Text = OK
DialogResult = OK
button2
Name = btn_Cancel

Text =CancelDialogResult = Cancel

Our subForm will looks like this











INSERT
 My idea is to show the sub form,when the user click the Insert button.After entering data in the textboxes,press OK button to save the data in the database.
For this to happen we have to create some codes in the click event of the Insert button in the Main form.To go to click event of the Insert button,double click the Insert button in the design view.Function InsertData is used to saving the data to the database.We need a connection string to connect with the database.To get the connection string easily,open server explorer in visual studio,right click the Data Connections and click Add Connection.Then enter server name and select database and click Ok.Then right click the database and click Properties.Copy the connection string and stored in a string variable.We need this connection string through out the programs,so we declared it as global.Add the following namespace
System.Data.SqlClient



Write the following code in Insert button click event.















InsertData function is showing below.


















Our Insert section is finished.Press F5 to run the code.After clicking Insert button application looks like this.














EDIT
To edit an item,click the Edit button after selecting the row you want to edit.Then the sub form will open with the selected data s.There we edit and click Ok to save.The selected data in the main form is given to the sub form by its constructor.The constructor is shown below



















Add the following code into the click event of the Edit button



























UpdateData function is shown below.

Our Edit section is finished.Run the project and click Edit button,it will looks like this.



 DELETE
To delete an entry,select the row you want to delete and click Delete button.
Add the following code in the click event of Delete button.


DeleteData function is shown below.
















Our project is completed.