watherton
asked on
ExecuteScalar error
Hi guys some help please.
I have this sp:
ALTER PROCEDURE dbo.sp_CreateProject
(
@UserKey int,
@RetailerCompanyKey int,
@ProjectTitle nvarchar(50)
)
AS
DECLARE @ProjectKey int
SET @ProjectKey = 0
SET NOCOUNT ON
If Exists(Select ProjectTitle from Project Where ProjectTitle = @ProjectTitle)
return @ProjectKey
Else
Begin
insert into Project ([ProjectUserKey],[Project Title],[Re tailerComp anyKey], [ProjectCreatedOn]) values (@UserKey,@ProjectTitle,@R etailerCom panyKey,ge tDate())
Set @ProjectKey = @@IDENTITY
End
IF NOT @ProjectKey IS NULL
Begin
insert into ProjectBrief (ProjectKey) values (@ProjectKey)
insert into ProjectStatus(ProjectKey,S tatusKey,C hangeDate) values (@ProjectKey,'1',getDate() )
End
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("@RetailerCom panyKey", System.Data.SqlDbType.Int, 4);
parameters[1].Value = retailer;
parameters[2] = new SqlParameter("@ProjectTitl e", System.Data.SqlDbType.NVar Char,50);
parameters[2].Value = title;
object obj =SqlHelper.ExecuteScalar(C onnection. Connection String, CommandType.StoredProcedur e, "sp_CreateProject",paramet ers);
if (obj == System.DBNull.Value)
{
return 0;
}
else
{
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
Wayne
I have this sp:
ALTER PROCEDURE dbo.sp_CreateProject
(
@UserKey int,
@RetailerCompanyKey int,
@ProjectTitle nvarchar(50)
)
AS
DECLARE @ProjectKey int
SET @ProjectKey = 0
SET NOCOUNT ON
If Exists(Select ProjectTitle from Project Where ProjectTitle = @ProjectTitle)
return @ProjectKey
Else
Begin
insert into Project ([ProjectUserKey],[Project
Set @ProjectKey = @@IDENTITY
End
IF NOT @ProjectKey IS NULL
Begin
insert into ProjectBrief (ProjectKey) values (@ProjectKey)
insert into ProjectStatus(ProjectKey,S
End
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,
parameters[0].Value = userkey;
parameters[1] = new SqlParameter("@RetailerCom
parameters[1].Value = retailer;
parameters[2] = new SqlParameter("@ProjectTitl
parameters[2].Value = title;
object obj =SqlHelper.ExecuteScalar(C
if (obj == System.DBNull.Value)
{
return 0;
}
else
{
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
Wayne
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.St
cmd.Parameters.Add("@UserK
cmd.Parameters["@UserKey"]
cmd.Parameters.Add("@Retai
cmd.Parameters["@RetailerC
cmd.Parameters.Add("@Proje
cmd.Parameters["@ProjectTi
cmd.Parameters.Add("RETURN
cmd.Parameters["RETURN_VAL
cmd.ExecuteNonQuery();
Console.Writeline( Convert.ToInt32(cmd.Parame