Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

Additional information: A field or property with the name 'PurchaseOrderName' was not found on the selected data source.

ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Staff.Master" AutoEventWireup="true" CodeBehind="PurchaseOrderDraftDetails.aspx.cs" Inherits="JEAN.PurchaseOrderDraftDetails" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
      <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 267px;
        }
        .auto-style3 {
            width: 386px;
        }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <p></p>
    <p></p>
    <p></p>
    
        <p style="margin-left:440px">Purchase Order Draft Details</p>
        <table class="auto-style1" style="margin-left:440px">
            <tr>
                <td class="auto-style2">Purchase Order ID :</td>
                <td>
                    <asp:Label ID="lblPoID" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Purchase Order Name :</td>
                <td>
                    <asp:TextBox ID="tbPoName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Purchase Order Description : </td>
                <td>
                    <asp:TextBox ID="tbPoDescription" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Purchase Order Status :</td>
                <td>
                    <asp:Label ID="lblPoStatus" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Created by :</td>
                <td>
                    <asp:Label ID="lblCreateBy" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Created Date : </td>
                <td>
                    <asp:Label ID="lblCreatedDate" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Last Modified Date : </td>
                <td>
                    <asp:Label ID="lblLastModifiedDate" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">Supplier :</td>
                <td>
                    <asp:DropDownList ID="ddlSupplier" runat="server" AppendDataBoundItems="True">
                        <asp:ListItem Value="Select">-- Select --</asp:ListItem>
                        <asp:ListItem>1</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Supplier&#39;s Name : </td>
                <td>
                    <asp:TextBox ID="tbSName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Supplier&#39;s Phone :</td>
                <td>
                    <asp:TextBox ID="tbSPhone" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Supplier&#39;s Email :</td>
                <td>
                    <asp:TextBox ID="tbSEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Preferred Date : </td>
                <td>
                    <asp:TextBox ID="tbPreferredDate" runat="server"></asp:TextBox>
                    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbPreferredDate" />
                </td>
            </tr>
            <tr>
                <td class="auto-style2">Preferred Session : </td>
                <td>
                    <asp:TextBox ID="tbSession" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
    
    
        <p>
            <asp:GridView ID="gvDraftPoDetails" runat="server" style="margin-left:420px" Width="841px" OnRowCancelingEdit="gvDraftPoDetails_RowCancelingEdit" OnRowDeleting="gvDraftPoDetails_RowDeleting" OnRowEditing="gvDraftPoDetails_RowEditing" OnRowUpdating="gvDraftPoDetails_RowUpdating" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="PurchaseOrderID" HeaderText="PoID" />
                    <asp:BoundField DataField="PurchaseOrderName" HeaderText="Purchase Order Name" />
                    <asp:BoundField DataField="RoomType" HeaderText="Room Type" />
                    <asp:BoundField DataField="ProductDescription" HeaderText="Product Description" />
                    <asp:TemplateField HeaderText="Quantity"></asp:TemplateField>
                    <asp:BoundField DataField="Price" HeaderText="RoomCostPrice" />
                    <asp:TemplateField HeaderText="Total Price"></asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                <RowStyle BackColor="White" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>
        </p>
        <table class="auto-style1" style="margin-left:440px" >
            <tr>
                <td class="auto-style3">
                    <asp:Label ID="lblResult" runat="server"></asp:Label>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style3">
                    <asp:Button ID="btnUpdatePo" runat="server" Text="Update Purchase Order" OnClick="btnUpdatePo_Click" />
                </td>
                <td>
                    <asp:Button ID="btnCreatePo" runat="server" style="text-align: left" Text="Create Purchase Order" OnClick="btnCreatePo_Click" />
                </td>
            </tr>
            <tr>
                <td class="auto-style3">
                    <asp:Button ID="btnAddRoom" runat="server" Text="Add Room" OnClick="btnAddRoom_Click" />
                </td>
                <td>
                    <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
                </td>
            </tr>
        </table>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
</asp:Content>



behind code

C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JEAN.DAL;
using JEAN.BLL;

namespace JEAN
{
    public partial class PurchaseOrderDraftDetails : System.Web.UI.Page
    {
        BLLPurchaseOrder purchaseOrderBLL = new BLLPurchaseOrder();
        LoginBLL loginBLL = new LoginBLL();
        BLLPurchaseOrderItem purchaseOrderItemBLL = new BLLPurchaseOrderItem();

        DALPurchaseOrder purchaseOrder = null;
        JEAN.DAL.Login login = null;
        DALPurchaseOrderItem purchaseOrderItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            CalendarExtender1.StartDate = DateTime.Now;

            if (Page.IsPostBack == false)
            {
                bindListBox();

                // Get PurchaseOrderID from querystring
                string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

                // Convert PurchaseOrderID to int
                int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

                // Call getPurchaseOrderDraftDetail() method
                purchaseOrder = purchaseOrderBLL.getPurchaseOrderDraftDetail(iPurchaseOrderID);

                int createUserID = purchaseOrder.CreateUserID;
                login = loginBLL.getUserDetails(createUserID);
                string username = login.UserName;

                // Display Purchase Order details on web form
                lblPoID.Text = purchaseOrder.PurchaseOrderID.ToString();
                tbPoName.Text = purchaseOrder.PurchaseOrderName;
                tbPoDescription.Text = purchaseOrder.PurchaseOrderDescription;
                lblPoStatus.Text = purchaseOrder.PurchaseOrderStatus;
                lblCreateBy.Text = username;
                lblCreatedDate.Text = purchaseOrder.CreateDate;
                lblLastModifiedDate.Text = purchaseOrder.LastModifiedDate;

                ddlSupplier.SelectedValue = purchaseOrder.SupplierID.ToString();
                tbSName.Text = purchaseOrder.SupplierContactPersonName;
                tbSPhone.Text = purchaseOrder.SupplierContactPersonTelephone.ToString();
                tbSEmail.Text = purchaseOrder.SupplierContactPersonEmail;
                tbPreferredDate.Text = purchaseOrder.PreferredDate;
                tbSession.Text = purchaseOrder.PreferredSession;

                List<DALPurchaseOrderItem> purchaseOrderItemDetail = new List<DALPurchaseOrderItem>();
                purchaseOrderItemDetail = purchaseOrderItemBLL.getDraftPurchaseOrderDetail(iPurchaseOrderID);
                gvDraftPoDetails.DataSource = purchaseOrderItemDetail;
                gvDraftPoDetails.DataBind();
            }
        }

        private void ShowAlert(string msg)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
           "showalert", sb.ToString(), true);
        }

        private SqlDataReader getReader()
        {
            // Get connection string from web.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["CompanyDB"].ConnectionString;
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            string strCommandText = "SELECT SupplierID, SupplierName FROM Supplier";
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        private void bindListBox()
        {
            ddlSupplier.DataSource = getReader();
            // SupplierName will be displayed in the dropdownlist control
            ddlSupplier.DataTextField = "SupplierName";
            // When an item is selected in dropdownlist
            // SupplierID will be returned in ddlSupplier.SelectedValue
            ddlSupplier.DataValueField = "SupplierID";
            ddlSupplier.DataBind();
        }

        protected void btnUpdatePo_Click(object sender, EventArgs e)
        {
            bool bValid = true;

            if (this.tbPoName.Text == "")
            {
                ShowAlert("Purchase Order Name is required!");
                this.tbPoName.Focus();
                bValid = false;
            }
            else if (this.tbPoDescription.Text == "")
            {
                ShowAlert("Purchase Order Description is required!");
                this.tbPoDescription.Focus();
                bValid = false;
            }
            else if (ddlSupplier.SelectedValue == "Select")
            {
                ShowAlert("Supplier is required!");
                bValid = false;
            }
            else if (this.tbSName.Text == "")
            {
                ShowAlert("Contact Person Name is required!");
                this.tbSName.Focus();
                bValid = false;
            }
            else if (this.tbSPhone.Text == "")
            {
                ShowAlert("Contact Person Telephone is required!");
                this.tbSPhone.Focus();
                bValid = false;
            }
            else if (this.tbSEmail.Text == "")
            {
                ShowAlert("Contact Person Email is required!");
                this.tbSEmail.Focus();
                bValid = false;
            }
            else if (this.tbPreferredDate.Text == "")
            {
                ShowAlert("Preferred Delivery Date is required!");
                this.tbPreferredDate.Focus();
                bValid = false;
            }
            else if (this.tbSession.Text == "")
            {
                ShowAlert("Preferred session is required!");
                this.tbSession.Focus();
                bValid = false;
            }
            else
            {
                // Get PurchaseOrderID from querystring
                string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

                // Convert PurchaseOrderID to int
                int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

                BLLPurchaseOrder purchaseBLL = new BLLPurchaseOrder();

                string SupplierID = ddlSupplier.SelectedValue.ToString();
                string PurchaseOrderName = tbPoName.Text;
                string PurchaseOrderDescription = tbPoDescription.Text;
                string ContactPersonName = tbSName.Text;
                string ContactPersonTelephone = tbSPhone.Text;
                string ContactPersonEmail = tbSEmail.Text;
                string PreferredDate = tbPreferredDate.Text;
                string PreferredSession = tbSession.Text;

                int iSupplierID = Int32.Parse(SupplierID);
                int iContactPersonTelephone = Int32.Parse(ContactPersonTelephone);

                string date = System.DateTime.Today.ToShortDateString();

                string msg = purchaseBLL.PurchaseOrderUpdate(iPurchaseOrderID, PurchaseOrderName, PurchaseOrderDescription, iSupplierID, ContactPersonName, iContactPersonTelephone, ContactPersonEmail, PreferredDate, PreferredSession);

                string msg1 = purchaseOrderBLL.PurchaseOrderDateUpdate(iPurchaseOrderID, date);

                ShowAlert("Purchase Order has been updated!");
            }
        }

        protected void btnCreatePo_Click(object sender, EventArgs e)
        {
            bool bValid = true;

            if (this.tbPoName.Text == "")
            {
                ShowAlert("Purchase Order Name is required!");
                this.tbPoName.Focus();
                bValid = false;
            }
            else if (this.tbPoDescription.Text == "")
            {
                ShowAlert("Purchase Order Description is required!");
                this.tbPoDescription.Focus();
                bValid = false;
            }
            else if (ddlSupplier.SelectedValue == "Select")
            {
                ShowAlert("Supplier is required!");
                bValid = false;
            }
            else if (this.tbSName.Text == "")
            {
                ShowAlert("Contact Person Name is required!");
                this.tbSName.Focus();
                bValid = false;
            }
            else if (this.tbSPhone.Text == "")
            {
                ShowAlert("Contact Person Telephone is required!");
                this.tbSPhone.Focus();
                bValid = false;
            }
            else if (this.tbSEmail.Text == "")
            {
                ShowAlert("Contact Person Email is required!");
                this.tbSEmail.Focus();
                bValid = false;
            }
            else if (this.tbPreferredDate.Text == "")
            {
                ShowAlert("Preferred Delivery Date is required!");
                this.tbPreferredDate.Focus();
                bValid = false;
            }
            else if (this.tbSession.Text == "")
            {
                ShowAlert("Contact Person Email is required!");
                this.tbSession.Focus();
                bValid = false;
            }
            else
            {
                // Get PurchaseOrderID from querystring
                string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

                // Convert PurchaseOrderID to int
                int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

                BLLPurchaseOrder purchaseBLL = new BLLPurchaseOrder();

                string SupplierID = ddlSupplier.SelectedValue.ToString();
                string PurchaseOrderName = tbPoName.Text;
                string PurchaseOrderDescription = tbPoDescription.Text;
                string ContactPersonName = tbSName.Text;
                string ContactPersonTelephone = tbSPhone.Text;
                string ContactPersonEmail = tbSEmail.Text;
                string PreferredDate = tbPreferredDate.Text;
                string PreferredSession = tbSession.Text;

                int iSupplierID = Int32.Parse(SupplierID);
                int iContactPersonTelephone = Int32.Parse(ContactPersonTelephone);

                string date = System.DateTime.Today.ToShortDateString();

                string msg = purchaseBLL.PurchaseOrderUpdate(iPurchaseOrderID, PurchaseOrderName, PurchaseOrderDescription, iSupplierID, ContactPersonName, iContactPersonTelephone, ContactPersonEmail, PreferredDate, PreferredSession);

                string msg1 = purchaseOrderBLL.PurchaseOrderDateUpdate(iPurchaseOrderID, date);

                string msg2 = purchaseOrderBLL.PurchaseOrderStatusUpdate(iPurchaseOrderID);

                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect",
                "alert('Purchase Order has been successfully created!'); window.location='" +
                Request.ApplicationPath + "PurchaseOrderPending.aspx';", true);
            }
        }

        protected void btnAddRoom_Click(object sender, EventArgs e)
        {
            string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

            Response.Redirect("RoomCatalogueBook.aspx?PurchaseOrderID=" + purchaseOrderID);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("PurchaseOrderDraft.aspx");
        }

        protected void gvDraftPoDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();
            int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

            int PurchaseOrderItemID = (int)gvDraftPoDetails.DataKeys[e.RowIndex].Value;
            purchaseOrderItemBLL.PurchaseOrderItemDelete(PurchaseOrderItemID);

            string date = System.DateTime.Today.ToShortDateString();

            string msg = purchaseOrderBLL.PurchaseOrderDateUpdate(iPurchaseOrderID, date);

            Response.Redirect("PurchaseOrderDraftDetails.aspx?PurchaseOrderID=" + purchaseOrderID);
        }

        protected void gvDraftPoDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDraftPoDetails.EditIndex = e.NewEditIndex;
            string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();
            int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

            List<DALPurchaseOrderItem> purchaseOrderItemDetail = new List<DALPurchaseOrderItem>();

            purchaseOrderItemDetail = purchaseOrderItemBLL.getDraftPurchaseOrderDetail(iPurchaseOrderID);
            gvDraftPoDetails.DataSource = purchaseOrderItemDetail;
            gvDraftPoDetails.DataBind();
        }

        protected void gvDraftPoDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDraftPoDetails.EditIndex = -1;
            string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

            int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

            List<DALPurchaseOrderItem> purchaseOrderItemDetail = new List<DALPurchaseOrderItem>();

            purchaseOrderItemDetail = purchaseOrderItemBLL.getDraftPurchaseOrderDetail(iPurchaseOrderID);
            gvDraftPoDetails.DataSource = purchaseOrderItemDetail;
            gvDraftPoDetails.DataBind();
        }

        protected void gvDraftPoDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Get selected row
            int selectedRow = e.RowIndex;

            // Get PurchaseOrderItemID from data key
            int PurchaseOrderItemID = (int)gvDraftPoDetails.DataKeys[selectedRow].Value;

            // Get current GridView row
            GridViewRow row = (GridViewRow)gvDraftPoDetails.Rows[selectedRow];
            // Find text box for tbQuantity
            TextBox Quantity = (TextBox)row.FindControl("tbQuantity");
            // Remove $ sign
            string strQuantity = Quantity.Text;
            int intQuantity = 0;
            string date = System.DateTime.Today.ToShortDateString();

            // Check if Quantity is valid
            if (Int32.TryParse(strQuantity, out intQuantity))
            {
                // Get PurchaseOrderID from querystring
                string purchaseOrderID = Request.QueryString["PurchaseOrderID"].ToString();

                // Convert PurchaseOrderID to int
                int iPurchaseOrderID = Int32.Parse(purchaseOrderID);

                UpdateQuantity(PurchaseOrderItemID, intQuantity);

                string msg = purchaseOrderBLL.PurchaseOrderDateUpdate(iPurchaseOrderID, date);

                Response.Redirect("PurchaseOrderDraftDetails.aspx?PurchaseOrderID=" + purchaseOrderID);
            }
            else
            {
                lblResult.Text = "Invalid Quantity";
            }
        }

        private void UpdateQuantity(int PurchaseOrderItem, int Quantity)
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["CompanyDB"].ConnectionString;
            SqlConnection myConnect = new SqlConnection(strConnectionString);

            string strCommandText = "UPDATE PurchaseOrderItem SET Quantity=@Quantity WHERE PurchaseOrderItemID=@PurchaseOrderItemID";

            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
            cmd.Parameters.AddWithValue("@PurchaseOrderItemID", PurchaseOrderItem);
            cmd.Parameters.AddWithValue("@Quantity", Quantity);

            myConnect.Open();
            int result = cmd.ExecuteNonQuery();

            if (result > 0)
                lblResult.Text = "";
            else
                lblResult.Text = "";

            myConnect.Close();

            // Cancel Edit mode
            gvDraftPoDetails.EditIndex = -1;
        }
    }
}
Posted
Comments
[no name] 1-Feb-16 2:07am    
At which line, it is throwing exception?
WinterBanana 1-Feb-16 2:32am    
this line " gvDraftPoDetails.DataBind();"

1 solution

You are binding PurchaseOrderColumn directly to the grid.
ASP.NET
<asp:BoundField DataField="PurchaseOrderName" HeaderText="Purchase Order Name" />

Make sure that this field is returned by the data source.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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