MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

ADO.NET

ADO.NET Projects

ADO.NET Project 1

ADO.NET Examples

Examples

Insert a Data in Database using Procedure in ASP .NET
Previous Home Next

Step 1: First creating a Stored Procedure using SQL Server. Procedure name as AddEmpDetails

CREATE PROCEDURE  AddEmpDetails  @name nchar(10),
  @id nchar(10), @emp_age nchar(10)
AS
BEGIN
INSERT INTO emp_tbl(nme,age,emp_id) 
  VALUES (@name,@emp_age,@id)
END

Step 2: Design the page. Take a Grid View Control to show data and three textboxes and a button.

<%@PageLanguage="C#"AutoEventWireup="true"
Codevirtual="Default.aspx.cs"Inherits="_Default"%>
<%@Registerassembly="AjaxControlToolkit"
namespace="AjaxControlToolkit"tagprefix="cc1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0
Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>UntitledPage</title>
</head>
<body><formid="form1"runat="server">
<div><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"
BorderStyle="Solid">
<RowStyleBackColor="White"BorderColor="#333333"BorderStyle="Solid"
BorderWidth="2px"/>
<Columns><asp:BoundFieldDataField="nme"HeaderText="Name"/>
<asp:BoundFieldDataField="age"HeaderText="Age"/>
<asp:BoundFieldDataField="emp_id"HeaderText="EmployeID"/>
</Columns><HeaderStyleBackColor="#CCCCCC"BorderStyle="Inset"BorderWidth="2px"/>
</asp:GridView></div>
<br/><br/><table>
<tr><td>EnterID:</td><td>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox></td></tr>
<tr><td>EnterName:</td><td>
<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox></td></tr>
<tr><td>EnterAge:</td><td>
<asp:TextBoxID="TextBox3"runat="server"></asp:TextBox></td></tr>
<tr><td>
<asp:ButtonID="Button1"runat="server"Text="Add"onclick="Button1_Click"/>
</td></tr>
</table>
</form>
</body>
</html>

 

Step 3:Default.aspx.cs Code.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection newConnection = new 
SqlConnection(@"Data Source=R4R-3EFD13BB468\SQLEXPRESS; 
initial catalog=newDatabase;integrated security=true;");
protected void Page_Load(object sender, EventArgs e)
{
newConnection.Open();
SqlCommand cmdd = new SqlCommand("Select * from emp_tbl", newConnection);
SqlDataReader dr = cmdd.ExecuteReader();
if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
newConnection.Close();
}
public void addValue(string emp_name, string Id, string emp_age)
{
newConnection.Open();
SqlCommand cmd = new SqlCommand("AddEmpDetails", newConnection);
cmd.CommandType = CommandType.StoredProcedure; //Procedure
cmd.Parameters.AddWithValue("@name", emp_name);
cmd.Parameters.AddWithValue("@id", Id);
cmd.Parameters.AddWithValue("@emp_age", emp_age);
cmd.ExecuteNonQuery();
newConnection.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{  
addValue(TextBox2.Text, TextBox1.Text, TextBox3.Text);
Response.Redirect("Default.aspx");
}
}

Step 4: Run the Application.

Click on Add button.

The Value is added in the database

Previous Home Next