locked
Dropdownlist with a concatenated value to gridview?? RRS feed

  • Question

  • User-2109039660 posted

    Hi,

    I have a page that I want to be able to amend certain user details.

    The page starts with a drop down list where I query a table that holds the first and lastname of the users from an Access database. I concatenate the results so the full name appears in the drop down.

    Then the problem comes when I click on a name I need it to populate a GridView with some of the details about that user with a Select and Delete button.

    Because I'm using this as a query for the drop down and combining the two values the subsequent gridview query fails to take the value.

    SelectCommand="SELECT FirstName + ' ' + LastName AS FullName FROM tblUserProfiles ORDER BY FirstName">

    Gridview query

    SelectCommand="SELECT aspnet_Users.UserId, tblUserProfiles.FirstName + ' ' + tblUserProfiles.LastName AS FullName, aspnet_Roles.RoleName FROM (((aspnet_Users INNER JOIN tblUserProfiles ON aspnet_Users.UserId = tblUserProfiles.UserId) INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId) INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId) WHERE (FullName = '@FullName')"

    I just wondered if you have any ideas what the problem could be, Many thanks in advance.

    Cheers Rob.

    Here is the full page and code details.

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
        CodeFile="AmendUsers.aspx.cs" Inherits="Admin_AmendUsers" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <div id="page">
            <div id="content">
                <div class="post">
                    <center>
                        <div class="entry">
                            <div class="entry-btm">
                                <!-- Start of page content --->
                                <div class="contactForm">
                                    <b>Choose a Name:</b>
                                    <asp:DropDownList 
                                        ID="DropDownList1"
                                        DataSourceID="AccessDataSource1" 
                                        AutoPostBack="true"
                                        DataTextField="FullName" 
                                        runat="server" 
                                        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
                                        CssClass="input" />
                                    <asp:AccessDataSource 
                                        ID="AccessDataSource1" 
                                        runat="server" 
                                        DataFile="~/App_Data/ASPNetDB.mdb"
                                        SelectCommand="SELECT FirstName + ' ' + LastName AS FullName FROM tblUserProfiles ORDER BY FirstName">
                                    </asp:AccessDataSource>
                                    <br />
                                    <br />
                                    <table>
                                        <tr>
                                            <td valign="top" align="left" style="padding: 4px">
                                                <asp:GridView 
                                                    ID="GridView1"
                                                    Runat="server"
                                                    DataSourceID="AccessDataSource2"
                                                    DataKeyNames="UserId"
                                                    AutoGenerateColumns="False"
                                                    Width="500px" 
                                                    SelectedIndex="0" 
                                                    OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                                                    OnPageIndexChanged="GridView1_PageIndexChanged" 
                                                    OnRowDeleted="GridView1_RowDeleted"
                                                    OnSorted="GridView1_Sorted" 
                                                    GridLines="None">
                                                    <Columns>
                                                        <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
                                                        <asp:BoundField 
                                                            DataField="UserId" 
                                                            HeaderText="UserId" 
                                                            SortExpression="UserId" 
                                                            InsertVisible="False" />
                                                        <asp:BoundField 
                                                            DataField="FullName" 
                                                            HeaderText="Name" 
                                                            SortExpression="FullName" />
                                                        <asp:BoundField 
                                                            DataField="RoleName" 
                                                            HeaderText="RoleName" 
                                                            SortExpression="RoleName" />
                                                    </Columns>
                                                </asp:GridView>
                                                <asp:AccessDataSource 
                                                    ID="AccessDataSource2"                     
                                                    runat="server" 
                                                    DataFile="~/App_Data/ASPNetDB.mdb" 
                                                    SelectCommand="SELECT aspnet_Users.UserId, tblUserProfiles.FirstName + ' ' + tblUserProfiles.LastName AS FullName, aspnet_Roles.RoleName FROM (((aspnet_Users INNER JOIN tblUserProfiles ON aspnet_Users.UserId = tblUserProfiles.UserId) INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId) INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId) WHERE (tblUserProfiles.LastName = '@LastName')"
                                                    DeleteCommand="DELETE FROM aspnet_Users WHERE aspnet_Users = @UserId">
                                                </asp:AccessDataSource>
                                            </td>
                                            <td valign="top" align="left" nowrap="nowrap">
                                                <asp:DetailsView 
                                                    AutoGenerateRows="False" 
                                                    DataSourceID="AccessDataSource3"
                                                    DataKeyNames="UserId"
                                                    HeaderText="Member Details" 
                                                    ID="DetailsView1" 
                                                    runat="server" 
                                                    Width="300px" 
                                                    OnItemUpdated="DetailsView1_ItemUpdated"
                                                    OnItemInserted="DetailsView1_ItemInserted" 
                                                    OnDataBound="DetailsView1_DataBound" 
                                                    GridLines="None" CellSpacing="4" Font-Bold="False">
                                                    <Fields>
                                                        <asp:BoundField 
                                                            DataField="UserId" 
                                                            HeaderText="User Id" 
                                                            SortExpression="UserId" 
                                                            InsertVisible="False" Visible="False" />
                                                        <asp:BoundField 
                                                            DataField="UserName" 
                                                            HeaderText="User Name" 
                                                            SortExpression="UserName" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:BoundField 
                                                            DataField="FirstName" 
                                                            HeaderText="First Name" 
                                                            SortExpression="FirstName" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:BoundField 
                                                            DataField="LastName" 
                                                            HeaderText="Last Name" 
                                                            SortExpression="LastName" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:BoundField 
                                                            DataField="RoleName" 
                                                            HeaderText="Role" 
                                                            SortExpression="RoleName" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:BoundField 
                                                            DataField="GalleryLink" 
                                                            HeaderText="Gallery Link" 
                                                            SortExpression="GalleryLink" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:BoundField 
                                                            DataField="Email" 
                                                            HeaderText="Email" 
                                                            SortExpression="Email" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:BoundField>
                                                        <asp:CheckBoxField 
                                                            DataField="IsApproved" 
                                                            HeaderText="IsApproved" 
                                                            SortExpression="IsApproved" 
                                                            ItemStyle-ForeColor="White" >
                                                        </asp:CheckBoxField>
                                                        <asp:CommandField 
                                                            ShowEditButton="True" />
                                                    </Fields>
                                                    <HeaderStyle ForeColor="White" Font-Bold="True" />
                                                </asp:DetailsView>
                                                <asp:AccessDataSource 
                                                    ID="AccessDataSource3" 
                                                    runat="server" 
                                                    DataFile="~/App_Data/ASPNetDB.mdb" 
                                                    SelectCommand="SELECT aspnet_Users.UserId, aspnet_Users.UserName, tblUserProfiles.FirstName, tblUserProfiles.LastName, aspnet_Roles.RoleName, tblUserProfiles.GalleryLink, aspnet_Membership.Email, aspnet_Membership.IsApproved FROM ((((aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId) INNER JOIN tblUserProfiles ON aspnet_Users.UserId = tblUserProfiles.UserId) INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId) INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId) WHERE (aspnet_Users.UserId = @UserId)">
                                                    <SelectParameters>
                                                        <asp:ControlParameter 
                                                            ControlID="GridView1" 
                                                            Name="UserId" 
                                                            PropertyName="SelectedValue"
                                                            Type="Int32" />
                                                    </SelectParameters>
                                               </asp:AccessDataSource>
                                            </td>
                                        </tr>
                                    </table>
                                    <br />
                                    <asp:Label 
                                        ID="ErrorMessageLabel" 
                                        EnableViewState="false" 
                                        runat="server" />
                                </div>
                                <!-- End of page content --->
                            </div>
                        </div>
                    </center>
                </div>
            </div>
        </div>
        <div style="clear: both;">
             </div>
    </asp:Content>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Admin_AmendUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void DetailsView1_ItemInserted(Object sender, System.Web.UI.WebControls.DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorMessageLabel.Text = "An error occured while entering this record.  Please verify you have entered data in the correct format.";
                e.ExceptionHandled = true;
            }
            GridView1.DataBind();
        }
    
        protected void DetailsView1_ItemUpdated(Object sender, System.Web.UI.WebControls.DetailsViewUpdatedEventArgs e)
        {
            GridView1.DataBind();
        }
    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }
    
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }
    
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }
    
        protected void GridView1_Sorted(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        }
    
        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                ErrorMessageLabel.Text = "Failed to DELETE due to foreign key contstraint on the table.  You may only delete rows which have no related records.";
                e.ExceptionHandled = true;
            }
        }
    
        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                TextBox stateTextBox = (TextBox)DetailsView1.Rows[6].Cells[1].Controls[0];
                stateTextBox.Text = DropDownList1.SelectedValue;
                stateTextBox.Enabled = false;
            }
        }
    
    
    
    }





     

    Saturday, November 27, 2010 6:35 AM

Answers

  • User-2109039660 posted

    I have it working as it should now

    I changed the WHERE to

    WHERE (aspnet_Users.UserId = @UserId)"


    and in the Controlparameter name = UserId

     

    and it all seems to be good now.

     

    many thanks for your help Hans

     

    Cheers Rob. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 27, 2010 2:33 PM

All replies

  • User-231977777 posted

    hi 

    may you show us error message .

     

    Saturday, November 27, 2010 9:29 AM
  • User-1199946673 posted

     You need to include the UserId field in the SelectCommand of the DropDownlist, and in the HTML markup of the DropDownList, set the DataValueField Property to UserId:

    <asp:DropDownList   
        ID="DropDownList1" 
        DataSourceID="AccessDataSource1"   
        AutoPostBack="true" 
        DataValueField="UserId"   
        DataTextField="FullName"   
        runat="server"   
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"   
        CssClass="input" />  
    <asp:AccessDataSource   
        ID="AccessDataSource1"   
        runat="server"   
        DataFile="~/App_Data/ASPNetDB.mdb" 
        SelectCommand="SELECT UserId, FirstName + ' ' + LastName AS FullName FROM tblUserProfiles ORDER BY FirstName">  

     

    The SelectValue property in the AccessDataSource3 SelectParameter is pointing the the DataValueField of the DropDownList,. But since you didn't set it, the DataTextField is used....

     

    Saturday, November 27, 2010 11:14 AM
  • User-2109039660 posted

    Hi Hans, thanks for replying. I added the two entries you specified as per the following:

    <asp:DropDownList 
    	ID="DropDownList1"
    	DataSourceID="AccessDataSource1" 
    	AutoPostBack="true"
    	DataValueField="UserId"
    	DataTextField="FullName" 
    	runat="server" 
    	OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="input" />
    <asp:AccessDataSource 
    	ID="AccessDataSource1" 
    	runat="server" 
    	DataFile="~/App_Data/ASPNetDB.mdb"                                    
    	SelectCommand="SELECT UserId, FirstName + ' ' + LastName AS FullName FROM tblUserProfiles ORDER BY FirstName">
    </asp:AccessDataSource>
     

    I now get this fail message No value given for one or more required parameters.

    I think possibly the problem is with the select statement for AccessDatSource2 at the WHERE statement

    SelectCommand="SELECT aspnet_Users.UserId, tblUserProfiles.FirstName + ' ' + tblUserProfiles.LastName AS FullName, aspnet_Roles.RoleName FROM (((aspnet_Users INNER JOIN tblUserProfiles ON aspnet_Users.UserId = tblUserProfiles.UserId) INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId) INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId) WHERE (FullName = '@FullName')"

    What do you think?

    Cheers Rob.



     

    Saturday, November 27, 2010 2:08 PM
  • User-1199946673 posted

    I think possibly the problem is with the select statement for AccessDatSource2 at the WHERE statement

    Yes, you don't need the quotes...

    WHERE (FullName = @FullName)

     

    Saturday, November 27, 2010 2:16 PM
  • User-2109039660 posted

    I get the same failure message  

    Saturday, November 27, 2010 2:19 PM
  • User-2109039660 posted

    I added a SelectParameter and now the gridview fills with all the records it should only display the one I clicked on from the dropdown.

     

    <asp:AccessDataSource 
    	ID="AccessDataSource2"                     
    	runat="server" 
    	DataFile="~/App_Data/ASPNetDB.mdb" 
    	SelectCommand="SELECT aspnet_Users.UserId, tblUserProfiles.FirstName + ' ' + tblUserProfiles.LastName AS FullName, aspnet_Roles.RoleName FROM (((aspnet_Users INNER JOIN tblUserProfiles ON aspnet_Users.UserId = tblUserProfiles.UserId) INNER JOIN aspnet_UsersInRoles ON aspnet_Users.UserId = aspnet_UsersInRoles.UserId) INNER JOIN aspnet_Roles ON aspnet_UsersInRoles.RoleId = aspnet_Roles.RoleId) WHERE (FullName = @FullName)"
    	DeleteCommand="DELETE FROM aspnet_Users WHERE aspnet_Users = @UserId">
    <SelectParameters>
    	<asp:ControlParameter ControlID="DropDownList1" Name="FullName" PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
    </asp:AccessDataSource>


     

    Saturday, November 27, 2010 2:27 PM
  • User-2109039660 posted

    I have it working as it should now

    I changed the WHERE to

    WHERE (aspnet_Users.UserId = @UserId)"


    and in the Controlparameter name = UserId

     

    and it all seems to be good now.

     

    many thanks for your help Hans

     

    Cheers Rob. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 27, 2010 2:33 PM