-3

I have a form with a dropdown list and 3 textbox fields. The dropdown list is supposed to populate the 3 textboxes. So far I got it to populate 2 textboxes but I don't know how I can populate the 3rd one. Here is the HTML portion:

        <EditItemTemplate>

            <div style="height:50px;overflow:hidden;border:0px;width:100%">
                <asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
                <div class="StatusAlert" style="float:left;">   Permit Picked Up</div>
                </asp:Panel>
                <div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
            </div>
            <p>
                Date:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
            </p>
                <asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="PickupDrivers" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" 
                    SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
                </asp:SqlDataSource>
            <p>
                Name:
                <asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
            </p>
                Phone:
                <asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
            <p>
                DL NO:
                <asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
            </p>
            <p>
                Contractor:
                <asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false"  DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>'  DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
                <asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
            </p>

Here is the C# portion:

protected void NameSelect_SelectedIndexChanged(object sender, EventArgs e) {

        DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
        TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
        TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
        TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;

        nameBox.Text = NameSelect.SelectedItem.ToString();
        string[] selectedValues = NameSelect.SelectedValue.Split(',');

        //phoneBox.Text = selectedValues[0];
        //DriversBox.Text = selectedValues[1];

        if (selectedValues.Length > 1)
        {
            phoneBox.Text = selectedValues[0];
            DriversBox.Text = selectedValues[1];
        }
        else
        {
            phoneBox.Text = selectedValues[0];
            DriversBox.Text = " ";
        }


    }

I had to modify the Stored procedure

CREATE PROCEDURE [ROW].[SELECT_PickupContactsDDL] @DriversNumber varchar(50) = '%', @Pickup_Name varchar(500) = '%', @Pickup_Phone varchar(50) = '%' AS BEGIN

SET NOCOUNT ON;

SELECT 
    Pickup_Name, ISNULL(Pickup_Phone,0) + ',' + ISNULL(DriversNumber,0) AS PickupDrivers, pickupDate
FROM 
    ROW.PickupContacts 
GROUP BY 
    Pickup_Name, pickupDate, Pickup_Phone, DriversNumber
ORDER BY 
    pickupDate DESC

END

user3736492
  • 3
  • 1
  • 6
  • this is where you as the coder must learn to use the debugger.. if 2 of the 3 are working then what's the difference between the 1 that is not working..? this you should have been able to discover and or tell us. – MethodMan Sep 01 '16 at 19:59
  • I would think it would be something like DriversBox.Text = NameSelect.SelectedValue.DriversNumberValue.ToString(); but what do I know I'm just a newb. – user3736492 Sep 01 '16 at 20:02
  • here is where my big question would be.. why are you trying to populate 3 different TextBoxes using a single dropdown. either join all the values with a delimiter and split out later or have 3 different dropdowns don't mix and match.. – MethodMan Sep 01 '16 at 20:05
  • I don't know how to tie the selected value with to the DriversBox value. – user3736492 Sep 01 '16 at 20:06
  • I am curious to see what your stored procedure looks like honestly in my opinion, this is a poor approach / way to handle binding data to a dropdownList – MethodMan Sep 01 '16 at 20:11
  • If you know the name of the control, you don't need to use FIND - just say phoneBox.Text = "Blah." Ooops - my bad. forgot you have the frontend. DriversBox is indeed spelled differently than the others. – Shannon Holsinger Sep 01 '16 at 20:15

1 Answers1

0

In your dropdown select stored procedure, concatenate phoneBox and DriversBox as one value. For Example, 123 - phoneBox 345 - DriversBox 123,345 - Final drop down list value

On Dropdown selection event, use Split method to show it in 3text boxes.

Stored Procedure:

ALTER PROCEDURE [ROW].[SELECT_PickupContactsDDL] 
AS 
BEGIN 
    SET NOCOUNT ON; 
        SELECT Pickup_Name, Pickup_Phone + ',' + DriversNumber AS [PickupDrivers], pickupDate FROM ROW.PickupContacts GROUP BY Pickup_Name, pickupDate, Pickup_Phone, DriversNumber ORDER BY pickupDate DESC 
END

DESIGN:

<EditItemTemplate>

            <div style="height:50px;overflow:hidden;border:0px;width:100%">
                <asp:Panel runat="server" Visible='<%# IsActive((string)Eval("Status")) %>'>
                <div class="StatusAlert" style="float:left;">   Permit Picked Up</div>
                </asp:Panel>
                <div class="StatusAlert" style="background-color:#808080;float:left">Pending Pick Up</div>
            </div>
            <p>
                Date:
                <asp:Label ID="dateLabel" runat="server" Text='<%# Bind("PickupDate") %>'></asp:Label>
            </p>
                <asp:DropDownList ID="NameSelect" runat="server" DataSourceID="PickupNameSource" AppendDataBoundItems="true" DataTextField="Pickup_Name" DataValueField="***PickupDrivers***" OnSelectedIndexChanged="NameSelect_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="SELECT CONTRACTOR" Selected="True" Value=""></asp:ListItem>
                </asp:DropDownList>
                <asp:SqlDataSource ID="PickupNameSource" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" 
                    SelectCommand="[ROW].SELECT_PickupContactsDDL" SelectCommandType="StoredProcedure">
                </asp:SqlDataSource>
            <p>
                Name:
                <asp:TextBox ID="nameBox" runat="server" Text='<%# Bind("Pickup_Name") %>'></asp:TextBox>
            </p>
                Phone:
                <asp:TextBox ID="phoneBox" runat="server" Text='<%# Bind("Pickup_Phone") %>'></asp:TextBox>
            <p>
                DL NO:
                <asp:TextBox ID="DriversBox" runat="server" Text='<%# Bind("DriversNumber") %>'></asp:TextBox>
            </p>
            <p>
                Contractor:
                <asp:DropDownList ID="contractorList" runat="server" AutoPostBack="false"  DataSourceID="SelectContractorDB" SelectedValue='<%# Bind("ContractorID") %>'  DataTextField="Company" DataValueField="ContractorID"></asp:DropDownList>
                <asp:SqlDataSource ID="SelectContractorDB" runat="server" ConnectionString="<%$ ConnectionStrings:ROW_PermitsConnectionString %>" SelectCommand="SELECT * FROM [ROW].[All_Contractor_Names]"></asp:SqlDataSource>
            </p>

Code-Behind

protected void NameSelect_SelectedIndexChanged(object sender, EventArgs e)
    {

        DropDownList NameSelect = pickupView.FindControl("NameSelect") as DropDownList;
        TextBox nameBox = pickupView.FindControl("nameBox") as TextBox;
        TextBox phoneBox = pickupView.FindControl("phoneBox") as TextBox;
        TextBox DriversBox = pickupView.FindControl("DriversBox") as TextBox;


        nameBox.Text = NameSelect.SelectedItem.ToString();
        string[] selectedValues = NameSelect.SelectedValue.Split(',');
        phoneBox.Text = selectedValues[0];
        DriversBox.Text = selectedValues[1];

    }
Ganesh
  • 26
  • 6
  • I'm trying to put 3 textboxes with 3 different values with one dropdown to fill out the form because the same names are used repeatedly, and on occasion new names are used. So I'm trying to make it so the user would just select the name instead of inputting the same information every time. So when the user selects the name off the dropdown it populates then he/she just clicks update. – user3736492 Sep 01 '16 at 20:14
  • Hi user3736492, Did you get my logic? This logic will help fix your issue. – Ganesh Sep 01 '16 at 20:28
  • Sorry Ganesh. I don't. I'm extremely new to this sort of stuff. I don't even know why or how I would have to combine the phoneBox and DriversBox as 1 Value. I tried doing it and I got a new problem. – user3736492 Sep 01 '16 at 21:08
  • Hi user3736492, Can you post your SELECT_PickupContactsDDL Stored Procedure? – Ganesh Sep 01 '16 at 21:14
  • ALTER PROCEDURE [ROW].[SELECT_PickupContactsDDL] AS BEGIN SET NOCOUNT ON; SELECT Pickup_Name, Pickup_Phone + ' ' + DriversNumber, pickupDate FROM ROW.PickupContacts GROUP BY Pickup_Name, pickupDate, Pickup_Phone, DriversNumber ORDER BY pickupDate DESC END – user3736492 Sep 01 '16 at 21:35
  • Okay Ganesh I've updated the C# section. I don't know if I have it written correctly so you may need to help me out. I apologize as I am new to this site as well and don't use it much. – user3736492 Sep 02 '16 at 14:39
  • Hi, You need to update DataValueField="PickupDrivers" in your design and try. – Ganesh Sep 02 '16 at 16:19
  • Okay Ganesh. Disregard that last comment. Here's what I got now when I select from the dropdown list: Additional information: Format of the initialization string does not conform to specification starting at index 0. – user3736492 Sep 02 '16 at 18:06
  • Hi I guess there is some issue with connection string . Please refer http://stackoverflow.com/questions/8243008/format-of-the-initialization-string-does-not-conform-to-specification-starting-a – Ganesh Sep 02 '16 at 18:57
  • I was able to fix that by adding "ROW." into the connectionstring. I also updated the stored procedure with wildcard parameters since the application was complaining about it. Please see above. Now the problem is how do I split the values into 3 textboxes on dropdown select. It's complaining about the phoneBox.Text = sdr["Pickup_Phone"].ToString(); and etc. – user3736492 Sep 02 '16 at 19:32
  • It works! But only when I select records that contain the DriversNumber otherwise I get this "Index was outside the bounds of the array". – user3736492 Sep 02 '16 at 20:48
  • Hi I added condition try now – Ganesh Sep 02 '16 at 20:56
  • The error I got is "expected catch or finally". Don't believe the try statement would work here. Also SelectedText has no definition. – user3736492 Sep 02 '16 at 21:21
  • Removed try which is not needed here and changed selected text . try now – Ganesh Sep 02 '16 at 21:30
  • Sorry I hadn't replied in a few days. I just wanted to update you on this; I finally got this feature working and thank you so much for putting me in the right direction. I couldn't have done without your help. Thanks a bunch Ganesh. – user3736492 Sep 09 '16 at 19:58