Working With SqlParameter in .NET

  • scott mcnitt (11/9/2007)


    For those of us coming from the old ADO world, do the parameter names have to exactly match the sproc parm names? In ADO the ordinal position mattered not the name.

    The order does not matter in .NET but the names do. So, if the sproc has a paramter @test-2, then from .NET it is required to give @test-2 as parameter name. Not doing so will result in runtime error.

  • It should be noted that the “@” symbol is a syntax flag for parameters in the SQL CommandString, and is not part of the actual Parameter Name. The .Add methods will accept the name with or without the “@” symbol for convenience, but it’s not technically proper to include it since it isn’t really part of the name.

  • Hi,

    I'm new to ASP.NET and ADO.NET and was just trying out SqlParameter. How do I use SqlParameter to get a return code along with other parameters (input). I have a stored procedure that accepts some parameters to insert new record in a table.

    I need to get the return code whether my insert execution is successful or not. So how do I get the return code?

    Thanks in a million.

  • Halim Dahlan (12/5/2007)


    Hi,

    I'm new to ASP.NET and ADO.NET and was just trying out SqlParameter. How do I use SqlParameter to get a return code along with other parameters (input). I have a stored procedure that accepts some parameters to insert new record in a table.

    I need to get the return code whether my insert execution is successful or not. So how do I get the return code?

    Thanks in a million.

    Sorry I don't usually use myself but as I recall if you have a return code the parameter collection of the SqlCommand object will contain it in the 0 item or @ret item so something like this in C# (assuming your SqlCommand object is named cmdSQL)

    cmdSQL.Parameters[0]...

    or

    cmdSQL.Parameters["@ret"]...

  • Try the following code, it should get you started:

    SqlCommand cmd = new SqlCommand("usp_Table_Action");

    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter param = new SqlParameter("@ReturnCode", SqlDbType.Int);

    param.Direction = ParameterDirection.ReturnValue;

    cmd.Parameters.Add(param);

  • Hey thanks a lot programmer. It worked.

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply