How to update values in asp.net c# using stored procedure
In this article, we learn that how to update values
in asp.net c# using stored procedure. As we know that stored procedure is a
collection of group of one or more Transact-SQL statements. And execute in a
single unit and save under a single name.
Data base:-
Table :-
Stored producer syntax:-
CREATE PROCEDURE spupdate_record
@Name nvarchar(50),
@Contact_no nvarchar(50),
@Email nvarchar (50),
@Password
nvarchar (50),
@recordid int
AS
BEGIN
SET NOCOUNT ON;
UPDATE record SET
Name=@Name,
Contact_no=@Contact_no,
Email=@Email,
Password=@Password
WHERE
record_id=@recordid
END
GO
Now we want to update name update record id is 1.
Source Code:-
<%@
Page Language="C#" AutoEventWireup="true" CodeFile="updatevaluesusingtextbox.aspx.cs"
Inherits="updatevaluesusingtextbox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width: 280px;">
<legend>update
the values where record_id is 1</legend>
<asp:TextBox ID="txtname" runat="server" placeholder="Please Enter Your Name"></asp:TextBox><br />
<br />
<asp:TextBox ID="txtcontact" runat="server" placeholder="Please Enter Your Contact Number"></asp:TextBox><br />
<br />
<asp:TextBox ID="txtemail" runat="server" placeholder="Please Enter Your Email ID"></asp:TextBox><br />
<br />
<asp:TextBox ID="txtpwd" runat="server" placeholder="Please Enter Your Password"></asp:TextBox><br />
<br />
<asp:Button ID="btnupdate" runat="server" Text="Update"
onclick="btnupdate_Click" />
<br />
<asp:Label ID="lbresult" runat="server" ForeColor="Green"></asp:Label>
</fieldset>
</div>
</form>
</body>
</html>
Code behind(C#):-
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data.SqlClient;
using System.Data;
public partial class updatevaluesusingtextbox : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
view_values();
}
}
// this code to
show values in textbox
// here we show
record_id 1 values
protected void view_values()
{
SqlConnection
con = new SqlConnection("Data Source=NEERAJ-PC;Initial
Catalog=CodeSolution;Persist Security Info=True;User ID=sa;
password=12345678");
con.Open();
SqlCommand
cmd = new SqlCommand("Select * from record where record_id ='1'",
con);
SqlDataReader
dr = cmd.ExecuteReader();
if
(dr.Read())
{
txtname.Text = dr["Name"].ToString();
txtcontact.Text = dr["Contact_no"].ToString();
txtemail.Text = dr["Email"].ToString();
txtpwd.Text = dr["Password"].ToString();
}
con.Close();
}
// this code we
will updates the values
protected void btnupdate_Click(object
sender, EventArgs e)
{
SqlConnection
con = new SqlConnection("Data Source=NEERAJ-PC;Initial
Catalog=CodeSolution;Persist Security Info=True;User ID=sa;
password=12345678");
SqlCommand
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spupdate_record";
cmd.Parameters.AddWithValue("@Name", txtname.Text);
cmd.Parameters.AddWithValue("@Contact_no", txtcontact.Text);
cmd.Parameters.AddWithValue("@Email", txtemail.Text);
cmd.Parameters.AddWithValue("@Password", txtpwd.Text);
cmd.Parameters.AddWithValue("@recordid", 1);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
lbresult.Text = "Record updated successfully";
con.Close();
}
}
Out-Put:-
How to update values in asp.net c# using stored procedure
Reviewed by NEERAJ SRIVASTAVA
on
9:13:00 AM
Rating:
Although this is correct, I don't recommend it. This is using ADO.Net, which was the ASP.NET 1.0/2.0 standard.
ReplyDeleteWith ASP.NET 3.0 came linq. With linq you save 30 lines of code and 30 minutes of effort, just dragging the stored procedure using WYSIWIG. You can do the same with entity framework as well, which adds an extra dll to the deployment.