Today I will discuss about how to insert data
into database. Inserting data into database is useful for dynamic websites. Now
a day everyone wants his website should be dynamic so it is very important now
a days. To insert data to database we need database first. I am using SQL
Server 2008. You can use SQL server, Oracle, DB2 etc or any other database to
store your data. In this tutorial I just created a simple web application. I
have not write code for data validation. In my previous application I have discuss
how to validate data using JavaScript and in my next post I will discuss about
data validation using JQuery. Now follow me to create a web application which
will help you to insert data to data base.
Step1:
Open server explorer to your Visual studio
and create a database. Now create a table. I considered that you know how to
create a database and a table into a database. I named my table as
‘customer’.this table contains following columns
Customer_name
|
nvarchar(50)
|
customer_number
|
nvarchar(50)
|
customer_address
|
nvarchar(MAX)
|
Step2:
Open your solution explorer. Right click to
your project and click to ‘Add new item..’ option. Now add a web page. Design
this page as you want. I have design my page three field ‘Customer name,
Contact Number and Address’. I have a taken a level which will a successful
message.
Step3:
Now double click on ‘Add Customer button’ and
write following code.
……………………………………………………………………………………………………………………………………
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void Button1_Click(object
sender, EventArgs e)
{
SqlConnection
con=new SqlConnection("Your data base source");
SqlCommand
cmd = new SqlCommand("insert into Customer
values(@var1,@var2,@var3)", con);
cmd.Parameters.Add("@var1", SqlDbType.NVarChar);
cmd.Parameters.Add("@var2", SqlDbType.NVarChar);
cmd.Parameters.Add("@var3", SqlDbType.NVarChar);
cmd.Parameters["@var1"].Value=cname.Text;
cmd.Parameters["@var2"].Value
= cnumber.Text;
cmd.Parameters["@var3"].Value
= address.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
message.Text = "Successful";
cname.Text = "";
cnumber.Text = "";
address.Text = "";
}
}
Step4:
Add some
name space
using
System.Data;
using
System.Data.SqlClient;
Explanation:
See the
above image. First set your connection string. I have mark 1 where you will add
your connection string. Now edit your SQL query. I have mark 2 where you have
to write your SQL Query. After setting your values you have to open your
connection string. You have to close your connection also. When you will open
your connection string then you have to wright code for execute your SQL
Queries. I have execute my SQL Queries where I have marked 4. After closing
your connection you can write a code to show your successful message. After
insert your data your browser will clear your inserted data from your webpage
and it will show successful message.
Now Run
your project and enjoy.!!!! !!! !!!:)
No comments:
Post a Comment