Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my table
SQL
create table tbl_SalesOrder (SalesOrder_id int primary key not null identity(1,1),
Voucher_No  AS 'S' + RIGHT('0'+ CONVERT(VARCHAR(5),SalesOrder_id),5),
Account_No int,
Customer_Name varchar(50),
Address varchar(50),
SalesTax_Type varchar(50),
Date varchar(50),
Store_Name int,
Currency int )


i want to link my autogenerated Voucher_No field to front end (asp.net) which is html textbox

What I have tried:

this is my front end code:
C#
protected void btnSubmit_Click(object sender, EventArgs e)
      {
          string strcon = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ToString();

      
          String insert, VoucherNumber, AccountNumber, CustomerName, Address, SalesTaxType, Date, StoreName, Currency;

      

          SqlConnection con = new SqlConnection(strcon);
          con.Open();
          SqlCommand com = new SqlCommand("spr_SalesOrder", con);
          insert = "insert";
          com.CommandType = CommandType.StoredProcedure;
          com.Parameters.AddWithValue("@abc", insert);

          VoucherNumber = txtVoucherNo1.Value.ToString();
          AccountNumber = ddAccountNumber1.Text;
          CustomerName = txtCustomerName1.Text;
          Address = txtAddress1.Text;
          SalesTaxType = ddSalesTaxType1.Value;
          Date = txtDateTime1.Value;
          StoreName = ddStoreName1.Text;
          Currency = ddCurrency1.Text;

        SqlParameter p1 = new SqlParameter("Voucher_No", VoucherNumber);
     
          SqlParameter p2 = new SqlParameter("Account_No", AccountNumber);
          SqlParameter p3 = new SqlParameter("Customer_Name", CustomerName);
          SqlParameter p4 = new SqlParameter("Address", Address);
          SqlParameter p5 = new SqlParameter("SalesTax_Type", SalesTaxType);
          SqlParameter p6 = new SqlParameter("Date", Date);
          SqlParameter p7 = new SqlParameter("Store_Name", StoreName);
        SqlParameter p8 = new SqlParameter("Currency", Currency);

          com.Parameters.Add(p1).Direction=ParameterDirection.Output;
          com.Parameters.Add(p2);
          com.Parameters.Add(p3);
          com.Parameters.Add(p4);
          com.Parameters.Add(p5);
          com.Parameters.Add(p6);
          com.Parameters.Add(p7);
        com.Parameters.Add(p8);

          com.ExecuteNonQuery();
 
          con.Close();


     
          lblSaleOrder.Text = "Registered Successfully";
Posted
Updated 24-Apr-16 22:24pm
v2
Comments
Suvendu Shekhar Giri 25-Apr-16 2:44am    
What is the problem here?
Member 12385326 25-Apr-16 2:47am    
it is not displaying autogenerated voucher_no from backend to front end
Karthik_Mahalingam 25-Apr-16 3:14am    
post only relevant code for displaying
Suvendu Shekhar Giri 25-Apr-16 3:34am    
Seems, you have mixed up many things. This should be a very simple task.
Can you share the stored procedure spr_SalesOrder, then we'll try to provide the error fix and correct approach.

Things to look:
1. Voucher_No AS 'S' + RIGHT('0'+ CONVERT(VARCHAR(5),SalesOrder_id),5)
>> is it a computed column?
2. SqlParameter p1 = new SqlParameter("Voucher_No", VoucherNumber);
>> what is the use of sending to the stored proc if it is a computed column?

Please share the stored proc.
Thanks
Member 12385326 25-Apr-16 3:51am    
yes it is computed column.it is computing S01,S02....... like this.


1 solution

Change your code as

SQL:

SQL
ALTER proc [dbo].[spr_SalesOrder]
(
 
@Voucher_No varchar(50) output , -- output keyword for out params
@Account_No varchar(50)=null,
@Customer_Name varchar(50)=null,
@Address varchar(50)=null,
@PurchaesTax_Type varchar(50)=null,
@Date varchar(50)=null,
@Store_Name varchar(50)=null,
@Currency varchar(50)=null,
@abc varchar(20) =null
)
AS
 
BEGIN
set nocount on;
DECLARE @SalesOrder_Id int
IF @abc='insert' 
insert into tbl_PurchaseRegister(Account_No,customer_Name,Address,SalesTax_Type,Date,Store_Name,Currency) 
Values(@Account_No,@Customer_Name,@Address,@SalesTax_Type,@Date,@Store_Name,@Currency)
SET @SalesOrder_Id =  SCOPE_IDENTITY();
select @Voucher_No  =  Voucher_No from tbl_PurchaseRegister where SalesOrder_id = @SalesOrder_Id
END


c#

C#
SqlParameter paramVoucher = new SqlParameter("@Voucher_No", SqlDbType.VarChar, 50);
           paramVoucher.Direction = ParameterDirection.Output;  //you should provide direction for out param
           com.Parameters.Add(paramVoucher);
           com.ExecuteNonQuery();
           string voucherNumber = paramVoucher.Value.ToString();
           lblSaleOrder.Text = voucherNumber;
 
Share this answer
 
Comments
Member 12385326 25-Apr-16 4:38am    
thnx for ur help.the code is working if i take lable to display it. but i am taking textbox. and it is not showing text property.what to do?
Karthik_Mahalingam 25-Apr-16 4:41am    
write
YourTextBoxName.Text = voucherNumber;
Member 12385326 25-Apr-16 4:43am    
i tried this. but it is not suggesting text property.
Member 12385326 25-Apr-16 4:46am    
i used textboxname.value :) thnx a lot for ur help
Karthik_Mahalingam 25-Apr-16 4:47am    
welcome :)

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