S
Seok Bee
Hi Experts,
I am puzzled with my codes, I am trying to assign some default values to few
controls within my detailsview control. I have few detailsview controls in my
form and also call the same the Sub section. However, there is a particular
detailsview control is giving me error when I run the Sub GetDefaultValue
program. The error message is stating that "Object reference not set to an
instance of an object.". When detailsview controls are not giving this error.
<asp
etailsView ID="dvInventory_Computer"
runat="server"
AutoGenerateRows="False" CellPadding="4"
DataKeyNames="Key"
DataSourceID="dsInventory_Computer_Update"
Font-Size="Small"
ForeColor="#333333"
GridLines="None" Height="50px" Style="position:
static"
Width="800px" Visible="False"
EnableViewState="False"
OnModeChanging="Mode_Changing"
OnItemInserting="Validate_PrimaryData"
OnItemUpdating="Validate_UpdateData">
<FooterStyle BackColor="#507CD1"
Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1"
Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF"
ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Main Type"
SortExpression="MainType">
<EditItemTemplate>
<asp
ropDownList
ID="ddlMainType_Update" runat="server" AutoPostBack="True"
DataSourceID="dsMainType"
DataTextField="Description"
DataValueField="MainType" SelectedValue='<%# Bind("MainType") %>'
Style="position: static"
Width="250px">
</asp
ropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp
ropDownList
ID="ddlMainType_Insert" runat="server" AutoPostBack="True"
DataSourceID="dsMainType"
DataTextField="Description"
DataValueField="MainType" SelectedValue='<%# Bind("MainType") %>'
Style="position: static"
Width="250px">
</asp
ropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMainType"
runat="server" Text='<%# Bind("MainType") %>' Width="250px"
Font-Size="Small"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Type"
SortExpression="SubType">
<EditItemTemplate>
<asp
ropDownList
ID="ddlSubType_Update" runat="server" SelectedValue='<%# Bind("SubType") %>'
Style="position: static"
Width="250px" DataSourceID="dsSubType" DataTextField="Description"
DataValueField="SubType">
</asp
ropDownList>
<asp:Label ID="lblSubType_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Select a Product Sub Type"
Visible="False"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp
ropDownList
ID="ddlSubType_Insert" runat="server" SelectedValue='<%# Bind("SubType") %>'
Style="position: static"
Width="250px" DataSourceID="dsSubType" DataTextField="Description"
DataValueField="SubType" >
</asp
ropDownList>
<asp:Label ID="lblSubType_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Select a Product Sub Type"
Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSubType"
runat="server" Text='<%# Bind("SubType") %>' Width="250px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manufacturer"
SortExpression="Manufacturer">
<EditItemTemplate>
<asp
ropDownList
ID="ddlManufacturer_Update" runat="server" DataSourceID="dsOptions_Brands"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Manufacturer") %>'
Style="position: static"
Width="250px">
</asp
ropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp
ropDownList
ID="ddlManufacturer_Insert" runat="server" DataSourceID="dsOptions_Brands"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Manufacturer") %>'
Style="position: static"
Width="250px">
</asp
ropDownList>
<asp:Label
ID="lblManufacturer_ErrMsg" runat="server" ForeColor="Red" Style="position:
static"
Text="Please key in the Product
Manufacturer" EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblManufacturer"
runat="server" Text='<%# Bind("Manufacturer") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model"
SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="txtModel_Update"
runat="server" Text='<%# Bind("Model") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblModel_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Product
Model" EnableViewState="False" Visible="False"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtModel_Insert"
runat="server" Text='<%# Bind("Model") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblModel_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Product
Model" EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblModel"
runat="server" Text='<%# Bind("Model") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial No."
SortExpression="Key">
<EditItemTemplate>
<asp:Label ID="lblKey"
runat="server" Text='<%# Eval("Key") %>' Width="250px" Font-Size="Small"
Font-Bold="True"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtKey_Insert"
runat="server" Text='<%# Bind("Key") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblKey_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Serial
No." EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblKey"
runat="server" Text='<%# Bind("Key") %>' Font-Bold="True"
Font-Size="Medium"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price
(RM)" SortExpression="UnitPrice">
<EditItemTemplate>
<asp:TextBox
ID="txtUnitPrice_Update" runat="server" Text='<%# Bind("UnitPrice") %>'
Width="100px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox
ID="txtUnitPrice_Insert" runat="server" Text='<%# Bind("UnitPrice", "{0:c}")
%>' Width="100px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUnitPrice"
runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase
Date" SortExpression="PurDate">
<EditItemTemplate>
<asp:TextBox ID="txtPurDate_Update"
runat="server" Text='<%# Bind("PurDate") %>' Width="100px"></asp:TextBox>
<asp:HyperLink
ID="hlPurDate_Computer_Update" runat="server" ImageUrl="~/cal.gif"
NavigateUrl="javascript:NewCal('dvInventory_Computer_txtPurDate_Update','ddmmmyyyy')"
Style="position:
static">Calendar</asp:HyperLink>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtPurDate_Insert"
runat="server" Text='<%# Bind("PurDate", "{0:dd-MMM-yyyy}") %>'
Width="100px"></asp:TextBox>
<asp:HyperLink
ID="hlPurDate_Computer_Insert" runat="server" ImageUrl="~/cal.gif"
NavigateUrl="javascript:NewCal('dvInventory_Computer_txtPurDate_Insert','ddmmmyyyy')"
Style="position:
static">Calendar</asp:HyperLink>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPurDate"
runat="server" Text='<%# Bind("PurDate", "{0:dd-MMM-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity"
SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity_Update"
runat="server"
Text='<%#
Bind("Quantity") %>'
Width="100px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtQuantity_Insert"
runat="server"
Text='<%#
Bind("Quantity") %>'
Width="100px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity"
runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status"
SortExpression="Status">
<EditItemTemplate>
<asp
ropDownList
ID="ddlStatus_Update" runat="server" SelectedValue='<%# Bind("Status") %>'
Style="position: static"
Width="100px" DataSourceID="dsOptions_Status"
DataTextField="Description"
DataValueField="Key">
</asp
ropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp
ropDownList
ID="ddlStatus_Insert" runat="server"
Style="position: static"
Width="100px" DataSourceID="dsOptions_Status"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Status") %>'>
</asp
ropDownList>
</InsertItemTemplate>....
Protected Sub gvInventory_Computer_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
gvInventory_Computer.SelectedIndexChanged
dvInventory_Computer.DataBind()
dvInventory_Computer.ChangeMode(DetailsViewMode.ReadOnly)
dvInventory_Computer.Visible = True
btnCancel.Visible = True
btnAddInventory.Visible = False
End Sub
Protected Sub dvInventory_Computer_PreRender(ByVal sender As Object,
ByVal e As System.EventArgs) Handles dvInventory_Computer.PreRender
Dim dv As DetailsView = CType(sender, DetailsView)
If dvInventory_Computer.CurrentMode = DetailsViewMode.Insert Then
GetDefaultValue(dv.UniqueID.ToString)
ElseIf dvInventory_Computer.CurrentMode = DetailsViewMode.Edit Then
'Checking(CType(dvInventory_Computer.FindControl("ddlMainType_Update"),
DropDownList).SelectedItem.Value,
CType(dvInventory_Computer.FindControl("ddlSubType_Update"),
DropDownList).SelectedItem.Value)
End If
End Sub
Private Sub GetDefaultValue(ByVal objMainType As Object)
Select Case (objMainType)
Case "dvInventory_Computer"
CType(dvInventory_Computer.FindControl("ddlMainType_Insert"),
DropDownList).SelectedValue = ddlMainType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("ddlSubType_Insert"),
DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("txtUnitPrice_Insert"), TextBox).Text
= "0.00"
CType(dvInventory_Computer.FindControl("txtQuantity_Insert"), TextBox).Text =
"1"
'If UCase(strSubType) <> "Notebook" Then
'
CType(dvInventory_Computer.FindControl("ddlModemManufacturer_Insert"),
DropDownList).Enabled = False
'
CType(dvInventory_Computer.FindControl("txtModemModel_Insert"),
TextBox).Enabled = False
'
CType(dvInventory_Computer.FindControl("txtPowerAdapter_Insert"),
TextBox).Enabled = False
'End If
End Select
End Sub
Can any of you give me some tips/advise on this?
Many thanks in advance
Regards,
SB
I am puzzled with my codes, I am trying to assign some default values to few
controls within my detailsview control. I have few detailsview controls in my
form and also call the same the Sub section. However, there is a particular
detailsview control is giving me error when I run the Sub GetDefaultValue
program. The error message is stating that "Object reference not set to an
instance of an object.". When detailsview controls are not giving this error.
<asp
runat="server"
AutoGenerateRows="False" CellPadding="4"
DataKeyNames="Key"
DataSourceID="dsInventory_Computer_Update"
Font-Size="Small"
ForeColor="#333333"
GridLines="None" Height="50px" Style="position:
static"
Width="800px" Visible="False"
EnableViewState="False"
OnModeChanging="Mode_Changing"
OnItemInserting="Validate_PrimaryData"
OnItemUpdating="Validate_UpdateData">
<FooterStyle BackColor="#507CD1"
Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1"
Font-Bold="True" />
<EditRowStyle BackColor="#2461BF" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF"
ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Main Type"
SortExpression="MainType">
<EditItemTemplate>
<asp
ID="ddlMainType_Update" runat="server" AutoPostBack="True"
DataSourceID="dsMainType"
DataTextField="Description"
DataValueField="MainType" SelectedValue='<%# Bind("MainType") %>'
Style="position: static"
Width="250px">
</asp
</EditItemTemplate>
<InsertItemTemplate>
<asp
ID="ddlMainType_Insert" runat="server" AutoPostBack="True"
DataSourceID="dsMainType"
DataTextField="Description"
DataValueField="MainType" SelectedValue='<%# Bind("MainType") %>'
Style="position: static"
Width="250px">
</asp
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMainType"
runat="server" Text='<%# Bind("MainType") %>' Width="250px"
Font-Size="Small"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sub Type"
SortExpression="SubType">
<EditItemTemplate>
<asp
ID="ddlSubType_Update" runat="server" SelectedValue='<%# Bind("SubType") %>'
Style="position: static"
Width="250px" DataSourceID="dsSubType" DataTextField="Description"
DataValueField="SubType">
</asp
<asp:Label ID="lblSubType_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Select a Product Sub Type"
Visible="False"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp
ID="ddlSubType_Insert" runat="server" SelectedValue='<%# Bind("SubType") %>'
Style="position: static"
Width="250px" DataSourceID="dsSubType" DataTextField="Description"
DataValueField="SubType" >
</asp
<asp:Label ID="lblSubType_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Select a Product Sub Type"
Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSubType"
runat="server" Text='<%# Bind("SubType") %>' Width="250px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manufacturer"
SortExpression="Manufacturer">
<EditItemTemplate>
<asp
ID="ddlManufacturer_Update" runat="server" DataSourceID="dsOptions_Brands"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Manufacturer") %>'
Style="position: static"
Width="250px">
</asp
</EditItemTemplate>
<InsertItemTemplate>
<asp
ID="ddlManufacturer_Insert" runat="server" DataSourceID="dsOptions_Brands"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Manufacturer") %>'
Style="position: static"
Width="250px">
</asp
<asp:Label
ID="lblManufacturer_ErrMsg" runat="server" ForeColor="Red" Style="position:
static"
Text="Please key in the Product
Manufacturer" EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblManufacturer"
runat="server" Text='<%# Bind("Manufacturer") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model"
SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="txtModel_Update"
runat="server" Text='<%# Bind("Model") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblModel_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Product
Model" EnableViewState="False" Visible="False"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtModel_Insert"
runat="server" Text='<%# Bind("Model") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblModel_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Product
Model" EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblModel"
runat="server" Text='<%# Bind("Model") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial No."
SortExpression="Key">
<EditItemTemplate>
<asp:Label ID="lblKey"
runat="server" Text='<%# Eval("Key") %>' Width="250px" Font-Size="Small"
Font-Bold="True"></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtKey_Insert"
runat="server" Text='<%# Bind("Key") %>' Width="250px"></asp:TextBox>
<asp:Label ID="lblKey_ErrMsg"
runat="server" ForeColor="Red" Style="position: static"
Text="Please key in the Serial
No." EnableViewState="False" Visible="False"></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblKey"
runat="server" Text='<%# Bind("Key") %>' Font-Bold="True"
Font-Size="Medium"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price
(RM)" SortExpression="UnitPrice">
<EditItemTemplate>
<asp:TextBox
ID="txtUnitPrice_Update" runat="server" Text='<%# Bind("UnitPrice") %>'
Width="100px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox
ID="txtUnitPrice_Insert" runat="server" Text='<%# Bind("UnitPrice", "{0:c}")
%>' Width="100px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUnitPrice"
runat="server" Text='<%# Bind("UnitPrice", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase
Date" SortExpression="PurDate">
<EditItemTemplate>
<asp:TextBox ID="txtPurDate_Update"
runat="server" Text='<%# Bind("PurDate") %>' Width="100px"></asp:TextBox>
<asp:HyperLink
ID="hlPurDate_Computer_Update" runat="server" ImageUrl="~/cal.gif"
NavigateUrl="javascript:NewCal('dvInventory_Computer_txtPurDate_Update','ddmmmyyyy')"
Style="position:
static">Calendar</asp:HyperLink>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtPurDate_Insert"
runat="server" Text='<%# Bind("PurDate", "{0:dd-MMM-yyyy}") %>'
Width="100px"></asp:TextBox>
<asp:HyperLink
ID="hlPurDate_Computer_Insert" runat="server" ImageUrl="~/cal.gif"
NavigateUrl="javascript:NewCal('dvInventory_Computer_txtPurDate_Insert','ddmmmyyyy')"
Style="position:
static">Calendar</asp:HyperLink>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblPurDate"
runat="server" Text='<%# Bind("PurDate", "{0:dd-MMM-yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity"
SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity_Update"
runat="server"
Text='<%#
Bind("Quantity") %>'
Width="100px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="txtQuantity_Insert"
runat="server"
Text='<%#
Bind("Quantity") %>'
Width="100px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity"
runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status"
SortExpression="Status">
<EditItemTemplate>
<asp
ID="ddlStatus_Update" runat="server" SelectedValue='<%# Bind("Status") %>'
Style="position: static"
Width="100px" DataSourceID="dsOptions_Status"
DataTextField="Description"
DataValueField="Key">
</asp
</EditItemTemplate>
<InsertItemTemplate>
<asp
ID="ddlStatus_Insert" runat="server"
Style="position: static"
Width="100px" DataSourceID="dsOptions_Status"
DataTextField="Description"
DataValueField="Key" SelectedValue='<%# Bind("Status") %>'>
</asp
</InsertItemTemplate>....
Protected Sub gvInventory_Computer_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
gvInventory_Computer.SelectedIndexChanged
dvInventory_Computer.DataBind()
dvInventory_Computer.ChangeMode(DetailsViewMode.ReadOnly)
dvInventory_Computer.Visible = True
btnCancel.Visible = True
btnAddInventory.Visible = False
End Sub
Protected Sub dvInventory_Computer_PreRender(ByVal sender As Object,
ByVal e As System.EventArgs) Handles dvInventory_Computer.PreRender
Dim dv As DetailsView = CType(sender, DetailsView)
If dvInventory_Computer.CurrentMode = DetailsViewMode.Insert Then
GetDefaultValue(dv.UniqueID.ToString)
ElseIf dvInventory_Computer.CurrentMode = DetailsViewMode.Edit Then
'Checking(CType(dvInventory_Computer.FindControl("ddlMainType_Update"),
DropDownList).SelectedItem.Value,
CType(dvInventory_Computer.FindControl("ddlSubType_Update"),
DropDownList).SelectedItem.Value)
End If
End Sub
Private Sub GetDefaultValue(ByVal objMainType As Object)
Select Case (objMainType)
Case "dvInventory_Computer"
CType(dvInventory_Computer.FindControl("ddlMainType_Insert"),
DropDownList).SelectedValue = ddlMainType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("ddlSubType_Insert"),
DropDownList).SelectedValue = ddlSubType.SelectedValue.ToString
CType(dvInventory_Computer.FindControl("txtUnitPrice_Insert"), TextBox).Text
= "0.00"
CType(dvInventory_Computer.FindControl("txtQuantity_Insert"), TextBox).Text =
"1"
'If UCase(strSubType) <> "Notebook" Then
'
CType(dvInventory_Computer.FindControl("ddlModemManufacturer_Insert"),
DropDownList).Enabled = False
'
CType(dvInventory_Computer.FindControl("txtModemModel_Insert"),
TextBox).Enabled = False
'
CType(dvInventory_Computer.FindControl("txtPowerAdapter_Insert"),
TextBox).Enabled = False
'End If
End Select
End Sub
Can any of you give me some tips/advise on this?
Many thanks in advance
Regards,
SB