Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
hello sir.
i have a stored procedure which returns string as a out parameter. that string is
print 'Records are successfully inserted'
i want to print this message on a label in aspx page.
how to do this. any body plz help me.

What I have tried:

my stored procedure is

SQL
ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50)
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
print 'Records are inserted successfully'
end


i want to print this print statement on my aspx page.
my aspx.cs page is below

C#
{
       DataSet ds = new DataSet();
       SqlParameter[] oparam = new SqlParameter[3];
       oparam[0] = new SqlParameter("@eid", Txteid.Text);
       oparam[1] = new SqlParameter("@ename", Txtename.Text);


       FU.SaveAs("E:\\BackUpWebsites\\sri\\images\\" + FU.FileName);
       Server.MapPath("images\\" + FU.FileName);

       oparam[2] = new SqlParameter("@image", "E:\\BackUpWebsites\\sri\\images\\" + FU.FileName );


       ds = BusinessLogic.InsertDetails(oparam );
       SqlParameter paramoutput=new SqlParameter ("@print", SqlDbType .NVarChar );
       paramoutput .Direction =ParameterDirection .Output ;
       paramoutput .Value =10;
       LblResult.Visible = true;
       LblResult.Text = paramoutput.Value.ToString();
   }

thanking you.
Posted
Updated 6-May-23 14:57pm
v3

Hi,

print 'text statement' in SQL is not considered as an output parameter, it's like as Console.Write('Text') in .NET development environment.

So, please change your sp:
SQL
ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50),
@outString nvarchar(100) output
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
SET @outString = 'Records are inserted successfully'
end

In aspx:
public void InsertSP()
    {
        SqlConnection cnn = new SqlConnection("your connection string here");
        try
        {            
            SqlCommand cmd = new SqlCommand("sp_insert", cnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@eid", SqlDbType.Int);
            cmd.Parameters.Add("@ename", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@image", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@outString", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;
            cnn.Open();
            cmd.ExecuteNonQuery();
            LblResult.Visible = true;
            LblResult.Text = cmd.Parameters["@outString"].Value.ToString();

        }
        catch (Exception ex)
        {
            //Process error
        }
        finally
        {
            if (cnn != null && cnn.State == ConnectionState.Open)
            {
                cnn.Close();
            }
        }
    }
 
Share this answer
 
v4
change in your procedure
SQL
ALTER proc [dbo].[sp_insert]
(
@eid int,
@ename nvarchar(50),
@image nvarchar(50),
@print nvarchar(50) output 
)
as begin 
insert into tab (eid , ename , image )
values(
@eid,
@ename,
@image 
)
 select @print = 'Records are inserted successfully'
end

now execute code you get value of @print parameter
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900