Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a store procedure to get latest entered id. It works. But in c# coding, it displays zero value. I want to get last entered id. Please help.

What I have tried:

CREATE PROCEDURE spDonor (
@flag char(1), @donorcode varchar(2), @donor varchar(25),  @lastid int out
) WITH ENCRYPTION AS
SET NOCOUNT OFF
BEGIN 
	IF @flag = 'I' BEGIN
		INSERT INTO tblDonor (DonorCode, Donor) VALUES (@donorcode, UPPER(@donor))
		SELECT @lastid = SCOPE_IDENTITY()
 	END
	IF @flag = 'U' BEGIN
		UPDATE tblDonor
		SET DonorCode = @donorcode, Donor = UPPER(@donor) WHERE SeqNo = @lastid 	END
	IF @flag = 'D' BEGIN
		DELETE tblDonor WHERE SeqNo = @lastid 	END
	IF @flag = 'A' BEGIN
		SELECT SeqNo, DonorCode, Donor FROM tblDonor 	END
	IF @flag = 'S' BEGIN
		SELECT SeqNo, DonorCode, Donor FROM tblDonor WHERE SeqNo = @lastid 	END
	IF @flag = 'N' BEGIN
	SELECT SeqNo, DonorCode, Donor FROM tblDonor WHERE (UPPER(Donor) Like '%'+UPPER(@donor)+'%') 	END
END
SET NOCOUNT ON
RETURN
GO

/// 
private string _flag = string.Empty;
        public string Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }

        private string _dnrCode = string.Empty;
        public string DnrCode
        {
            get { return _dnrCode; }
            set { _dnrCode = value; }
        }

        private string _donor = string.Empty;
        public string Donor
        {
            get { return _donor; }
            set { _donor = value; }
        }
        
        private int _lastid = 0;
        public int Lastid
        {
            get { return _lastid; }
            set { _lastid = value; }
        }


public int executeDonor()
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@flag", _flag)
                , new SqlParameter("@donorcode", _dnrCode)
                , new SqlParameter("@donor", _donor)
                , new SqlParameter("@lastid", _lastid)
            };
            return DAO.ExecuteProc("spDonor", param);            
       }

private void btnSave_Click(object sender, EventArgs e)
{
mtacsys.Flag = "N";
mtacsys.Donor = txtDonor.Text.Trim();
DataTable dtchk = mtacsys.GetDonorByCodeName();
if (dtchk.Rows.Count > 0)
{
toolStripMessage.Text = mtacsys.Donor + " : This is already entered.";
}
else
{
mtacsys.Flag = "I";
mtacsys.DnrCode = txtDonorId.Text.Trim();
mtacsys.Donor = txtDonor.Text.Trim();

int i = mtacsys.executeDonor();
if (i > 0)
{
toolStripMessage.Text = "Record Saved Successfully";
ResetTextBox();
MessageBox.Show(mtacsys.Lastid + " : is entered");
}
}
Posted
Updated 11-Jan-18 22:15pm
v2

check this sql server - Using stored procedure output parameters in C# - Stack Overflow[^]
to read the value from out parameter in c#, you will have to change the ExecuteProc method.

refer this sample code:
public int executeDonor()
      {
          int lastID = 0;
          SqlConnection con = new SqlConnection("Your Conn string");
          SqlCommand cmd = new SqlCommand("spDonor ", con);
          cmd.CommandType = System.Data.CommandType.StoredProcedure;
          cmd.Parameters.Add(new SqlParameter("@flag", _flag));
          cmd.Parameters.Add(new SqlParameter("@donorcode", _dnrCode));
          cmd.Parameters.Add(new SqlParameter("@donor", _donor));
          SqlParameter paramOutId = new SqlParameter("@lastid", System.Data.SqlDbType.Int);
          cmd.Parameters.Add(paramOutId);
          paramOutId.Direction = System.Data.ParameterDirection.Output;
          con.Open();
          cmd.ExecuteNonQuery();
          lastID = (int)paramOutId.Value;
          con.Close();
      }
 
Share this answer
 
v2
You already know the last ID - you saved in it @LASTID when you did the insert:
SQL
INSERT INTO tblDonor (DonorCode, Donor) VALUES (@donorcode, UPPER(@donor))
SELECT @lastid = SCOPE_IDENTITY()

Just return that.
 
Share this answer
 
Comments
sulomila 12-Jan-18 3:44am    
Yes, but I need help in c# code. when I save record I want to know the id. Please.
OriginalGriff 12-Jan-18 4:01am    
And you have it!
Just eiter SELECT @LASTID or retrieve it from the parameters, given that it's an OUT parameter to the SP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900