How to execute stored procedure in another store procedure
--Create Database
create database dbtest
--Use Database
use
dbtest
--Create table StudentDetails RollNo with primary key
create table StudentDetails(RollNo
int identity(1,1) primary key,Name nvarchar(50),Age int)
--Create table FeeDetails RollNo relation with StudentDetails
RollNo(foregin key)
create table FeeDetails(RollNo
int foreign key references
StudentDetails(RollNo),Fee
int)
--Create proc ins_fee on FeeDetails Table
create proc ins_fee
@RollNo int,@Fee
int
as
begin
insert into FeeDetails values(@RollNo,@Fee)
end
--exec ins_fee 1,500
--Create proc ins_student on StudentDetails Table using out keyword
create proc ins_student @RollNo int
out,@Name nvarchar(50),@Age int,@Fee int
as
begin
insert into StudentDetails values(@Name,@Age)
set
@RollNo=@@IDENTITY
exec
ins_fee @RollNo ,@Fee
end
--declare @RollNo int
--exec ins_student @RollNo out,'Kush Tiwari',25,300
--select @RollNo
select * from StudentDetails
select *
from FeeDetails
################################################################
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial
class MyPage
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
}
protected void BtnSave_Click(object
sender, EventArgs e)
{
SqlConnection con = new
SqlConnection("Data
Source=kush;Initial Catalog=dbtest;Integrated Security=True");
con.Open();
SqlCommand
com = new SqlCommand("ins_student", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", txtname.Text);
com.Parameters.AddWithValue("@Age", Convert.ToInt32(txtage.Text));
com.Parameters.AddWithValue("@Fee", Convert.ToInt32(txtfee.Text));
com.Parameters.Add("@RollNo", SqlDbType.Int);
com.Parameters["@RollNo"].Direction
= ParameterDirection.Output;
com.ExecuteNonQuery();
con.Close();
LMessage.Text = "Your rollno is:" +" "+ com.Parameters["@RollNo"].Value.ToString();
}
}
0 comments:
Post a Comment