Solved

Best method to set insert field in Form View

Posted on 2011-03-21
2
408 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

13 Experts available now in Live!

Get 1:1 Help Now