1.Create stored procedure in SQL like this...
CREATE procedure test
@retval int output
As
Begin
insert INTO Testtable(abc,xyz) values('ajay','balwindar')
@retval=@@identity
return @retval
End
2 .In asp.net code behind page..on page load call this function written below
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text= Convert.ToString(GetIdentityValue());
}
public int GetIdentityValue()
{
string S4CConString = ConfigurationManager.AppSettings["S4CConString"].ToString();
SqlConnection _con = new SqlConnection(S4CConString);
SqlCommand _cmd = new SqlCommand("test", _con);
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Parameters.Add("@subjectParam", SqlDbType.NVarChar);
_cmd.Parameters["@retval"].Direction = ParameterDirection.ReturnValue;
_cmd.ExecuteNonQuery();
int i = (int)_cmd.Parameters["@retval"].Value;
return i;
}
CREATE procedure test
@retval int output
As
Begin
insert INTO Testtable(abc,xyz) values('ajay','balwindar')
@retval=@@identity
return @retval
End
2 .In asp.net code behind page..on page load call this function written below
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text= Convert.ToString(GetIdentityValue());
}
public int GetIdentityValue()
{
string S4CConString = ConfigurationManager.AppSettings["S4CConString"].ToString();
SqlConnection _con = new SqlConnection(S4CConString);
SqlCommand _cmd = new SqlCommand("test", _con);
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Parameters.Add("@subjectParam", SqlDbType.NVarChar);
_cmd.Parameters["@retval"].Direction = ParameterDirection.ReturnValue;
_cmd.ExecuteNonQuery();
int i = (int)_cmd.Parameters["@retval"].Value;
return i;
}
No comments:
Post a Comment