LITOW - LearnITOnWeb.com
Home
Articles
Videos
Contact Us
Sitemap
Donate
Read Article
Read or Write Comments
ADO.NET with Windows Forms using Visual C#
Tags:
ADO, .NET, Windows, Forms, C#, Microsoft, Tutorial, Article
Posted by:
Dhaval Faria
| Posted on:
Dec 10, 2008
Downloads:
Page 1 of 2
Next ~>
In this tutorial we are going to look at ADO.NET using various application types in .NET, we are going to do it using Windows Forms and ASP.NET.
You will require following software installed in order to use this tutorial.
Software Requirements
1 - Microsoft Visual Studio 2005 or 2008
2 - Microsoft SQL Server 2005/2008 or Microsoft SQL Server 2005/2008 Express
ADO.NET with Windows Forms
Start Visual Studio 2005 or 2008.
1 - Click on Create Project
If you have Visual Studio 2008 you will need to perform 2nd step or else you can skip to 3rd step, 2nd step allows you to select you want to develop application for which .NET Framework version? We are targeting .NET Framework 2.0 in this article.
2 - Select .NET Framework 2.0
3 - Select Visual C# -
you might find Visual C# language not under Other Languages, it depends on what setting you have done for your Visual Studio, you might find it under Other Languages or parent like Visual Basic shown in above screen shot.
4 - Select Windows Forms Application - Windows Forms Applications are Graphical User Interface (GUI) based application.
5 - Keep name WindowsFormsApplication1 or if you want you can change name. No any specific reason why I left the name as it is. If you have some other setting done in your Visual Studio, you might find few more options below it, like location where you want to save this project and solution directory, you can select location where you want to save it.
6 - Click OK Button.
NOTE: I am using Visual Basic Environment setting for my Visual Studio, hence I am not able to see several options. This setting option can be changes from Tools > Import and Export Settings of Visual Studio.
You should be able to see a screen like following:
1 - It's a workspace where you will create user interface of the application.
2 - box most used place, once you put anything in workspace you need to change properties like width, height, and many more. We will use it a lot so play special attention to it.
3 - Toolbox, special weapon tool I would say, from this you can get a lots of things which you put on the workspace which looks sad right now.
4 - Solution Explorer.. it’s like a bag from where we can take out new workspaces, basically it’s a place where we can see list of projects in the solution which is opened and also files inside the project.
5 - - Server Explorer.. which allows us to do various operations like connecting to database server, we can also connect to other server and look at the services running on other server.
If you are not able to see any of above things in your screen, you can get them from View menu of Visual Studio.
First thing that we are going to do is create a database and in database create tables for Students, Subjects and Attendance. To start this, open Server Explorer, it looks like following:
1 - Right click on Data Connections
2 - Select Create New SQL Server Database
3 - Enter SQL Server instance name in the Server Name box, if SQL Server is running on your machine enter (local) or localhost or enter 127.0.0.1, if you have SQL Server Express installed, you will have to enter (local)\SQLEXPRESS or localhost\SQLEXPRESS or 127.0.0.1\SQLEXPRESS
4 - Select Use Windows Authentication
5 - Enter database name as
CollegeDB
6 - Click OK.
When you click on OK button, it will take some time to create database, once database is created you will see your database listed under
Data Connections
as follows:
Now we are going to create Students table, to do this double click on
CollegeDB.dbo
to expand it, and then select Tables, now right click on
Tables
and select
Add New Table
.
What you will be able to see is something like bellow.
1 - First part that you see is made up of Column Name that’s the name for the field, 2nd is Data Type that’s the data type for the field, and 3rd says whether this field allows Null values or not.. meaning blank data or not.
2 - Second part is Column Properties, whatever data field that we create; it has several properties like primary key, etc... so that’s what we can set it in here.
NOTE: we are using Server Explorer to work with Database to just get idea that we can use Visual Studio to do basic operations related to the Database. We could have used SQL Server Management Studio as well... but using Server Explorer will allow us to work just inside Visual Studio and no need to go out from that environment.
Now, create table structure like following:
Column Name
Data Type
Allow Nulls
studId
int
False
studName
nvarchar(255)
False
studDOB
datetime
False
studClass
int
False
studDiv
nvarchar(2)
False
Now, once you are done with above work, select
studId
field, right click on it and select
Set Primary Key
.
Now in the column properties, click on Alphabetical -
in order to see column properties in alphabetical order, now from column properties select Identity Specification, click on + sign which you see on left of it to expand it, below it you will see (Is Identity) set its value from
No
to
Yes
.
By, doing the above task we are making studId field auto-generated, so it will keep incrementing its value by 1 as we keep insert records in it.
Now, press
CTRL+F4
key to close it or click on close button to close it and when u close it will ask you whether you want to save this table or not, click on
Yes
and give it name
Students
and click on
Ok
button to save table in database.
Now, we will be writing code which will retrieve records from the database and show it on the form and also we will write code to add, edit and delete students from the database, which is going to take a reasonable amount of time and good amount of time to understand it, but things will make it much simpler as lot of things are there which are done multiple times.
Downloads:
Page 1 of 2
Next ~>
ADO.NET with Windows Forms using Visual C#
Tags:
ADO, .NET, Windows, Forms, C#, Microsoft, Tutorial, Article
Posted by:
Dhaval Faria
| Posted on:
Dec 10, 2008
Write Comment
Name:
E-mail:
Website:
http://
Comment:
Copyright © 2009 - LearnITOnWeb.com