[Last Call] Learn how to a build a cloud-first strategyRegister Now


ExecuteScalar error

Posted on 2004-11-30
Medium Priority
Last Modified: 2010-04-15
Hi guys some help please.

I have this sp:

ALTER PROCEDURE dbo.sp_CreateProject
@UserKey int,
@RetailerCompanyKey int,
@ProjectTitle nvarchar(50)

DECLARE @ProjectKey int

SET @ProjectKey = 0

      If Exists(Select ProjectTitle from Project Where ProjectTitle = @ProjectTitle)
            return @ProjectKey
            insert into Project ([ProjectUserKey],[ProjectTitle],[RetailerCompanyKey], [ProjectCreatedOn]) values (@UserKey,@ProjectTitle,@RetailerCompanyKey,getDate())
            Set @ProjectKey = @@IDENTITY

      IF NOT @ProjectKey IS NULL      
            insert into ProjectBrief (ProjectKey) values (@ProjectKey)

            insert into ProjectStatus(ProjectKey,StatusKey,ChangeDate) values (@ProjectKey,'1',getDate())
RETURN @ProjectKey

I call this sp with the following code:

public int NewProject(int userkey, int retailer, string title)
                  SqlParameter[] parameters = new SqlParameter[3];

                  parameters[0] = new SqlParameter("@UserKey", System.Data.SqlDbType.Int,4);
                  parameters[0].Value = userkey;

                  parameters[1] = new SqlParameter("@RetailerCompanyKey", System.Data.SqlDbType.Int,4);
                  parameters[1].Value = retailer;

                  parameters[2] = new SqlParameter("@ProjectTitle", System.Data.SqlDbType.NVarChar,50);
                  parameters[2].Value = title;
                  object obj =SqlHelper.ExecuteScalar(Connection.ConnectionString, CommandType.StoredProcedure, "sp_CreateProject",parameters);
                  if (obj == System.DBNull.Value)
                        return 0;
                        return (int) obj;

Instead of returning the next project number it crashs with an error stating that object is not set to an instance of an object.

If I change the ExecuteScalar to ExecuteNonQuery I get -1 returned.

Any help would be mostly appreciated

Question by:watherton
LVL 15

Expert Comment

ID: 12710486
ExecuteScalar is for looking at the first field of a select statement.  So you definitely don't want to do that.

I can see that you are returning items.  What you want to do is add add a return value

SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandText = "sp_CreateProject";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@UserKey", System.Data.SqlDbType.Int,4);
cmd.Parameters["@UserKey"].Value = userkey;
cmd.Parameters.Add("@RetailerCompanyKey", System.Data.SqlDbType.Int,4);
cmd.Parameters["@RetailerCompanyKey"].Value = retailer;
cmd.Parameters.Add("@ProjectTitle", System.Data.SqlDbType.NVarChar,50);
cmd.Parameters["@ProjectTitle"].Value = title;
cmd.Parameters.Add("RETURN_VALUE", System.Data.SqlDbType.Int,4);
cmd.Parameters["RETURN_VALUE"].Direction = ParameterDirection.ReturnValue;

Console.Writeline( Convert.ToInt32(cmd.Parameters["RETURN_VALUE"].Value) );

Accepted Solution

melodiesoflife earned 150 total points
ID: 12724623
Hi  watherton

+ The ExecuteScalar method returns the first column of the first row of data returned by the command, no matter how many rows the command actually selects.

when you call:

return (int) obj;

it crashed because the obj in your code may not be a number.

+ The ExecuteNonQuery method return the number of rows have been update, so in this case you should used ExecuteNonQuery rather than ExecuteScalar

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question