The simple answer is going to be set a value to it; because as it is written there is never a value assigned to it.
cmd.Parameters.Add(DataAccess.CreateParameter(cmd, "@Id", DbType.String, DBNull.Value, ParameterDirection.Output));
So you have created an
OUTPUT
parameter within C# and have populated it with
NULL. This is fine so far.
Now let's look at the SP you have created, which has a few comments added
alter procedure spaddpinrequest(
@Appmstregno varchar(100),
@Qty varchar(100),
@Amount int,
@paymode varchar(100),
@transaction varchar(200),
@Bankname varchar(200),
@Branchname varchar(200),
@Accountno varchar(200),
@Ifsc varchar(100),
@FileName varchar(200),
@msg varchar(200),
@id varchar(100)
) as
begin
insert into addpinrequest(
Appmstregno, Qty, Amount, paymode, [transaction], bankname
, branchname, accountno, ifsc, [filename], msg, id
) values(
@Appmstregno, @Qty, @Amount, @paymode, @transaction, @Bankname
, @Branchname, @Accountno, @Ifsc, @FileName, @msg, @id
);
end
So what we have here is a Stored Procedure which has not OUTPUT parameters identified, so they are not going to be returned. Furthermore, the value for
@id
is never set.
Now for what I would be doing....
Within the C# portion, I would have defined the parameter as a local variable with a default value that I could capture later.
var spOut = DataAccess.CreateParameter(cmd, "@Id", DbType.String, "InitValue", ParameterDirection.Output)
cmd.Parameters.Add(spOut);
int i = DataAccess.ExecuteNonQuery(cmd);
string id = spOut.Value.ToString();
if (id == "InitValue") { throw new Exception("SP did not change the value"); }
Within the Stored Procedure, you need to add the
OUTPUT
identifier within the declaration portion. I also set a default value just in case the calling code does not pass a value you... This can also captured later by the calling code
ALTER PROCEDURE spaddpinrequest(
@id varchar(100) = "No Value Passed In" OUTPUT
) AS
if (id == "InitValue") { throw new Exception("SP did not change the value"); }
if (id == "No Value Passed In") { throw new Exception("No Value Passed In"); }
Something else that I do is check that rows affected variable (i) a little more thoroughly; we know that it should return 1 which you are checking for, but what are you going to do if it returns a 2? Or 100?
int i = DataAccess.ExecuteNonQuery(cmd);
string id = spOut.Value.ToString();
if (i != 1) { throw new Exception("Unexpected results from INSERT Query"); }
else {
foreach (GridViewRow gr in gvPins.Rows)
So right now your code should work... but the procedure never sets a value to it, and there is nothing in your code that even hints at what should be going into it. Just know that somewhere within the body of the procedure you may need to add a line like this to return a value
SET @id =
This is only for the first block of code. I did notice that you do reuse the
cmd
object further down, and then you start adding parameters to it. You should be aware that the parameter collection may still has values in it from the first query, which can be cleared out with
cmd.Parameters.Clear();
.