Results 1 to 6 of 6

Thread: [RESOLVED] Pre - populate DetailsView with variables

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Resolved [RESOLVED] Pre - populate DetailsView with variables

    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:
    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:
    Code:
    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
    Last edited by tagtech; Feb 2nd, 2017 at 09:58 AM.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Pre - populate DetailsView with variables

    Declare txtEditDT as a new TextBox control.

    Code:
    Dim txtEditDt As New TextBox
    and then use the logic from txtEditID with CType() statement.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: Pre - populate DetailsView with variables

    Thank you for the response. I have edited my original Code Behind to shove all commented lines of code - things that I have tried that have not worked - to the side so as to try to be more clear. I tried your suggestion, it did not work. 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.
    Code:
        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
    Last edited by tagtech; Feb 2nd, 2017 at 10:37 AM.

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Pre - populate DetailsView with variables

    Going back to your .aspx code, the fields are declared as BoundField which can't be retrieved
    using FindControl() method. So to access a field, use the Cell's Text property.

    Code:
    Dim txtEditDt As New TextBox
    txtEditDt.Text = DetailsView1.Rows(0).Cells(16).Text
    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: Pre - populate DetailsView with variables

    Quote Originally Posted by KGComputers View Post
    ... the fields are declared as BoundField which can't be retrieved
    using FindControl() method. So to access a field, use the Cell's Text property.
    - kgc
    Where does one go to learn this? My multiple books didn't address this. You are exactly right and I thank you profusely for the answer!

    Code:
        Protected Sub ddlBranch_Selected(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBranch.SelectedIndexChanged
    
            Dim sBranch As String = ddlBranch.SelectedItem.Text
    
            If sBranch <> "" Then
                DetailsView1.Rows(22).Cells(1).Text = Date.Now()
                DetailsView1.Rows(23).Cells(1).Text = User.Identity.Name
                If sBranch = "Air" Then
                    DetailsView1.Rows(13).Cells(1).Text = "SCANG"
                Else
                    DetailsView1.Rows(13).Cells(1).Text = "SCARNG"
                End If
                MultiView1.SetActiveView(View2)
            End If
    
        End Sub

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: [RESOLVED] Pre - populate DetailsView with variables

    My mistake. The code from my last post only displayed the data in the DetailsView when it was shown, it did not write the data to the SQL record. Template fields were required to effect the results I was after.

    Display code:
    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:
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width