简体   繁体   中英

c# Order Processing ASP.NET Page

I am building a ASP.NET page which is an Sales Order Processing Form I have two Last issues that due to my lack of experience in the language I am struggling with hopefully you can help.

Issue 1 - I have a field <asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList> that is pulling from a database table Called Products to populate its options list. I need to automatically pull the SalesPrice value and insert it into the txtUnitAmount field based on the selection of the Product.

Issue 2 - Once the form has been filled and there are records in the DataTable I then need to insert all of the rows into the Database table SalesOrders when a user clicks a button.

My ASPX code (in full)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SOPOrderEntry.aspx.cs" Inherits="CRM2Sage.SOPOrderEntry1" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="Stylesheet" type="text/css" href="stylesheet.css" />
    <title>CRM2Sage - SOP Entry Form</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            float: left;
            border-style: solid;
            border-width: 1px;
        }
    </style>
    <script type="text/javascript">
        function q_multi_p(form) {
            q = eval(form.txtQty.value)
            p = eval(form.txtUnitAmount.value)
            t = q * p
            form.txtValue.value = t
        }
    </script>
</head>
<body style="padding: 0; margin: 0;">
<div style="width: 1170px; margin: 0 auto;">
    <form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CRM2Sage %>"
    SelectCommand="SELECT [AccountRef], [Name], [Add1], [Add2], [Add3], [Add4], [Add5], [ContactName], [Telephone], [Terms] FROM [Customers]">
    <SelectParameters>
        <asp:Parameter DefaultValue="1" Name="AccountRef" Type="String" />
    </SelectParameters>
    </asp:SqlDataSource> 
    <div style="width: 1170px; height: 211px;">
        <table align="right" cellpadding="0" class="style1">
            <tr>
                <td>
    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="585px" 
        AutoGenerateRows="False" DataSourceID="SqlDataSource1" 
        EnableModelValidation="True" HorizontalAlign="Left" CellPadding="4" ForeColor="#333333" 
                        GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <Fields>
            <asp:BoundField DataField="AccountRef" HeaderText="Account Ref" 
                SortExpression="AccountRef" />
            <asp:BoundField DataField="Name" HeaderText="Company Name" 
                SortExpression="Name" />
            <asp:BoundField DataField="Add1" HeaderText="Address" SortExpression="Add1" />
            <asp:BoundField DataField="Add2" SortExpression="Add2" />
            <asp:BoundField DataField="Add3" HeaderText="City" SortExpression="Add3" />
            <asp:BoundField DataField="Add4" HeaderText="County" SortExpression="Add4" />
            <asp:BoundField DataField="Add5" HeaderText="Post Code" SortExpression="Add5" />
            <asp:BoundField DataField="ContactName" HeaderText="Contact" 
                SortExpression="ContactName" />
            <asp:BoundField DataField="Telephone" HeaderText="Telephone" 
                SortExpression="Telephone" />
            <asp:BoundField DataField="Terms" HeaderText="Credit Terms" 
                SortExpression="Terms">
            <HeaderStyle CssClass="lbl" />
            <ItemStyle CssClass="row" />
            </asp:BoundField>
        </Fields>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    </asp:DetailsView>
                </td>
                <td>
                    <table>
                    <tr>
                    <td><asp:Label ID="OrderNoLabel" runat="server" Text="Customer Order No."></asp:Label></td>
                    <td><asp:TextBox ID="OrderNoText" runat="server" 
                            ontextchanged="OrderNoText_TextChanged"></asp:TextBox></td>
                    </tr>
                    <tr>
                    <td><asp:Button ID="GenInvRef" runat="server" Text="Gen Invoice Ref" 
                            onclick="GenInvRef_Click" /></td>
                    <td><asp:TextBox ID="InvRefText" runat="server" 
                            ontextchanged="InvRefText_TextChanged"></asp:TextBox></td>
                    </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 100%; border: 2px Groved #666; background-color: #FFF; height: 100%; padding: 15px;">
    <fieldset style="height:200px;">
    <asp:Label ID="lblTips" runat="server" ForeColor="White" BackColor="Red" Width="100%" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Width="100%" CellPadding="4" EmptyDataText="No Order Lines Created Yet!" 
            ForeColor="#333333" EnableModelValidation="True" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" Visible="False" />
                <asp:BoundField DataField="AccountRef" HeaderText="Account #" />
                <asp:BoundField DataField="InvRef" HeaderText="Invoice Ref" />
                <asp:BoundField DataField="CustOrderNo" HeaderText="Order Number" />
                <asp:BoundField DataField="Product" HeaderText="Product" />
                <asp:BoundField DataField="Qty" HeaderText="Qty" />
                <asp:BoundField DataField="UnitAmount" HeaderText="Unit Amount" />
                <asp:BoundField DataField="Value" HeaderText="Line Total" />
                <asp:BoundField DataField="TakenBy" HeaderText="Taken By" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
            <RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <div style="margin: 0 auto; position: relative;">  
            <asp:Table ID="Table1" runat="server" Width="1158px" 
                ForeColor="#333333" GridLines="None">
            <asp:TableHeaderRow EnableTheming="true" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White">
                <asp:TableHeaderCell>Account Ref:</asp:TableHeaderCell>
                <asp:TableHeaderCell>Invoice Ref</asp:TableHeaderCell>
                <asp:TableHeaderCell>Customer Order #</asp:TableHeaderCell>
                <asp:TableHeaderCell>Product</asp:TableHeaderCell>
                <asp:TableHeaderCell>Qty</asp:TableHeaderCell>
                <asp:TableHeaderCell>Unit Amount</asp:TableHeaderCell>
                <asp:TableHeaderCell>Value</asp:TableHeaderCell>
                <asp:TableHeaderCell>Taken By</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow ForeColor="#333333" BackColor="#F7F6F3">
                <asp:TableCell><asp:TextBox ID="txtAccountRef" runat="server" CssClass="ContactSmallTextField" ReadOnly="True"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtInvoiceRef" runat="server" CssClass="ContactSmallTextField"></asp:TextBox ></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtCustOrderNo" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtQty" runat="server" CssClass="ContactExtTextField" onChange="javascript:q_multi_p(this.form)"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtUnitAmount" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtValue" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtTakenBy" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>           
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell><asp:Button ID="btnAdd" runat="server" Text="Add Order Line" OnClick="btnAdd_Click" CssClass="btn" /></asp:TableCell>
            </asp:TableRow>
            </asp:Table>
        </div>
    </fieldset>
    </div>
    </form>
</div>
</body>
</html>

My CS Code in Full

using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace CRM2Sage
{
    public partial class SOPOrderEntry1 : System.Web.UI.Page
    {

        internal DataTable myDt = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                myDt = new DataTable();
                myDt = CreateDataTable();
                Session["myDatatable"] = myDt;

                this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
                this.GridView1.DataBind();

            }
            getAccountRef();
            Fill1();
        }



        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtAccountRef.Text.Trim() == "")
            {
                this.lblTips.Text = "You must have an Account Number.";
                return;
            }
            else
            {
                AddDataToTable (
                    this.txtAccountRef.Text.Trim(),
                    (string)Request.Form["InvRefText"].ToString(),
                    (string)Request.Form["OrderNoText"].ToString(),
                    (string)Request.Form["txtProduct"].ToString(),
                    //this.txtProduct.Text.Trim(),
                    this.txtQty.Text.Trim(),
                    (string)Request.Form["txtUnitAmount"].ToString(),
                    //this.txtUnitAmount.Text.Trim(),
                    this.txtValue.Text.Trim(),
                    this.txtTakenBy.Text.Trim(),
                    (DataTable)Session["myDatatable"]
                );


                this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
                this.GridView1.DataBind();

                //this.txtAccountRef.Text = "";
                this.txtInvoiceRef.Text = "";
                //this.txtCustOrderNo.Text = "";
                //this.txtProduct.SelectedItem.Text = "";
                this.txtQty.Text = "";
                //this.txtUnitAmount.Text = "";
                this.txtValue.Text = "";
                this.txtTakenBy.Text = "";
                this.lblTips.Text = "";
            }
        }

        private void AddDataToTable(string AccountRef, string InvRef, string CustOrderNo, string Product, string Qty, string UnitAmount, string Value, string TakenBy, DataTable myTable)
        {
            DataRow row;

            row = myTable.NewRow();

            row["id"] = Guid.NewGuid().ToString();
            row["AccountRef"] = AccountRef;
            row["InvRef"] = InvRef;
            row["CustOrderNo"] = CustOrderNo;
            row["Product"] = Product;
            row["Qty"] = Qty;
            row["UnitAmount"] = UnitAmount;
            row["Value"] = Value;
            row["TakenBy"] = txtTakenBy.Text;


            myTable.Rows.Add(row);
        }

        private DataTable CreateDataTable()
        {
            DataTable myDataTable = new DataTable();

            DataColumn myDataColumn;

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "id";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "AccountRef";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "InvRef";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "CustOrderNo";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Product";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Qty";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "UnitAmount";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Value";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "TakenBy";
            myDataTable.Columns.Add(myDataColumn);

            return myDataTable;
        }

        public void Fill1()
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
            using (SqlConnection _con = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con))
            {
                cmd.Connection.Open();

                SqlDataReader ddlValues;
                ddlValues = cmd.ExecuteReader();

                txtProduct.DataSource = ddlValues;
                txtProduct.DataValueField = "Description";
                txtProduct.DataTextField = "Description";
                txtProduct.DataBind();

                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }

        public void getAccountRef()
        {
            string getAccountRef = (string)Request.QueryString["AccountRef"].ToString();
            SqlDataSource1.SelectParameters[0].DefaultValue = getAccountRef;
            txtAccountRef.Text = getAccountRef;
        }

        public void getOrderNo()
        {
            string getOrderNo = (string)Request.Form["OrderNoText"].ToString();
            txtCustOrderNo.Text = getOrderNo;
        }

        protected void OrderNoText_TextChanged(object sender, EventArgs e)
        {
            getOrderNo();
        }

        private string GenerateId()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        protected void GenInvRef_Click(object sender, EventArgs e)
        {
            string GenInvRef = GenerateId();
            InvRefText.Text = GenInvRef;
        }

        public void getInvRef()
        {
            string getInvRef = (string)Request.Form["InvRefText"].ToString();
            txtInvoiceRef.Text = getInvRef;
        }

        protected void InvRefText_TextChanged(object sender, EventArgs e)
        {
            getInvRef();
        }

        //public void Fill2()
        //{
        //    string getProdValue = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
        //    using (SqlConnection _con = new SqlConnection(getProdValue))
        //    using (SqlCommand cmd = new SqlCommand("SELECT SalesPrice FROM Products WHERE Description = '" + txtProduct + "'", _con))
        //    {
        //        cmd.Connection.Open();
        //        var unitPrice = cmd.ExecuteScalar();
        //        txtUnitAmount.Text = unitPrice.ToString();
        //        cmd.Connection.Close();
        //        cmd.Connection.Dispose();
        //    }           
        //}

        //protected void txtProduct_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Fill2();
        //}

    }
}

I think with a bit of help and guidance I will be done and ever so grateful this will also stand me in good sted for further projects, thanks in advance to everyone who answers I will of course give full props to everyone who helps out.

In the mean time a thank you to @Slappy, @Nick & @MikeEast and the other many who have been really helpful thus far.

Best

Justin

As I can't write the entire code, so only giving you some hints.

 1. Create a class with the field name you want to insert into you DB. 
    Like for Sales order, whatever fields you have in your form, 
    create properties for the same.

 2. Bind the grid and Dropdowns from the database using the datatable.

 3. Put a hidden field on the form to detect insert or update process

 4. On selection chnage of grid or dropdown fill the form fields, 
    you have for sales order. By extracting the rows from the datatable 
    and fill it to textboxes.

 5. While save button click , create a method that will be used for filling
    up the SalesOrder Class object. pass this object to data layer, extract 
   the properties and pass it as parameter in your AddUpadte query.

which framework are you using? Dropdown list auto postback=true should be set, then create event SelectedIndex Change then write your code to auto fill the text post ( textboxName.text = dropdownlistName.selectedItem.Tex ).

Hope it helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM