Knowledgebase: QODBC POS
[QODBC-POS] Sample C#.NET Web Application for Creating SalesReceipt
Posted by Jack - QODBC Support on 29 August 2017 07:31 AM

Sample C#.NET Web Application for Creating SalesReceipt

Sample Application:

Please click here to download Sample Code.

Please refer below steps for using an application for Creating to SalesReceipt using C#.NET.

Run the application & click on the "Connect" button.

The application is connected with QuickBooks POS.

You need to insert CustomerListId, Comments, Cashier, SalesReceiptType, Item ListID, Quantity, Rate & Amount, which you want to enter, and click on the "Insert New SalesReceiptItem" button.

New SalesReceipt created.

Result in QuickBooks POS.

Note: Modify/Append existing SalesReceipt feature is not supported by QuickBooks SDK, so it is unavailable through QODBC POS.

Application Source Code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    private static OdbcConnection _cn;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {           
            lblErrorMessage.Visible = false;
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {

        }
    }
    protected void btnConnet_Click(object sender, EventArgs e)
    {
        try
        {
            if (btnConnect.Text == "Disconnect")
            {
                if (_cn != null)
                {
                    lblConnectionStatus.Text = "Disconnecting....";
                    _cn.Close();
                    _cn.Dispose();
                    _cn = null;
                    btnConnect.Text = "Connect";
                    lblConnectionStatus.Text = "Not Connected";
                    lblConnectionStatus.ForeColor = Color.DarkRed;
                }
            }
            else
            {
                if (_cn == null || _cn.State == ConnectionState.Closed)
                {
                    lblConnectionStatus.Text = "Connecting....";
                    _cn = new OdbcConnection(string.Format("DSN={0}", cboListOfDSN.Text));
                    _cn.ConnectionTimeout = 60;
                    _cn.Open();
                    btnConnect.Text = "Disconnect";
                    lblConnectionStatus.Text = "Connected";
                    lblConnectionStatus.ForeColor = Color.Green;
                }

            }

        }
        catch (Exception ex)
        {
            btnConnect.Text = "Errorred";
            lblConnectionStatus.Text = "Errorred";
            lblErrorMessage.Text = string.Format("Error - {0}, Stack Trace {1}", ex.Message, ex.StackTrace);
            lblErrorMessage.Visible = true;            
        }
    }  
    private void InsertSalesReceiptLineItem(string customerName, string comments, string cashierName, string salesReceiptType, string itemName, int quanity, int rate, int tenderAmount)
        {
            string lastInsrtedId = "";
            string query = string.Format("Insert Into SalesReceiptItem (CustomerListId, Comments, Cashier, SalesReceiptType, SalesReceiptItemListId,  SalesReceiptItemQty, SalesReceiptItemPrice, TenderCash01TenderAmount) Values ('{0}','{1}','{2}','{3}','{4}',{5},{6},{7})", customerName, comments, cashierName, salesReceiptType, itemName, quanity, rate, tenderAmount);
                using (OdbcCommand QBEmployeecmd = new OdbcCommand(query, _cn))
                {
                    QBEmployeecmd.CommandType = CommandType.Text;
                    QBEmployeecmd.ExecuteNonQuery();                    
                    lblErrorMessage.Text = "SalesReceiptItem Insert Successfully";
                    lblErrorMessage.Visible = true;
                }                
        }


        private void ProcessQuery(string query)
        {
            var cmd = new OdbcCommand(query, _cn);            
            DataSet dataSet = new DataSet();            
            OdbcDataReader reader = cmd.ExecuteReader();
            DataTable myTable = new DataTable();
            myTable.Load(reader);
            grvData.AutoGenerateColumns = true;
            grvData.DataSource = myTable;
            grvData.BorderStyle = BorderStyle.Double;
            grvData.DataBind();
        }

        private void DisplaySalesReceiptInGrid(string customerListID)
        {
            string query = string.Format("SELECT CustomerListId, Comments, Cashier, SalesReceiptType, SalesReceiptItemListId, SalesReceiptItemDesc1,  SalesReceiptItemQty As ItemQuantity , SalesReceiptItemPrice As ItemPrice, TenderCash01TenderAmount as TenderTotalAmount FROM SalesReceiptItem where TxnID='{0}'", customerListID);
            ProcessQuery(query);
        }
        protected void btnSalesReceiptItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (_cn == null || _cn.State == ConnectionState.Closed)
                {
                    btnConnet_Click(null, null);
                    btnConnect.Text = "Connect";
                    lblConnectionStatus.Text = "NotConnected";
                    lblConnectionStatus.ForeColor = Color.DarkRed;
                }
                if (string.IsNullOrEmpty(txtcustomerName.Text))
                {
                    lblErrorMessage.Text = "Customer Name is Required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtComments.Text))
                {
                    lblErrorMessage.Text = "Comments is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtCashierName.Text))
                {
                    lblErrorMessage.Text = "Cashier Name is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtSalesReceiptType.Text))
                {
                    lblErrorMessage.Text = "Sales Receipt Type is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtItemName.Text))
                {
                    lblErrorMessage.Text = "Item Name is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtQuantity.Text))
                {
                    lblErrorMessage.Text = "Quantity is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtItemPrice.Text))
                {
                    lblErrorMessage.Text = "Item Price is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                if (string.IsNullOrEmpty(txtTenderAmount.Text))
                {
                    lblErrorMessage.Text = "Tender Amount is required.";
                    lblErrorMessage.Visible = true;
                    return;
                }
                InsertSalesReceiptLineItem(txtcustomerName.Text, txtComments.Text, txtCashierName.Text, txtSalesReceiptType.Text, txtItemName.Text, int.Parse(txtQuantity.Text), int.Parse(txtItemPrice.Text), int.Parse(txtTenderAmount.Text));
                string lastInsertedId = GetLastInsertedId("sp_lastInsertID SalesReceiptItem");
                DisplaySalesReceiptInGrid(lastInsertedId);
            }
            catch (Exception ex)
            {
                lblErrorMessage.Text = string.Format("Error - {0}, Stack Trace {1}", ex.Message, ex.StackTrace);
                lblErrorMessage.Visible = true;
            }
        }

        private string GetLastInsertedId(string query)
        {
            string lastInsertedId = "";
            using (OdbcCommand QBEmployeecmd = new OdbcCommand(query, _cn))
            {
                QBEmployeecmd.CommandType = CommandType.Text;
                var executedResult = QBEmployeecmd.ExecuteScalar();
                lastInsertedId = executedResult.ToString();
                lblErrorMessage.Text = "SalesReceiptItem Insert Successfully";
                lblErrorMessage.Visible = true;
            }
            return lastInsertedId;
        }
}
(0 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please complete the captcha below (we use this to prevent automated submissions).