Information from a user control

  • Thread starter Thread starter Christopher Young
  • Start date Start date
C

Christopher Young

I have several user controls on a page and I am trying to
get information out of them. The postback is being caused
on the aspx page and not in the user control. I have
tried using a property but I keep getting a message that
the object is not referenced.

What I am trying to do is be information from text boxes
and insert them into a new row in a dataset. The
application has several tabs of information. I have a
button set up so they can save the information at any
interval. 1)Is there a way to run a method on the user
control to save just the information on the user control?
2) If not, then how can I get the information from the
user control.

I have declared the control in my code behind. Thanks in
advance.
 
You can expose additional public properties and methods from your user
control, depending on the approach that you want to take. For example, you
may want to expose a public void SaveData() function that will persist the
relevant data. Alternately, you could put up some public properties (I
wouldn't make your member variables externally accessible) that allow you to
get at the information you need from the control. Remember, a type that
derives from System.Web.UI.UserControl is still a type, and you can extend
them as necessary.
 
Here is the code from the user control method.
Public Sub SaveOrder()

Dim order As New Orders
Dim _OrderData As New OrderData

If
CStr(_OrderData.Tables(OrderData.OrderHistory_TABLE).Rows(0)(OrderData.O
rderID_FIELD)) = "new" Then

_OrderID = order.CreateOrderID

Dim dr As DataRow =
_OrderData.Tables(OrderData.OrderHistory_TABLE).NewRow

dr(OrderData.OrderDate_FIELD) = CDate(Me.RecievedDate.Text)
dr(OrderData.OrderTypeID_FIELD) =
Me.dropOrderType.SelectedItem.Value
dr(OrderData.CurrencyID_FIELD) =
Me.dropCurrencyList.SelectedItem.Value
dr(OrderData.PurchOrderNumber_FIELD) =
Me.txtPurchaseOrderNum.Text
dr(OrderData.ReqDelDate_FIELD) = CDate(Me.ReqDelvDate.Text)
dr(OrderData.SpecialInstructions_FIELD) =
Me.txtSpecialInstructions

_OrderData.Tables(OrderData.OrderHistory_TABLE).Rows.Add(dr)
Else

End If

End Sub

Here is the code from the aspx page behind.

Imports SulfaTreat.Common.Data
Imports SulfaTreat.BusinessRules

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents GenInfo As
SulfaTreat.MySulfaTreat.OnlineOrders.GeneralInformation

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Session("OrderID") = "new"
GenInfo.SaveOrder()

End Sub
End Class

When I try this is the error I get.
Object reference not set to an instance of an object.
 
You have a number of objects occurring, each of which could be a null
reference. You'll have to step through your code and determine where you are
throwing this exception. I don't think it's being thrown at the
GenInfo.SaveOrder line because at this point all of the objects on the page
will exist and you should hold a valid reference here.
 
That is exactly where it is throwing it that is what is what is stumping
me.

Server Error in '/OnlineOrders' Application.
------------------------------------------------------------------------
--------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:


Line 30:
Line 31: Session("OrderID") = "new"
Line 32: GenInfo.SaveOrder()
Line 33:
Line 34: End Sub


Source File: c:\inetpub\wwwroot\OnlineOrders\WebForm1.aspx.vb Line:
32

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
SulfaTreat.MySulfaTreat.OnlineOrders.WebForm1.Button1_Click(Object
sender, EventArgs e) in
c:\inetpub\wwwroot\OnlineOrders\WebForm1.aspx.vb:32
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.Rai
sePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
 
Interesting - well, I can vouch for the fact that it can work, so we just
need to figure out what is different about what you are doing. I don't
"speak" VB.NET, so there are some syntactical differences that aren't
immediately obvious to me. Can you give the (sterilized) source of your aspx
file so we can see the entire picture? Also, as a sanity check, if you put a
breakpoint at that line and then print the value of that control at the
command line, does it return null in the immediate window?
 
Here are the files

GeneralInformation.ascx

<%@ Register TagPrefix="igsch"
Namespace="Infragistics.WebUI.WebSchedule"
Assembly="Infragistics.WebUI.WebDateChooser.v1" %>
<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="GeneralInformation.ascx.vb"
Inherits="SulfaTreat.MySulfaTreat.OnlineOrders.GeneralInformation"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<%@ Register TagPrefix="date" Namespace="PeterBlum.DateTextBoxControls"
Assembly="DateTextBoxControls" %>
<asp:validationsummary id="svalGeneralInfo" Height="16px" Width="152px"
runat="server"></asp:validationsummary>
<table width="90%"> <!-- style="Z-INDEX: 102; LEFT: 8px; POSITION:
absolute; TOP: 56px">-->
<TR>
<TD noWrap>
<TABLE style="WIDTH: 344px; HEIGHT: 138px">
<TR>
<TD>Order Type:</TD>
<TD><asp:dropdownlist id="dropOrderType"
Runat="server"></asp:dropdownlist></TD>
</TR>
<TR>
<TD noWrap>Date Received:</TD>
<TD><igsch:webdatechooser id="RecievedDate" Width="153"
runat="server">
<CalendarLayout PrevMonthImageUrl="ig_cal_grayP0.gif"
ShowTitle="False" NextMonthImageUrl="ig_cal_grayN0.gif">
<DayStyle BorderWidth="1px" BorderColor="#909090"
BorderStyle="Solid" BackgroundImage="ig_cal_light3.gif"></DayStyle>
<FooterStyle Height="16pt" Font-Size="8pt" ForeColor="#707070"
BackgroundImage="ig_cal_light2.gif"></FooterStyle>
<SelectedDayStyle ForeColor="Black"
BackgroundImage="ig_cal_light2.gif"></SelectedDayStyle>
<OtherMonthDayStyle ForeColor="#909090"></OtherMonthDayStyle>
<NextPrevStyle
BackgroundImage="ig_cal_light1.gif"></NextPrevStyle>
<CalendarStyle BorderWidth="1px" Font-Size="9pt"
Font-Names="Verdana" BorderColor="Gray" BorderStyle="Solid"
ForeColor="#505050" BackColor="#C0C0C0"></CalendarStyle>
<TodayDayStyle ForeColor="Black"
BackgroundImage="ig_cal_light1.gif"></TodayDayStyle>
<DayHeaderStyle Height="1pt" Font-Size="8pt" Font-Bold="True"
ForeColor="#606060" BackgroundImage="ig_cal_light2.gif"
BackColor="#C0C0C0"></DayHeaderStyle>
<TitleStyle Height="18pt" Font-Size="10pt" Font-Bold="True"
ForeColor="#606060" BackgroundImage="ig_cal_light1.gif"
BackColor="#E0E0E0"></TitleStyle>
</CalendarLayout>
<ExpandEffects ShadowColor="LightGray"></ExpandEffects>
</igsch:webdatechooser></TD>
</TR>
<TR>
<TD>Currency:</TD>
<TD><asp:dropdownlist id="dropCurrencyList" Width="88px"
runat="server"></asp:dropdownlist></TD>
</TR>
<TR>
<TD>Purchase Order #:</TD>
<TD><asp:textbox id="txtPurchaseOrderNum" Width="153px"
runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD noWrap>Req. Delivery Date:</TD>
<TD><igsch:webdatechooser id="ReqDelvDate" Width="153"
runat="server" NullDateLabel=" ">
<CalendarLayout PrevMonthImageUrl="ig_cal_grayP0.gif"
ShowTitle="False" NextMonthImageUrl="ig_cal_grayN0.gif">
<DayStyle BorderWidth="1px" BorderColor="#909090"
BorderStyle="Solid" BackgroundImage="ig_cal_light3.gif"></DayStyle>
<FooterStyle Height="16pt" Font-Size="8pt" ForeColor="#707070"
BackgroundImage="ig_cal_light2.gif"></FooterStyle>
<SelectedDayStyle ForeColor="Black"
BackgroundImage="ig_cal_light2.gif"></SelectedDayStyle>
<OtherMonthDayStyle ForeColor="#909090"></OtherMonthDayStyle>
<NextPrevStyle
BackgroundImage="ig_cal_light1.gif"></NextPrevStyle>
<CalendarStyle BorderWidth="1px" Font-Size="9pt"
Font-Names="Verdana" BorderColor="Gray" BorderStyle="Solid"
ForeColor="#505050" BackColor="#C0C0C0"></CalendarStyle>
<TodayDayStyle ForeColor="Black"
BackgroundImage="ig_cal_light1.gif"></TodayDayStyle>
<DayHeaderStyle Height="1pt" Font-Size="8pt" Font-Bold="True"
ForeColor="#606060" BackgroundImage="ig_cal_light2.gif"
BackColor="#C0C0C0"></DayHeaderStyle>
<TitleStyle Height="18pt" Font-Size="10pt" Font-Bold="True"
ForeColor="#606060" BackgroundImage="ig_cal_light1.gif"
BackColor="#E0E0E0"></TitleStyle>
</CalendarLayout>
<ExpandEffects ShadowColor="LightGray"></ExpandEffects>
</igsch:webdatechooser></TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD>Special Instructions:</TD>
</TR>
<TR>
<TD><asp:textbox id="txtSpecialInstructions" Height="114px"
Width="280px" Runat="server" TextMode="MultiLine"></asp:textbox></TD>
</TR>
</TABLE>
</TD>
</TR>
</table>
<date:datetextboxvalidator id="dvalDateReceived" runat="server"
ErrorMessage="Please enter or select a valid date."
Display="None"
ControlToValidate="RecievedDate"></date:datetextboxvalidator><date:datet
extboxvalidator id="dvalReqDelivDate" runat="server"
ErrorMessage="Please enter or select a valid date."
Display="None"
ControlToValidate="ReqDelvDate"></date:datetextboxvalidator><asp:button
id="Submit" runat="server" Text="Button" Visible="False"></asp:button>

GeneralInformation.ascx.vb
Imports System.Diagnostics
Imports SulfaTreat.Common.Data
Imports SulfaTreat.BusinessRules

Public Class GeneralInformation
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Public WithEvents txtSpecialInstructions As
System.Web.UI.WebControls.TextBox
Public WithEvents dropCurrencyList As
System.Web.UI.WebControls.DropDownList
Public WithEvents txtPurchaseOrderNum As
System.Web.UI.WebControls.TextBox
Protected WithEvents dvalReqDelivDate As
PeterBlum.DateTextBoxControls.DateTextBoxValidator
Protected WithEvents svalGeneralInfo As
System.Web.UI.WebControls.ValidationSummary
Public WithEvents dropOrderType As
System.Web.UI.WebControls.DropDownList
Protected WithEvents dvalDateReceived As
PeterBlum.DateTextBoxControls.DateTextBoxValidator
Public WithEvents ReqDelvDate As
Infragistics.WebUI.WebSchedule.WebDateChooser
Public WithEvents RecievedDate As
Infragistics.WebUI.WebSchedule.WebDateChooser
Public WithEvents Submit As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

#Region "Enum"

Public Enum CurrencyType As Integer
US
Canadian
End Enum

#End Region

Private _ds As DataSet
Private _OrderData As OrderData
Private _OrderID As String



#Region "Properties"

Public ReadOnly Property OrderID() As String
Get
If Not IsNothing(Session("OrderID")) Then
Return CStr(Session("OrderID"))
Else
Return Nothing
End If
End Get
End Property

Public Property OrderTypeID() As Integer
Get
If dropOrderType.SelectedIndex <> -1 Then
Return CInt(dropOrderType.SelectedItem.Value)
Else
dropOrderType.SelectedIndex = 0
Return CInt(dropOrderType.SelectedItem.Value)
End If
End Get
Set(ByVal Value As Integer)
dropOrderType.SelectedIndex = Value
End Set
End Property

Public Property DateRecieved() As Date
Get
If dvalDateReceived.IsValid = False Then

SulfaTreat.SystemSupport.ApplicationLog.WriteError("Please select a
valid date.")
Else
Return CDate(Me.RecievedDate.Text)
End If
End Get
Set(ByVal Value As Date)
Me.RecievedDate.Value = Value
End Set
End Property

Public Property CurrencyID() As Integer
Get
Return CInt(dropCurrencyList.SelectedItem.Value)
End Get
Set(ByVal Value As Integer)
dropCurrencyList.SelectedIndex = Value
End Set
End Property

Public Property PurchaseOrderNum() As String
Get
Return txtPurchaseOrderNum.Text
End Get
Set(ByVal Value As String)
txtPurchaseOrderNum.Text = Value
End Set
End Property

Public Property ReqDevliveryDate() As Date
Get
If Me.dvalReqDelivDate.IsValid = False Then

SulfaTreat.SystemSupport.ApplicationLog.WriteError("Please select a
valid date.")
Else
Return CDate(Me.ReqDelvDate.Text)
End If
End Get
Set(ByVal Value As Date)
Me.ReqDelvDate.Value = Value
End Set
End Property

Public Property SpecialInstructions() As String
Get
Return txtSpecialInstructions.Text
End Get
Set(ByVal Value As String)
txtSpecialInstructions.Text = Value
End Set
End Property

Private Property CurrencyList() As DataSet
Get
If Not IsNothing(HttpContext.Current.Cache("CurrencyList"))
Then
Return CType(HttpContext.Current.Cache("CurrencyList"),
DataSet)
Else
Me.LoadCurrencyList()
Return _ds
End If
End Get
Set(ByVal Value As DataSet)
HttpContext.Current.Cache("CurrencyList") = Value
End Set
End Property

Private Property OrderTypeList() As DataSet
Get
If Not IsNothing(HttpContext.Current.Cache("OrderTypeList"))
Then
Return CType(HttpContext.Current.Cache("OrderTypeList"),
DataSet)
Else
LoadOrderTypeList()
Return _ds
End If
End Get
Set(ByVal Value As DataSet)
HttpContext.Current.Cache("OrderTypeList") = Value
End Set
End Property

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
Me.RecievedDate.Value = Date.Today
LoadDropLists()
End If

End Sub

Private Sub LoadDropLists()

With Me.dropCurrencyList
.DataSource = Me.CurrencyList
.DataTextField = "Currency"
.DataValueField = "CurrencyID"
.DataBind()
End With

With Me.dropOrderType
.DataSource =
Me.OrderTypeList.Tables(OrderData.OrderTypes_TABLE)
.DataTextField = OrderData.OrderTypeDescription_FIELD
.DataValueField = OrderData.OrderTypeID_FIELD
.DataBind()
End With

End Sub

Private Sub LoadCurrencyList()

Dim ord As New Orders

_ds = ord.GetCurrencyList()

HttpContext.Current.Cache("CurrencyList") = _ds

End Sub

Private Sub LoadOrderTypeList()

Dim ord As New Orders

_ds = ord.GetOrderTypes()

HttpContext.Current.Cache("OrderTypeList") = _ds

End Sub

Public Sub Clear()

Me.RecievedDate.Value = Date.Today
Me.dropCurrencyList.SelectedIndex = 0
Me.dropOrderType.SelectedIndex = 0
Me.ReqDelvDate.Value = ""
Me.txtPurchaseOrderNum.Text = ""
Me.txtSpecialInstructions.Text = ""

End Sub

Public Sub SaveOrder()

Dim order As New Orders
Dim _OrderData As New OrderData

If
CStr(_OrderData.Tables(OrderData.OrderHistory_TABLE).Rows(0)(OrderData.O
rderID_FIELD)) = "new" Then

_OrderID = order.CreateOrderID

Dim dr As DataRow =
_OrderData.Tables(OrderData.OrderHistory_TABLE).NewRow

dr(OrderData.OrderDate_FIELD) = CDate(Me.RecievedDate.Text)
dr(OrderData.OrderTypeID_FIELD) =
Me.dropOrderType.SelectedItem.Value
dr(OrderData.CurrencyID_FIELD) =
Me.dropCurrencyList.SelectedItem.Value
dr(OrderData.PurchOrderNumber_FIELD) =
Me.txtPurchaseOrderNum.Text
dr(OrderData.ReqDelDate_FIELD) = CDate(Me.ReqDelvDate.Text)
dr(OrderData.SpecialInstructions_FIELD) =
Me.txtSpecialInstructions

_OrderData.Tables(OrderData.OrderHistory_TABLE).Rows.Add(dr)
Else

End If

End Sub

Public Sub Submit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Submit.Click
SaveOrder()
End Sub
End Class

webform1.aspx

<%@ Register TagPrefix="uc1" TagName="GeneralInformation"
Src="Controls/GeneralInformation.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb"
Inherits="SulfaTreat.MySulfaTreat.OnlineOrders.WebForm1"%>
<%@ Register TagPrefix="uc1" TagName="CustomerInformation"
Src="Controls/CustomerInformation.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<uc1:GeneralInformation id="GeneralInformation1"
runat="server"></uc1:GeneralInformation></P>
<P>
<asp:Button id="Button1" runat="server"
Text="Button"></asp:Button></P>
</form>
</body>
</HTML>

webform1.aspx.vb

Imports SulfaTreat.Common.Data
Imports SulfaTreat.BusinessRules

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents GenInfo As
SulfaTreat.MySulfaTreat.OnlineOrders.GeneralInformation

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Session("OrderID") = "new"
System.Diagnostics.Debug.Write(GenInfo.ClientID)
GenInfo.SaveOrder()

End Sub
End Class
 
Back
Top