I have written this article for
beginners. 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.
Implementation:
Create
a database i.e. “Test”. Then create a
table “Student_Info”.
Column Name
|
Datatype
|
Student_id
|
Int(Primary Key. So set Is
Identity=True)
|
Student_Name
|
Varchar(500)
|
Age
|
int
|
Class
|
Varchar(50)
|
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>
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>
ASP.NET code behind File using C#:
In code behind, add following code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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";
}
VB.NET code behind file:
In code behind, add following code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
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"
End Sub
0 comments:
Post a Comment