Output Parameter
It is used to return identity column value when insertion operation occurs in the database.
Let's see with an example:
It is used to return identity column value when insertion operation occurs in the database.
Let's see with an example:
select * from EmployeeTest
In the aspx file put the following code
In code behind add the following code
In the aspx file put the following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Employee Name :
<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox><br />
Employee designation :
<asp:TextBox ID="txtEmpDesignation" runat="server"></asp:TextBox><br />
<asp:Button ID="savedata" runat="server" OnClick="btnClick_Save" Text="Save" />
<br />
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ADONET
{
public partial class sqloutputparameter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Save(object sender, EventArgs e)
{
String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(CS))
{
SqlCommand sqlcmd = new SqlCommand("EmployeeTest_Insert", sqlcon);
sqlcmd.CommandType =
System.Data.CommandType.StoredProcedure;
sqlcmd.Parameters.AddWithValue("@EmpName",
txtEmployeeName.Text);
sqlcmd.Parameters.AddWithValue("@EmpDesignation",
txtEmpDesignation.Text);
SqlParameter outPara = new SqlParameter();
outPara.ParameterName = "@EmpID";
outPara.SqlDbType =
System.Data.SqlDbType.Int;
outPara.Direction =
System.Data.ParameterDirection.Output;
sqlcmd.Parameters.Add(outPara);
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
string EmpID = outPara.Value.ToString();
lblMessage.Text="Emp ID :" +
EmpID;
}
}
}
}
Lets Execute our application we will get the following output
Lets check the database
select * from EmployeeTest
EmpID generated is 2 which is the identity column id, when record is inserted.
0 comments:
Post a Comment