Results 1 to 2 of 2

Thread: [RESOLVED] What Event should I be using?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Resolved [RESOLVED] What Event should I be using?

    It appears to me that as the code from 'ddlBranch_Selected' in the Code Behind works, so should the code from Record_Inserting. However, even though all of the fields in the Code Behind show the data for the SSN and PHONE that was entered, I get the following error:

    Cannot insert the value NULL into column 'SSN', table 'Retire.dbo.Pensions'; column does not allow nulls. INSERT fails.

    Since I am using the Microsoft code from:

    https://msdn.microsoft.com/en-us/lib...code-snippet-1

    as my guide, I can only think that the example shows code from where the SQL record is retrieved from the database to populate the DetailsView and that this Event is triggered against the SQL record / link and not the DetailsView record.

    So, am I wrong in my coding or my thinking? What code correction or alternate Event should I employ?

    Display Code:
    Code:
        <div style="text-align:center">
            <br />
            <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" OnItemInserting="Record_Inserting" >
                        <CommandRowStyle HorizontalAlign="Center" />
                        <Fields>
                            <asp:TemplateField HeaderText="Social Security Number" SortExpression="SSN">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtSSNumber" runat="server" Text='<%# Bind("SSN") %>' ReadOnly="true" Visible="false"></asp:TextBox>
                                    <asp:TextBox ID="txtSSN1" runat="server" MaxLength="3" Width="26px" ></asp:TextBox>
                                    <asp:Label ID="lblHyph1" runat="server" Text=" - " />
                                    <asp:TextBox ID="txtSSN2" runat="server" MaxLength="2" Width="18px" ></asp:TextBox>
                                    <asp:Label ID="lblHyph2" runat="server" Text=" - " />
                                    <asp:TextBox ID="txtSSN3" runat="server" MaxLength="4" Width="33px" ></asp:TextBox>
                                </InsertItemTemplate>
                            </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="ADDR2" HeaderText="Address 2" SortExpression="ADDR2" />
                            <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:TemplateField HeaderText="Phone Number" SortExpression="PHONE">
                                <InsertItemTemplate>
                                    <asp:TextBox ID="txtPhNumber" runat="server" Text='<%# Bind("PHONE")%>' ReadOnly="true" Visible="false"></asp:TextBox>
                                    <asp:Label ID="lblLftPrn" runat="server" Text="(" />
                                    <asp:TextBox ID="txtPhArea" runat="server" MaxLength="3" Width="26px" ></asp:TextBox>
                                    <asp:Label ID="lblRgtPrn" runat="server" Text=") " />
                                    <asp:TextBox ID="txtPhPref" runat="server" MaxLength="3" Width="26px" ></asp:TextBox>
                                    <asp:Label ID="lblHyph3" runat="server" Text=" - " />
                                    <asp:TextBox ID="txtPhNumb" runat="server" MaxLength="4" Width="33px" ></asp:TextBox>
                                </InsertItemTemplate>
                            </asp:TemplateField>
                            <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: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:BoundField DataField="DISCHARGE" HeaderText="Discharge Type" SortExpression="DISCHARGE" />
                            <asp:CommandField ShowInsertButton="True" />
                        </Fields>
                        <RowStyle HorizontalAlign="Left" />
                    </asp:DetailsView>
                </asp:View>
            </asp:MultiView>
        <%--        <asp:Label id="MessageLabel"
                      forecolor="Red"
                      runat="server"/>--%>
    
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:connString %>" 
                SelectCommand="SELECT * FROM [Pensions]" 
                InsertCommand="INSERT INTO [Pensions] ([SSN], [NAMEL], [NAMEF], [NAMEM], [ADDR], [ADDR2], [CITY], [STATE], [ZIP], [PHONE], [EMAIL], [DOB], [DISCHDT], [COMPONENT], [EDITDT], [EDITUSERID], [DISCHARGE]) VALUES (@SSN, @NAMEL, @NAMEF, @NAMEM, @ADDR, @ADDR2, @CITY, @STATE, @ZIP, @PHONE, @EMAIL, @DOB, @DISCHDT, @COMPONENT, @EDITDT, @EDITUSERID, @DISCHARGE" >
                <InsertParameters>
                    <asp:Parameter Name="SSN" Type="String" />
                    <asp:Parameter Name="NAMEL" Type="String" />
                    <asp:Parameter Name="NAMEF" Type="String" />
                    <asp:Parameter Name="NAMEM" Type="String" />
                    <asp:Parameter Name="ADDR" Type="String" />
                    <asp:Parameter Name="ADDR2" Type="String" />
                    <asp:Parameter Name="CITY" Type="String" />
                    <asp:Parameter Name="STATE" Type="String" />
                    <asp:Parameter Name="ZIP" Type="String" />
                    <asp:Parameter Name="PHONE" Type="String" />
                    <asp:Parameter Name="EMAIL" Type="String" />
                    <asp:Parameter Name="DOB" Type="String" />
                    <asp:Parameter Name="DISCHDT" Type="String" />
                    <asp:Parameter Name="COMPONENT" Type="String" />
                    <asp:Parameter Name="EDITDT" Type="DateTime" />
                    <asp:Parameter Name="EDITUSERID" Type="String" />
                    <asp:Parameter Name="DISCHARGE" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
        </div>
        <br />
        &nbsp;
        <br />
    </asp:Content>
    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("txtSSN1")
            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
    
        Protected Sub Record_Inserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
    
            Dim txtSSNAll As TextBox = DetailsView1.FindControl("txtSSNumber")
            Dim txtSSNOne As TextBox = DetailsView1.FindControl("txtSSN1")
            Dim txtSSNTwo As TextBox = DetailsView1.FindControl("txtSSN2")
            Dim txtSSNThree As TextBox = DetailsView1.FindControl("txtSSN3")
            Dim txtPhoneAll As TextBox = DetailsView1.FindControl("txtPhNumber")
            Dim txtPhoneOne As TextBox = DetailsView1.FindControl("txtPhArea")
            Dim txtPhoneTwo As TextBox = DetailsView1.FindControl("txtPhPref")
            Dim txtPhoneThree As TextBox = DetailsView1.FindControl("txtPhNumb")
    
            txtSSNAll.Text = txtSSNOne.Text + txtSSNTwo.Text + txtSSNThree.Text
            txtPhoneAll.Text = txtPhoneOne.Text + txtPhoneTwo.Text + txtPhoneThree.Text
    
            e.Values("txtSSNumber") = txtSSNAll.Text       'These two lines are redundant to the two previous - added because of the Microsoft example
            e.Values("txtPhNumber") = txtPhoneAll.Text     'These two lines are redundant to the two previous - added because of the Microsoft example
    
        End Sub
    
    
        'Protected Sub Record_Inserted(ByVal sender As Object, ByVal e As DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
    
        '    Response.Redirect("01DataEntry.aspx")
    
        'End Sub
    
    End Class

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    28

    Re: What Event should I be using?

    The code should be:
    Code:
    e.Values("SSN") = txtSSNAll.Text

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