Monday 13 May 2013

Creating a Database Table in ASP.NET


Creating a Database Table in ASP.NET

This is a tutorial for beginners who are just starting, and are looking for a simple way to build database tables with Microsoft Visual Studio 2010.

Setup

Open up Visual Studios and select new project, select Web Form Visual C#, and save under whatever name you’d like.

Step 1

In the Solution Explorer, right click on the name of the website. Select Add New ASP.NET Folder from the list and then select App Data folder.

A new folder named App_Data will be created in the Solution Explorer. Right click on the App_Data folder and select Add New Item from the list. Select SQL Server Database from the list. You can name the database any name you want as long as it has the .mdf extension attached to it. 

Click “Add”. 

Once the database is created, select the Server Explorer tab (located at the bottom of the solution explorer window).Under the Data Connections folder, a database file with the name you gave it will appear on the list. Click to expand the list and then right click on the Table Folder. Select Add New Table from the list. We are going to create a mock customer database to establish some of the basic functions of adding properties to a database table. In future tutorials, you will use the database created here. 

For the first column we will create the customer ID. Type the text “CustomerID” under the Column Name in the table. In the column to the right of the column name, select int from the dropdown list. This assigns an integer property to the first column in the table. 

Uncheck the Allow Nulls box. Unchecking the Allow Nulls box requires the user to enter information in the column. In other words, the field cannot be left blank if the row is not set to allow null 

Step 2

We are going to create five more columns of data. In the second row under customerID, type “FirstName”, select varchar(50) for the data type and uncheck the allow nulls box.

“Varchar” is an abbreviation for variable character which is a single character of data such as a single letter or number. The (50) represents the maximum amount of characters that can be entered into a given text field. 


Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

Step 3

In the third row type “LastName” for the column name, give it a data type of varchar(50) and uncheck the allow nulls box. In the fourth row type “AccountStatus” for the column name, give it a data type of varchar(50), and uncheck the allow nulls box. In the fifth row type “MemberSince” for the column name, give it a data type of smalldatetime and uncheck the allow nulls box.

“SmallDateTime” is a property where a date format is set. For example, mm/dd/yyyy is the set format for this property. In the sixth row type “Accountbalance” for the column name, give it a data type of smallmoney and uncheck the allow nulls box. “smallmoney” property displays a monetary value in the database, and can only consist of numeric characters.

Select the first column of the first row (CustumerID), and then go to the column properties list (located under the table). Scroll to Identity Specification and expand the list. To the right of the (is identity) property set the value to Yes. Right click in the CustomerID column where you typed the text and select Set Primary Key from the dropdown list.

The image below is what the finished table should look like:

 
Description: http://v4.dbtutorials.com/AtomsNetwork/Files/122/Tables01.png

Save the table and name it Customers.




Ado.Net Application

Hi Friends ! Today i am going to make a ado.net Application in .NET.In this application if you select one value from comboBox then  all values related to that id will display on the textBox  Fields.This is very useful concept for you ,if  you want to make any Application in  .NET.If you understand this concept then you will relate any problem with this concept when you are making any .NET Application.You can solve big problem very easily.Because all big problem are collection of  some small problems.
If you will follow these steps which is given below then you will become good application Developer in.NET  after few month.You can download whole application from bottom.
Step 1  First open Your MSSql Server->create a student table which has three Fields and insert some values which is given below in image:-
·                     sid
·                     name
·                     age 
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN-xiS6GRt3NuwQkw99FWrLCGEqkBqkUrQ7xWmx3S8U-K3X4IQJuucT218DgYELO-geSVnybxrDO1T9N5rxF5hGxJWsvUfuld6XiPu1N1FF1Ys309BPTIq5ovLvOjaOfigDJIfeVEkdp0s/s1600/ado.net.PNG

Step 2 Now open your visual studio->File->New->project->Windows Form Application->OK->Now make aForm like which is given below:-
In this form i have added some controls:
·                     1 ComboBox
·                     2 textBox
·                     4 lebel
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYdJezo3TpeKmi7Eb8aG_k8iaWpPIL84sx39uP2LCW3pS9Fu5Lbn0noXBvRCSZYJjtKZuF_P780q5vUpWN1ges5orLhMNasIU8kCjZXB39Gq5YzPQ8OER114XxnwtMfvIn5nQBUbFI24Rj/s1600/ad.net+form.PNG


Step 3 Now go the property of comboBox->Click Items->write  all Sid of database here.
see it;
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi7aB5J9dSSicMy6fLMB4Tw5Njmvn95CFYq5o4joa5SJ-T1EbWqHIUSXl9Ke_EU39tT5YaI-QGByKkpZyPxEKdt_0Y7KYTf74h7IVcHyjYUFLi_bl-CUQmgD25Af1vLeikxT-ZJ9wvz5LbF/s1600/ado.net+combox+property.PNG

Step 4 Now Double click on comboBox ->write the following code which is give below->replace existing Ado.net_application namespace with your namespace  if you are creating  a new application otherwise it will error.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace ado.net_application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data source=RAMASHANKER-PC; integrated Security=yes; Database=master");
            con.Open();
            SqlCommand cmd = new SqlCommand("select name,age from student where sid="+comboBox1.SelectedItem.ToString(),con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                textBox1.Text = dr[0].ToString();
                textBox2.Text = dr[1].ToString();
            }
            con.Close();
        }
    }
}
Note:- 
1.           If you import this application after download then need for replace namespace.
2.           Here i have used connected architecture for Database connectivity.Please keep attention on some points which is given below:
·                     Write Data source correct[EX. Data source = Server name(mssql server name)].
·                     Write your Database name correct(By default it is master).
Step 5 Now Run the Application(press F5).
see it:
Description: https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2ppArZiRgmubxkTN1YN8SV8v9s9NOiuiq3wTb5Oc7Lgy1BtEC36GwEVfQbmnRE0NwhV5TLsZGmkgE-dN02A5oFpEJ2EP3cGIQE7v4mtNMbpDLTCOGTynwzrmbzMlWGVPAGLOphtejXvSo/s1600/ado.net+output.PNG


Note :- 
When you will change sid then name and age will change automatically.


2 comments: