locked
Pre - populate DetailsView with variables RRS feed

  • Question

  • User-2075826951 posted

    I have a page with MultiView where the user selects whether the information input is to be for Army or Air personnel (they have different rank abbreviations) and when they do, the DetailsView should appear with those variables (the Edit Date, User ID and another variable) set, visible but un-editable.  The Error is saying that the 'landing' spot for the variable data is Null, "Object reference not set to an instance of an object."  I would like this all to happen in the SelectedIndexChanged Event, can make it work easily when the record is written to SQL in the ItemInserting Event - when it is too late for my purposes - and have tried the Init, PreRender, Load, DataBound and PageIndexChanging Events with the same result; I have the data but the 'landing' spot is Null.  I take it that this means the fields are not instantiated but I don’t understand why not or how to accomplish that.

    Display code:

            <asp:MultiView ID="MultiView1" runat="server">

                <asp:View ID="View1" runat="server">Select Air or Army Branch:&nbsp;

                    <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true">

                        <asp:ListItem Text="" Selected="True"></asp:ListItem>

                        <asp:ListItem Text="Air"></asp:ListItem>

                        <asp:ListItem Text="Army"></asp:ListItem>

                    </asp:DropDownList>

                </asp:View>

                <asp:View ID="View2" runat="server">

                    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%"

                        AutoGenerateRows="False" DataSourceID="SqlDataSource1" DefaultMode="Insert"

                        HorizontalAlign="Center" DataKeyNames="SSN" EnableModelValidation="True" >

                        <CommandRowStyle HorizontalAlign="Center" />

                        <Fields>

                            <asp:BoundField DataField="SSN" HeaderText="Social Security Number" SortExpression="SSN" />

                            <asp:BoundField DataField="NAMEL" HeaderText="Name Last" SortExpression="NAMEL" />

                            <asp:BoundField DataField="NAMEF" HeaderText="Name First" SortExpression="NAMEF" />

                            <asp:BoundField DataField="NAMEM" HeaderText="Name Middle" SortExpression="NAMEM" />

                            <asp:BoundField DataField="ADDR" HeaderText="Address" SortExpression="ADDR" />

                            <asp:BoundField DataField="CITY" HeaderText="City" SortExpression="CITY" />

                            <asp:BoundField DataField="STATE" HeaderText="State" SortExpression="STATE" />

                            <asp:BoundField DataField="ZIP" HeaderText="Zip Code" SortExpression="ZIP" />

                            <asp:BoundField DataField="PHONE" HeaderText="Phone Number" SortExpression="PHONE" />

                            <asp:BoundField DataField="EMAIL" HeaderText="E-Mail Address" SortExpression="EMAIL" />

                            <asp:BoundField DataField="DOB" HeaderText="Date of Birth" SortExpression="DOB" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />

                            <asp:BoundField DataField="DISCHDT" HeaderText="Discharge Date" SortExpression="DISCHDT" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />

                            <asp:BoundField DataField="COMPONENT" HeaderText="Branch / Component" SortExpression="COMPONENT" ReadOnly="false" />

                            <asp:BoundField DataField="RANK" HeaderText="Rank" SortExpression="RANK" />

                            <asp:BoundField DataField="HRANK" HeaderText="Highest Rank" SortExpression="HRANK" />

                            <asp:BoundField DataField="EDITDT" HeaderText="Edit Date" SortExpression="EDITDT" ApplyFormatInEditMode="true" DataFormatString="{0:d}" />

                            <asp:BoundField DataField="EDITUSERID" HeaderText="Edit User ID" SortExpression="EDITUSERID" ReadOnly="false" />

                            <asp:CommandField ShowInsertButton="True" />

                        </Fields>

                        <RowStyle HorizontalAlign="Left" />

                    </asp:DetailsView>

                </asp:View>

            </asp:MultiView>

    Code behind:

     

    Imports System

    Imports System.Data

    Imports System.Data.SqlClient

     

    Partial Class _01DataEntry

        Inherits System.Web.UI.Page

     

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

     

            If Not IsPostBack Then

                MultiView1.SetActiveView(View1)

            End If

     

        End Sub

     

        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged

     

            Dim sBranch As String = ddlBranch.SelectedItem.Text

     

            'None of this works as the Error says that the 'landing' textbox is Null - we have the date and userID, but where we want to put them is Null

            'Object reference not set to an instance of an object.

            Dim txtEditDt As TextBox = DetailsView1.FindControl("EDITDT")

            'Dim txtEditID As TextBox = DetailsView1.FindControl("EDITUSERID")

            'Dim txtEditDt As New TextBox

            Dim txtEditID As New TextBox

            'txtEditDt = CType(DetailsView1.FindControl("EDITDT"), TextBox)

            txtEditID = CType(DetailsView1.FindControl("EDITUSERID"), TextBox)

            txtEditDt.Text = " "

            txtEditID.Text = " "

     

            If sBranch <> "" Then

                'It would be best if all of these values could be loaded in a routine before the DetailsView is displayed

                txtEditDt.Text = Date.Now()

                txtEditID.Text = User.Identity.Name

                'If sBranch = "Air" Then

                'The following property item is declared Read Only but DV says ReadOnly = "false"

                '    DetailsView1.Fields("COMPONENT") = "SCANG"

                'Else

                '    DetailsView1.Controls("COMPONENT") = "SCARNG"

                'End If

                MultiView1.SetActiveView(View2)

            End If

     

        End Sub

     

    End Class

    Monday, January 30, 2017 8:30 PM

Answers

  • User-707554951 posted

    Hi tagtech,

    From your error message, you couldn’t access EDITDT and EDITUSERID TextBox ( I didn’t find any TextBox named EDITDT and EDITUSERID exist in your DetailsView1) by using the following code:

    If you want to use FindControl() method, You could use TemplateField inside DetailsView1 and put a TextBox Named EDITDT and EDITUSERID in each. Then you could bind data for the two text Box,

    Something as below:

     <asp:TemplateField HeaderText="Edit">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text="<%#Eval("EDITDT")%>"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Edit User ID">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text="<%#Eval("EDITUSERID")%>"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>

    Following links For your reading:

    https://www.google.com.sg/#q=how+to+access+control+in+itemtemplate+inside+DetailsView+

    Best Regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2017 8:05 AM

All replies

  • User-707554951 posted

    Hi tagtech.

    From your description, i am not clear about your problem,

    Would you please provide me with precise description or where is your problem.

    Best Regards

    Cathy

    Wednesday, February 1, 2017 9:00 AM
  • User-2075826951 posted

    Thank you for the response.  My original Code Behind was intended, including commented lines of code - things that I have tried that have not worked - to show that the problem is that the receiving field (the 'landing' spot) for the current date and User ID errors out saying that it is Null (un-instantiated?).    Where I am trying to put an Edit Date, User ID and Branch of service (Component) is declared Null (un-instantiated?).

    I will re-post the Code Behind here with commented code deleted.



        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged

            Dim sBranch As String = ddlBranch.SelectedItem.Text

            'None of this works as the Error says that the 'landing' textbox is Null - we have the date and userID, but where we want to put them is Null
            'Object reference not set to an instance of an object.

            Dim txtEditDt As New TextBox
            Dim txtEditID As New TextBox
            txtEditDt = CType(DetailsView1.FindControl("EDITDT"), TextBox)
            txtEditID = CType(DetailsView1.FindControl("EDITUSERID"), TextBox)

            If sBranch <> "" Then
                txtEditDt.Text = Date.Now()                                                       'This line Errors out as txtEditDt is declared as Nothing
                txtEditID.Text = User.Identity.Name                                            'This line Errors out as txtEditID is declared as Nothing
                MultiView1.SetActiveView(View2)
            End If

        End Sub

    Thursday, February 2, 2017 3:22 PM
  • User-707554951 posted

    Hi tagtech,

    From your error message, you couldn’t access EDITDT and EDITUSERID TextBox ( I didn’t find any TextBox named EDITDT and EDITUSERID exist in your DetailsView1) by using the following code:

    If you want to use FindControl() method, You could use TemplateField inside DetailsView1 and put a TextBox Named EDITDT and EDITUSERID in each. Then you could bind data for the two text Box,

    Something as below:

     <asp:TemplateField HeaderText="Edit">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text="<%#Eval("EDITDT")%>"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Edit User ID">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text="<%#Eval("EDITUSERID")%>"></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>

    Following links For your reading:

    https://www.google.com.sg/#q=how+to+access+control+in+itemtemplate+inside+DetailsView+

    Best Regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 7, 2017 8:05 AM
  • User-2075826951 posted

    Thank you very much for your help; the problem is now resolved.

    Display code:

     <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                    Select Air or Army Branch:&nbsp;
                    <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged = "ddlBranch_Selected">
                        <asp:ListItem Text="" Selected="True"></asp:ListItem>
                        <asp:ListItem Text="Air"></asp:ListItem>
                        <asp:ListItem Text="Army"></asp:ListItem>
                    </asp:DropDownList>
                </asp:View>
                <asp:View ID="View2" runat="server">
                    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="50%" DataSourceID="SqlDataSource1" DefaultMode="Insert"
                        HorizontalAlign="Center" DataKeyNames="SSN" AutoGenerateRows="False" EnableModelValidation="True" >
                        <CommandRowStyle HorizontalAlign="Center" />
                        <Fields>
                            <asp:TemplateField HeaderText="Social Security Number" SortExpression="SSN">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtSSN" runat="server" Text='<%# Bind("SSN") %>'></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblSSN" runat="server" Text='<%# Bind("SSN") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="NAMEL" HeaderText="Name Last" SortExpression="NAMEL" />
                            <asp:BoundField DataField="NAMEF" HeaderText="Name First" SortExpression="NAMEF" />
                            <asp:BoundField DataField="NAMEM" HeaderText="Name Middle" SortExpression="NAMEM" />
                            <asp:BoundField DataField="ADDR" HeaderText="Address" SortExpression="ADDR" />
                            <asp:BoundField DataField="CITY" HeaderText="City" SortExpression="CITY" />
                            <asp:BoundField DataField="STATE" HeaderText="State" SortExpression="STATE" />
                            <asp:BoundField DataField="ZIP" HeaderText="Zip Code" SortExpression="ZIP" />
                            <asp:BoundField DataField="PHONE" HeaderText="Phone Number" SortExpression="PHONE" />
                            <asp:BoundField DataField="EMAIL" HeaderText="E-Mail Address" SortExpression="EMAIL" />
                            <asp:BoundField DataField="DOB" HeaderText="Date of Birth" SortExpression="DOB" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:BoundField DataField="DISCHDT" HeaderText="Discharge Date" SortExpression="DISCHDT" ApplyFormatInEditMode="True" DataFormatString="{0:d}" />
                            <asp:TemplateField HeaderText="Branch / Component" SortExpression="COMPONENT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtCmpt" runat="server" Text='<%# Bind("COMPONENT") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCmpt" runat="server" Text='<%# Bind("COMPONENT") %>'></asp:Label>
                                </ItemTemplate>
                                <ControlStyle BackColor="LightGray" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="RANK" HeaderText="Rank" SortExpression="RANK" />
                            <asp:BoundField DataField="HRANK" HeaderText="Highest Rank" SortExpression="HRANK" />
                            <asp:BoundField DataField="EDITDT" HeaderText="Edit Date" SortExpression="EDITDT" />
                            <asp:TemplateField HeaderText="Edit Date" SortExpression="EDITDT" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditDt" runat="server" Text='<%# Bind("EDITDT", "{0:d}") %>'></asp:Label>
                                </ItemTemplate>
                                <ControlStyle BackColor="LightGray" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Edit User ID" SortExpression="EDITUSERID" ControlStyle-BackColor="LightGray">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtEditID" runat="server" Text='<%# Bind("EDITUSERID") %>' ReadOnly="true"></asp:TextBox>
                                </InsertItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblEditID" runat="server" Text='<%# Bind("EDITUSERID") %>'></asp:Label>
                                </ItemTemplate>
                                <ControlStyle BackColor="LightGray" />
                            </asp:TemplateField>
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <RowStyle HorizontalAlign="Left" />
                    </asp:DetailsView>
                </asp:View>
            </asp:MultiView>
    
    

    Code behind:

    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    
    Partial Class _01DataEntry
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                MultiView1.SetActiveView(View1)
            End If
    
        End Sub
    
        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    
            Dim sBranch As String = ddlBranch.SelectedItem.Text
            Dim txtSocSecNum As TextBox = DetailsView1.FindControl("txtSSN")
            Dim txtComp As TextBox = DetailsView1.FindControl("txtCmpt")
            Dim txtEditDat As TextBox = DetailsView1.FindControl("txtEditDt")
            Dim txtEditIDent As TextBox = DetailsView1.FindControl("txtEditID")
    
            If sBranch <> "" Then
                If sBranch = "Air" Then
                    txtComp.Text = "SCANG"
                Else
                    txtComp.Text = "SCARNG"
                End If
                txtEditDat.Text = Date.Now()
                txtEditIDent.Text = User.Identity.Name
                MultiView1.SetActiveView(View2)
                txtSocSecNum.Focus()
            End If
    
        End Sub
    
    End Class
    

    Wednesday, February 8, 2017 6:36 PM