Solved

Best method to set insert field in Form View

Posted on 2011-03-21
2
409 Views
Last Modified: 2012-06-21
Hi,
I'm on the asp.net learning curve and would like advice on the best way to achieve the following please..
(1)I have a database table which lists labour rates for customers. It consists of id / customer_id and rate_id.
(2) I've setup a gridview control to list the labour rates by customer
(3) I've added a dropdown to filter the gridview - so we see only one customer's rates at any time.
(4) I added a Form view control to insert a new record, i.e. adding a new rate_id for the chosen customer_id.
I don't want the user to have to worry about selecting a customer when adding a new record as they've already selected the customer in the drop-down - so I'm now unsure how to automatically enter the current customer_id from the drop-down into the insert statement. (My attached code below just contains 2 textboxes on the form for testing.)
(I started looking at assigning the formview customer_id textbox in the onchange event of the dropdown but didn't get very far)
Any guidance appreciated. Thanks
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="admin_customer_rates.aspx.vb" Inherits="admin_customer_rates" 
    MasterPageFile="MasterPage.master" title="Admin:Customer Labour Rates"%>

<asp:Content ContentPlaceHolderId="Main" runat="server">
    <h2>Admin: Customer Labour Rates</h2>
    <div>
        <asp:Label ID="Label1" runat="server" Text="Select Customer"></asp:Label>
        <asp:DropDownList ID="ddl_customers1" runat="server" AutoPostBack="True DataSourceID="sql_customers" DataTextField="description" DataValueField="id">
        </asp:DropDownList>
    
        <asp:FormView ID="FormView1" runat="server" 
            DataSourceID="sql_customer_labour_rates" DefaultMode="Insert" Width="196px">
            <InsertItemTemplate>
                Add Labour Rate:<asp:TextBox ID="TextBox1" runat="server" 
                    Text='<%# Bind("rate_id") %>'></asp:TextBox>
                &nbsp;Customer id:
                <asp:TextBox ID="txt_customer_id" runat="server">
                </asp:TextBox>
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Save" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
        </asp:FormView>
    
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" 
        DataSourceID="sql_customer_labour_rates">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="customer_id" HeaderText="Customer" 
                SortExpression="customer_id" />
            <asp:BoundField DataField="rate_description" HeaderText="Labour Rate" 
                SortExpression="rate_description" />
        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="sql_customer_labour_rates" runat="server" 
        ConnectionString="<%$ ConnectionStrings:responseConnectionString %>" 
        DeleteCommand="DELETE FROM [customer_labour_rates] WHERE [id] = @original_id AND [customer_id] = @original_customer_id AND [rate_id] = @original_rate_id" 
        InsertCommand="INSERT INTO [customer_labour_rates] ([customer_id], [rate_id]) VALUES (@customer_id, @rate_id)" 
        SelectCommand="SELECT customer_labour_rates.id, customer_labour_rates.customer_id, customer_labour_rates.rate_id, labour_rates.rate_description FROM customer_labour_rates INNER JOIN labour_rates ON customer_labour_rates.rate_id = labour_rates.id WHERE (customer_labour_rates.customer_id = @customer_id) ORDER BY customer_labour_rates.customer_id" 
        
        UpdateCommand="UPDATE [customer_labour_rates] SET [customer_id] = @customer_id, [rate_id] = @rate_id WHERE [id] = @original_id AND [customer_id] = @original_customer_id AND [rate_id] = @original_rate_id" 
        ConflictDetection="CompareAllValues" 
        OldValuesParameterFormatString="original_{0}">
        <DeleteParameters>
            <asp:Parameter Name="original_id" Type="Int32" />
            <asp:Parameter Name="original_customer_id" Type="Int32" />
            <asp:Parameter Name="original_rate_id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="customer_id" Type="Int32" />
            <asp:Parameter Name="rate_id" Type="Int32" />
        </InsertParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="ddl_customers1" Name="customer_id" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="customer_id" Type="Int32" />
            <asp:Parameter Name="rate_id" Type="Int32" />
            <asp:Parameter Name="original_id" Type="Int32" />
            <asp:Parameter Name="original_customer_id" Type="Int32" />
            <asp:Parameter Name="original_rate_id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sql_customers" runat="server" 
        ConnectionString="<%$ ConnectionStrings:responseConnectionString %>" 
        SelectCommand="SELECT * FROM [customers] ORDER BY [description]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="sql_labour_rates" runat="server" 
        ConnectionString="<%$ ConnectionStrings:responseConnectionString %>" 
        SelectCommand="SELECT [id], [rate_description] FROM [labour_rates] ORDER BY [rate_description]">
    </asp:SqlDataSource>

</asp:Content>

Open in new window

0
Comment
Question by:nigelr99
2 Comments
 
LVL 3

Accepted Solution

by:
chrisrbloom earned 250 total points
ID: 35185712
If your dropdown list is bound to customer_id, then can't you just use that value in your insert function?
0
 

Author Closing Comment

by:nigelr99
ID: 35263541
Of course! I think referencing the dropdown control was the major issue but solution as follows:

Protected Sub FormView2_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs)
   Dim ctContent As Control
   Dim hourly_rate As Single, rate_description As String

   ctContent = Me.Controls(0).FindControl("Main")
   hourly_rate = CType(FindControlRecursive(ctContent, "ddl_customer_rates"), DropDownList).SelectedValue
   rate_description = CType(FindControlRecursive(ctContent, "ddl_customer_rates"), DropDownList).SelectedItem.ToString

   e.Values("hourly_rate") = hourly_rate
   e.Values("labour_rate_description") = rate_description

End Sub
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now