Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
i want to insert the data to a table and i want to return scope_identity() and value of inserted voucherNumber or ( UpdatedVoucherNo ) .. i got the query but i want to know how can i implement it on Code (c# code ) .. i cant use execute scalar because it only return a single value
right ??

so i want to insert and i need to return a row to code HOW ??? pls help me...

below is my query.. it is working correctly but i want to know to to use it in code !!!!!


SQL
 @ledgerId numeric(18,0) ,
 @voucherNo varchar(MAX) ,
 @invoiceNo varchar(MAX) ,
 @date datetime ,
 @month datetime ,
 @totalAmount decimal(18,5) ,
 @narration varchar(MAX) ,
-- @extraDate datetime ,
 @extra1 varchar(MAX) ,
 @extra2 varchar(MAX) ,
 @suffixPrefixId numeric(18,0) ,
 @voucherTypeId numeric(18,0) 
AS
DECLARE @UpdatedVoucherNo DECIMAL(18,0)

SET @UpdatedVoucherNo = (SELECT ISNULL( MAX(CAST (voucherNo AS NUMERIC(18,0))),0) + 1
 FROM tbl_SalaryVoucherMaster
 WHERE voucherTypeId=@voucherTypeId)

 IF (@UpdatedVoucherNo = @voucherNo )

 INSERT INTO tbl_SalaryVoucherMaster
 (
            /*salaryVoucherMasterId,*/
            ledgerId,
            voucherNo,
            invoiceNo,
            date,
            month,
            totalAmount,
            narration,
            extraDate,
            extra1,
            extra2,
            suffixPrefixId,
            voucherTypeId )
VALUES
 (
            /*@salaryVoucherMasterId, */
            @ledgerId, 
            @voucherNo, 
            @invoiceNo, 
            @date, 
            @month, 
            @totalAmount, 
            @narration, 
            getDate(), 
            @extra1, 
            @extra2,
            @suffixPrefixId,
            @voucherTypeId)

            SELECT SCOPE_IDENTITY() AS [Identity],0 AS [UpdatedVoucherNo]  
Posted

you can use data-adapter and retrieve data in datatable for SP you have written.

OR

Write SP with output parameter
see example
http://www.daniweb.com/software-development/csharp/threads/110318/getting-output-values-back-from-sql-stored-procedure#[^]

Happy Coding!
:)
 
Share this answer
 
String ConnString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";  
 SqlConnection con = new SqlConnection(ConnString);  
 SqlCommand cmd = new SqlCommand();  
 cmd.CommandType = CommandType.StoredProcedure;  
 cmd.CommandText = "SP_Name";  

//Add Your Parameters here

 cmd.Parameters.Add("@FName",SqlDbType.VarChar).Value = txtFName.Text.Trim();  
 cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = txtLName.Text.Trim();  
 cmd.Parameters.Add("@DateOfBirth", SqlDbType.DateTime).Value = txtDOB.Text.Trim();  
 cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim();  
 cmd.Parameters.Add("@State", SqlDbType.VarChar).Value = txtState.Text.Trim();  
 cmd.Connection = con;  
 try  
 {  
      con.Open();  
      int rows = cmd.ExecuteNonQuery();
                if (rows > 0)
                {
                    //do anything
                }
 }  
 catch (Exception ex)  
 {  
      throw ex;  
 }  
 finally  
 {  
      con.Close();  
      con.Dispose();  
 } 
 
Share this answer
 

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