In this article I have explained how to insert data from asp.net web
page to database. I have used query to insert data to database.
In this article I have created
a webpage and place 3 text box controls and save these values to database. I have
created a database and a table to explain this concept. Then I have created connection
in webconfig file of application. Then using code in code behind file on button
click, saved data to database.
Implementation:
1. Create
a database i.e. “Blog”.
2. Then
create a table “Student_Info”.
CREATE TABLE [dbo].[student_info](
[sudent_id]
[int] IDENTITY(1,1) NOT NULL,
[Student_Name]
[varchar](100) NULL,
[Age] [int] NULL,
[Class]
[varchar](50) NULL,
[is_deleted]
[bit] NULL,
[rec_date] [date] NULL,
)
Column Name
|
Datatype
|
Student_id
|
Int(Primary Key. So set Is
Identity=True)
|
Student_Name
|
Varchar(500)
|
Age
|
int
|
Class
|
Varchar(50)
|
3. Create
Connection: Now create connection in
webcofig file as given below.
<connectionStrings>
<add name="con" connectionString="Data
Source=localhost; Initial Catalog=Blog; Integrated Security=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>
|
4. Now add a webpage in web application. And design page
as given below:
ASP.Net Page design:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<fieldset style="width:400px"><legend><strong>Show Entered value in Label Control</strong></legend>
<div>
<table >
<tr>
<td >
</td>
<td>
</td>
</tr>
<tr>
<td >
name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td >
Age</td>
<td>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td >
Class</td>
<td>
<asp:TextBox ID="txtClass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td >
</td>
<td >
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Submit"
/>
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2" >
<asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div></fieldset>
</form>
</body>
</html>
|
5. ASP.NET code behind
File using C#:
In code behind, follow given steps:
1.
Add following Namespaces:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
|
2.
Now on button click even add
following code:
protected void btnSubmit_Click(object
sender, EventArgs e)
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
if
(con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into student_info(Student_Name, age,class)
values(@name,@age,@class)";
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@age", txtAge.Text);
cmd.Parameters.AddWithValue("@class", txtClass.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
lblShow.Text = "Saved Successfully";
clear_controls();
}
|
3.
Now after this add code to clear code:
public void
clear_controls()
{
txtName.Text = "";
txtAge.Text = "";
txtClass.Text = "";
}
|
VB.NET code behind file:
In code behind, follow given steps:
1.
Add following Namespaces:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
|
2. Now
on button click even add following code:
Protected Sub btnSubmit_Click(sender As
Object, e As EventArgs) Handles
btnSubmit.Click
Dim
con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
If
con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim
cmd As New SqlCommand()
cmd.Connection = con
cmd.CommandText = "insert into student_info(Student_Name, age,class)
values(@name,@age,@class)"
cmd.Parameters.AddWithValue("@name", txtName.Text)
cmd.Parameters.AddWithValue("@age", txtAge.Text)
cmd.Parameters.AddWithValue("@class", txtClass.Text)
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
lblShow.Text = "Saved Successfully"
clear_controls()
End Sub
|
3. Now after this add
code to clear code:
Public Sub clear_controls()
txtName.Text = ""
txtAge.Text = ""
txtClass.Text = ""
End Sub
|
Use
this code creates your own application and check result. If you have any doubt
or any other issue, you can ask by leaving comments in comment section. Hope this
article will help beginners.
Demo: