First off, I haven't done much ASP.net programming, so might be missing something obvious. I'm creating a form which displays the contents of a business object (BO) in a table (contains textboxes and dropdownlists). In Page_Load, I retrieve the ID of the inventory item via the QueryString which was passed to the form. It then fetches the data into a local BO and populates the various things in the table with the data.
I'm running into the problem of how do I persist the contents of the BO so the user can update some of the data back to the database. I initially tried just using the existing BO I had created and stored the data in, but when I'm in the btnUpdate_Click event, the BO is blank. Why?
Then I tried to store the data in the Context.Items cache. I can successfully retrieve the data while still in the Page_Load, but once again, when I'm in btnUpdate_Click I get
Object reference not set to an instance of an object., on the line with the Context.Item. Am I just using it wrong or missing something else entirely?
Details.aspx.cscs code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class InventoryDetails : System.Web.UI.Page
{
InventoryDataSource ids = new InventoryDataSource();
InvFunc invfunc = new InvFunc();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Int32 m_hwid = Convert.ToInt32(Request.QueryString["hwid"]);
InventoryBO invitem = ids.GetInventoryDetails(m_hwid);
Context.Items["hwid"] = m_hwid;
Context.Items["invitem"] = invitem;
//works fine
Label1.Text = ((Int32)Context.Items["hwid"]).ToString();
ddlAssigned.DataSource = invfunc.GetRequesters();
ddlAssigned.DataBind();
ddlAssigned.SelectedValue = invitem.AssignedTo;
ddlDevice.DataSource = invfunc.GetDevices();
ddlDevice.DataTextField = "Value";
ddlDevice.DataValueField = "Key";
ddlDevice.DataBind();
ddlDevice.SelectedValue = invitem.DeviceId.ToString();
txtNumber.Text = String.Format("{0:F4}", invitem.Number);
txtNumber.ReadOnly = true;
ddlManf.DataSource = invfunc.GetManufacturers();
ddlManf.DataTextField = "Value";
ddlManf.DataValueField = "Key";
ddlManf.DataBind();
ddlManf.SelectedValue = invitem.ManufacturerId.ToString();
txtModel.Text = invitem.Model;
txtSN.Text = invitem.Serial;
txtDateEntered.Text = invitem.DateArrived.ToString();
txtDateEntered.ReadOnly = true;
ddlStatus.DataSource = invfunc.GetStatuses();
ddlStatus.DataTextField = "Value";
ddlStatus.DataValueField = "Key";
ddlStatus.DataBind();
ddlStatus.SelectedValue = invitem.StatusId.ToString();
txtNotes.Text = invitem.Notes;
txtPONumber.Text = invitem.PoNumber.ToString();
txtPONumber.ReadOnly = true;
ddlCustomer.DataSource = invfunc.GetRequesters();
ddlCustomer.DataBind();
ddlCustomer.SelectedValue = invitem.Customer;
ddlCustomer.Enabled = false;
ddlOrderedby.DataSource = invfunc.GetRequesters();
ddlOrderedby.DataBind();
ddlOrderedby.SelectedValue = invitem.OrderedBy;
ddlOrderedby.Enabled = false;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Page.User.Identity.IsAuthenticated && Page.IsValid)
{
//dies here with "Object reference not set to an instance of an object."
Label1.Text = ((Int32)Context.Items["hwid"]).ToString();
//invitem.AssignedTo = ddlAssigned.SelectedItem.Text;
//invitem.DeviceId = Convert.ToInt32(ddlDevice.SelectedValue);
//invitem.ManufacturerId = Convert.ToInt32(ddlManf.SelectedValue);
//invitem.Model = txtModel.Text;
//invitem.Serial = txtSN.Text;
//invitem.StatusId = Convert.ToInt16(ddlStatus.SelectedValue);
//invitem.Notes = txtSN.Text;
//ids.UpdateInventory(invitem);
btnClose_Click(this, e);
}
}
protected void btnClose_Click(object sender, EventArgs e)
{ }
}
Details.aspxhtml code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="InventoryDetails.aspx.cs"
Inherits="InventoryDetails" Debug="true" %>
<!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">
<title>Inventory Details</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:Table ID="Table1" runat="server" Height="87px" Width="359px">
<asp:TableRow runat="server">
<asp:TableCell runat="server" Font-Bold="True">Device Info</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Assigned To</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlAssigned" AppendDataBoundItems="True" runat="server">
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="ddlAssigned"
ErrorMessage="Must select an end user" Operator="NotEqual" ValueToCompare="Other">*</asp:CompareValidator>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Device</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlDevice" runat="server" AppendDataBoundItems="True">
<asp:ListItem>-Select Device-</asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ControlToValidate="ddlDevice"
ErrorMessage="You must select a device type" ValidationExpression="\d+">*</asp:RegularExpressionValidator>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Number</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtNumber" runat="server" ReadOnly="True"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Manufacturer</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlManf" runat="server" AppendDataBoundItems="True">
<asp:ListItem>-Select Manufacturer-</asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="ddlManf"
ErrorMessage="You must select a manufacturer type" ValidationExpression="\d+">*</asp:RegularExpressionValidator>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Model</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtModel" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtModel"
ErrorMessage="You must provide a model number">*</asp:RequiredFieldValidator>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Serial</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtSN" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Date Entered</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtDateEntered" runat="server" ReadOnly="true"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Status</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlStatus" runat="server" AppendDataBoundItems="True">
<asp:ListItem>-Select Status-</asp:ListItem>
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="ddlStatus"
ErrorMessage="You must select an status" ValidationExpression="\d+">*</asp:RegularExpressionValidator>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Notes</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtNotes" runat="server" TextMode="MultiLine"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server" Font-Bold="True">PO Info</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">PO</asp:TableCell>
<asp:TableCell runat="server">
<asp:TextBox ID="txtPONumber" runat="server" ReadOnly="true"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Customer</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlCustomer" AppendDataBoundItems="True" runat="server">
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">Ordered By</asp:TableCell>
<asp:TableCell runat="server">
<asp:DropDownList ID="ddlOrderedby" AppendDataBoundItems="True" runat="server">
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update & Close" />
<asp:Button ID="btnClose" runat="server" OnClick="btnClose_Click" Text="Close" OnClientClick="javascript:window.close();" /><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
ShowSummary="False" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
</div>
</form>
</body>
</html>