please give steps to create and call stored proc in asp.net c#. instead of hard coding the sql Query I want to use stored proc to insert, Update, Delete record on respective click.
please guide where to store store proc in DB?.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jun 9, 2012, 8:28 AM
Stored procedure
ALTER PROCEDURE insert1
(@sid varchar(50),@sname varchar(50),@smarks int,@saddress varchar (50),@year varchar(50))
AS
insert student(sid,sname,smarks,saddress,year) values (@sid,@sname,@smarks,@saddress,@year)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Stored_procedure_to_insert._Default" %>
using System;
using System.Collections;
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;
namespace Stored_procedure_to_insert
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("insert1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@sid", txtid.Text);
com.Parameters.AddWithValue("@sname", txtname.Text);
com.Parameters.AddWithValue("@smarks", int.Parse(txtmarks.Text));
com.Parameters.AddWithValue("@saddress", txtaddress.Text);
com.Parameters.AddWithValue("@year", txtyear.Text);
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Data entered successfully!!!";
}
}
}
Similarly for update and delete records the stored procedure are as below
CREATE PROCEDURE update1
(@sid varchar(50),@sname varchar(50),@smarks int,@saddress varchar (50),@year varchar(50))
AS
update student set sname=@sname,smarks=@smarks,saddress=@saddress,year=@year where sid=@sid
CREATE PROCEDURE delete1
(@sid varchar(50))
AS
delete from student where sid=@sid
Thanks
Satyapriya NayakPosted Jun 9, 2012, 8:22 AM
Here is an example how to display records using stored procedure.
Stored procedure
ALTER PROCEDURE display
AS
select * from student
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Stored_procedure_to_display._Default" %>
using System;
using System.Collections;
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;
namespace Stored_procedure_to_display
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("display", con);
com.CommandType = CommandType.StoredProcedure;
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "student");
GridView1.DataSource = ds;
GridView1.DataMember = "student";
GridView1.DataBind();
con.Close();
}
}
}
Thanks