dslreports logo
 
    All Forums Hot Topics Gallery
spc
Search similar:


uniqs
747

USR56K
join:2000-05-20
Lynnwood, WA

USR56K

Member

[ASP] Issue with Context.Items

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.cs
cs 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.aspx
html 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>
&nbsp;<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>
Mike_343
I Need Speed.
join:2001-07-05
Dyer, IN

Mike_343

Member

ok let me get this straight. Your loading a data item from your datasource and then populating a form and then you want to update the item when you click update which calls your click event.

I have never used Context.Items for this.

here is an example.

using System;
using System.Collections.Generic;
using System.Web.UI;

namespace Example
{
public partial class ExampleForm : Page
{
private Car _car;
protected void Page_Load(object sender, EventArgs e)
{
_car = new Car();

_car.Make = "Chevrolet";
_car.Model = "Camaro";
_car.Doors = 2;

make.Text = _car.Make;
model.Text = _car.Model;
doors.SelectedValue = _car.Doors.ToString();

submitButton.Click += new EventHandler(OnUpdate);
}

protected void OnUpdate(object sender, EventArgs e)
{
_car.Make = make.Text;
_car.Model = model.Text;
_car.Doors = int.Parse(doors.SelectedValue);

// update your db here.
// example _car.Update();
}
}

public class Car
{
private string _make;
private string _model;
private int _doors;

public string Make
{
get
{
return _make;
}
set
{
_make = value;
}
}

public string Model
{
get
{
return _model;
}
set
{
_model = value;
}
}

public int Doors
{
get
{
return _doors;
}
set
{
_doors = value;
}
}
}
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExampleForm.aspx.cs" Inherits="Example.ExampleForm" %>

<!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>Example</title>
</head>
<body>
<form id="siteForm" runat="server">
<div>
<asp:TextBox ID="make" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="model" runat="server"></asp:TextBox>
<br />
<br />
<asp:DropDownList ID="doors" runat="server">
<asp:ListItem Value="2" Selected="True">2 Door</asp:ListItem>
<asp:ListItem Value="4" Selected="True">4 Door</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="submitButton" runat="server" Text="Update" />
</div>
</form>
</body>
</html>

USR56K
join:2000-05-20
Lynnwood, WA

USR56K

Member

I figured it out. Turns out I don't need to use caching really at all. Instead, I overrode the forms OnInit method and used that to populate the dropdown lists and my BO. Now the data persists across postbacks. yay.