Working With SqlParameter in .NET

  • Vasant Raj

    SSCommitted

    Points: 1835

    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.

  • Joel Rea

    Old Hand

    Points: 339

    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.

  • Halim Dahlan

    Grasshopper

    Points: 22

    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.

  • Antares686

    SSC Guru

    Points: 125444

    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"]...

  • programmers

    SSC Journeyman

    Points: 76

    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);

  • Halim Dahlan

    Grasshopper

    Points: 22

    Hey thanks a lot programmer. It worked.

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

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