Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Assignment with SQLHelper.ExecuteScalar() using a StoredProcedure in a single line. No can do.


I desperately wanted to assign an integer using Sqlhelper.ExecuteScaler() with a StoredProcedure IN A SINGLE LINE, but I threw in the towel after giving it a good college try. As in:

int k = int.Parse(SQLHelper.ExecuteScalar(DataHelper.Connection(), "p_insert_list_schools", new object[] { "test", 1}).ToString());

I couldn't escape the Object reference not set runtime.

I ended up using:

SqlParameter[] p =
{
 SQLHelper.MakeInParam("@school",SqlDbType.VarChar,100, txtSchool.Text,
 SQLHelper.MakeOutParam("@schid",SqlDbType.Int,4)
};
SQLHelper.ExecuteScalar(DataHelper.Connection(), CommandType.StoredProcedure, "p_insert_list_schools", p)
int k = int.Parse(p[2].Value.ToString());

I googled and found no evidence of anyone else being able to do this.  Doable using a SQL query, but not a stored procedure.  If anyone has the secret on this, please let me know!

Or the problem could be with my SQL procedure...

------------------------------------------------------------------------------------------------------------------------
-- Date Created: Wednesday, March 31, 2004
-- Created By:   Generated by CodeSmith
------------------------------------------------------------------------------------------------------------------------

ALTER PROCEDURE dbo.p_insert_list_schools
 @school varchar(100),
 @schid int output
AS

set nocount on

INSERT INTO [dbo].[list_schools] (
 [school]
) VALUES (
 @school
)

Select @schid = @@Identity

 

Posted: Apr 02 2004, 12:21 AM by daveburke | with 5 comment(s)
Filed under:

Comments

Scott Mitchell said:

I take it you are using the DAAB? What version? Here's the code template I normally use:

int id = Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "AddCustomer", new SqlParameter("@Name", "Scott"), new SqlParameter("@Age", 25)));

Namely, I use zero to many instances of SqlParameters, based on the sproc I am calling.

hth
# April 2, 2004 1:09 AM

Scott Mitchell said:

Also, to be pedantic, you should use SCOPE_IDENTITY() over @@IDENTITY (assuming you are using SQL 2000). See http://weblogs.sqlteam.com/travisl/archive/2003/10/29/405.aspx
# April 2, 2004 1:12 AM

Elph said:

Really you don't need use executescalar with this, because your gets the info trought output parameter. You can use Executenonquery, because the parameter return the info @@Identity.


# April 2, 2004 1:28 AM

Jerry Pisk said:

Using the output parameter is the problem, your single line doesn't have a local parameter variable to store the output value in (it does under the hood but...)
# April 2, 2004 2:55 AM

TrackBack said:

# April 2, 2004 9:21 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)