This article describes the ASP.net implementation invocation of stored procedures with output parameters. Share to everyone for your reference, specific as follows:
Front desk Jqurey
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (' #change_image '). Click (Function () {$ (' #imgAuthenCode '). attr (' src ', ' checkcode.aspx? ') +math.random ())
; $ ("#accounts"). Bind ("Blur", function () {
$.ajax ({
URL: "checkusername.aspx",
type: "Post"
, DataType: "HTML",
data:{user_name:$ ("#accounts"). Val ()},
success:function (msg) {$ ("#tip_accounts"). HTML ( msg);}
}
); </script>
aspx file:
protected void Page_Load (object sender, EventArgs e)
{
entity.user us = new Entity.user ();
us. user_name = request.params["user_name"]. ToString ();
if (US. User_checkname ())
{
Response.Write ("<font color=red> user name already exists, try another user name!") </font> ");
}
else
{
Response.Write ("<font color=black> user name can be used!") </font> ");
}
User class
public bool User_checkname ()
{
try
{
string[,] Sarray = new string[2, 2];
Sarray[0, 0] = "@user_name";
sarray[1, 0] = "@r_id";
Sarray[0, 1] = user_name;
Sarray[1, 1] = null;
Factory.sqlmodel sm = new Factory.sqlmodel ();
Id = Sm. proc_return_r_id ("User_checkname", Sarray);
if (Id > 0)
{return
true;
}
else
{return
false;
}
}
catch (Exception e)
{
log lg = new log ();
Lg. Exceptionerror (e);
return false;
}
}
Sqlmodel class Be sure to set the type and length of the output parameter otherwise an error occurs
STRING[1]: The Size property has a invalid size of 0.
public int proc_return_r_id (string proc_name, string[,] sarray)
{
try
{
if (sarray.getlength (0) >= 1)
{
Database db = new database ();
sqlparameter[] Sqlpar = new Sqlparameter[sarray.getlength (0)];//join return value for
(int i = 0; i < sarray.getlength (0); i++)
{
Sqlpar[i] = new SqlParameter (sarray[i, 0], sarray[i, 1]);
}
Sqlpar[sarray.getlength (0)-1]. Direction = ParameterDirection.Output;
Sqlpar[sarray.getlength (0)-1]. SqlDbType = SqlDbType.Int;
Return DB. PROC_RETURN_R_ID (Proc_name, Sqlpar);
}
else
{return
0;
}
}
Catch
{return
0;
}
}
DATABASE.cs class
public int proc_return_r_id (string proc_name, params sqlparameter[] cmdparms)
{
try
{
OpenConnection ();
if (cmdparms!= null)
{
foreach (SqlParameter parameter in cmdparms)
{
if (parameter. Direction = = Parameterdirection.inputoutput | | Parameter. Direction = = ParameterDirection.Input) && (parameter. Value = = null))
{
parameter. Value = DBNull.Value;
}
BASESQLCOMMAND.PARAMETERS.ADD (parameter);
}
Basesqlcommand.commandtype = CommandType.StoredProcedure;
Basesqlcommand.commandtext = Proc_name;
Basesqlcommand.executenonquery ();
return (int) basesqlcommand.parameters["@r_id"]. Value;
}
else
{return
0;
}
}
Catch
{return
0;
}
Finally
{
BaseSqlCommand.Parameters.Clear ();
CloseConnection ();
}
I hope this article will help you to ASP.net program design.