Showing posts with label occured. Show all posts
Showing posts with label occured. Show all posts

Friday, March 9, 2012

Fail to get Return_Value in stored procedure NullValueReference

I have a stored procedure that insert into a table and return a value indicating if the insert really occured.
If the a insert occurs then it returns the identity of that insert.

I have slimmed down my code here, so you can see.
My problem is that a NullValueReference exceptions occurs when trying to reference the returnvalue.
Is it not possible to get a return value along with a recordset ?

publicint RegisterCustomer(Customer customer)

{

SqlCommand myCommand =newSqlCommand("ins_RegistrerKunde", myConnection);

myCommand.CommandType =CommandType.StoredProcedure;

SqlParameter Epost =newSqlParameter("@.Epost",SqlDbType.VarChar, 25);

SqlParameter Passord =newSqlParameter("@.Passord",SqlDbType.VarChar, 25);

SqlParameter ReturnValue =newSqlParameter("@.RETURN_VALUE",SqlDbType.Int, 4);

ReturnValue.Direction =ParameterDirection.ReturnValue;

Epost.Value = customer.Email;

Passord.Value = customer.Password;

myCommand.Parameters.Add(ReturnValue);

myCommand.Parameters.Add(Epost);

myCommand.Parameters.Add(Passord);

myConnection.Open();

SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.SingleRow);

int isRegistered = (int)myCommand.Parameters["@.RETURN_VALUE"].Value;

myConnection.Close();

if (isRegistered == 1)

{

result.Read();

returnConvert.ToInt32(result["Kunde_ID"]);

result.Close();

}

returnConvert.ToInt32(isRegistered);

}

//Stored Procedure ins_RegistrerKunde
create procedure%PROC% (@.Epost varchar(25), @.Passord varchar(25))
as
begin
IF EXISTS( SELECT EpostFROM tbl_KundeWHERE Epost = @.Epost)
RETURN 0
ELSE
INSERT INTO tbl_Kunde( Epost, Passord)
VALUES (@.Epost, @.Passord)
SELECT @.@.IDENTITY AS Kunde_ID
RETURN 1
end
go

Output parameters and return values can be accessed only after a datareader is closed. My suggestion to you is to use an outputparameter for the @.@.IDENTITY value, and to then use an ExecuteNonQueryinstead of an ExecuteReader.

Also, instead of @.@.IDENTITY, use SCOPE_IDENTITY(), as this will returnthe the last Identity value in the current scope. SeeIdentity Crisis andUnderstanding Identity Columns for an explanation.|||

I see.

Thank you.Embarrassed [:$]

Sunday, February 26, 2012

Faced unknown problems on SQL Server (Urgent)

Hi all,

I'm facing some unknown errors occured in SQL server. SQL server prompt out the errors and I'm aware when are those errors occur.

Server displayed the sentences as below which I do not have any idea with its.


1) xpstar.dll to execute extended stored procedure 'xp_MSADEnabled'
2) xpsqlbot.dll to execute extended stored procedure 'xp_qv'

Anyone has any idea on how to solve those problem or any idea how is it caused?

I'm very appreciate for any reply and thank you for helping!!The second one is normally found in SQL Server's errorlog. I am not sure about xp_MSADEnabled. Have you registered the SQL Server with AD in Enterprise Manager? These messages are normally recorded in the errorlog, and do not normally indicate a problem.|||Hi,

Thank you for reply, MCrowley.
Any idea or suggestion on how to make the SQL server can execute back
because the system users could not login because of the database.|||Depends on what is wrong. What error are the users getting?|||Hi,

Users could not login to the system since both the issues happened. :S