?
Solved

Continued Query Problems.

Posted on 2006-05-09
1
Medium Priority
?
222 Views
Last Modified: 2008-01-09
Alright, in followup to my last question (which was beautifully answered) at : http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21843012.html

I am having an issue with getting the cmbEditRole/cmbEditState DropDownLists to display the currently set values when they load in edit mode. Below is my ASPX code and C# code-behind.

<%@ Control Language="C#" CodeFile="CreateEmployee.ascx.cs" Inherits="System.Web.Html.CreateEmployee" %>
<asp:SqlDataSource ID="employeeData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     DeleteCommand="DELETE FROM [Employees] WHERE [id] = @id" InsertCommand="INSERT INTO [Employees] ([firstname], [lastname], [role], [phone], [address], [city], [state], [zipcode], [password]) VALUES (@firstname, @lastname, @role, @phone, @address, @city, @state, @zipcode, @password)"
     SelectCommand="SELECT Employees.id, Employees.firstname, Employees.lastname, Employees.role, Employees.phone, Employees.address, Employees.city, Employees.state, Employees.zipcode, Employees.password, Roles.roleName, States.stateName FROM Employees INNER JOIN Roles ON Employees.role = Roles.roleid INNER JOIN States ON Employees.state = States.stateid"
     UpdateCommand="UPDATE [Employees] SET [firstname] = @firstname, [lastname] = @lastname, [role] = @role, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zipcode] = @zipcode, [password] = @password WHERE [id] = @id">
     <DeleteParameters>
          <asp:Parameter Name="id" Type="Int32" />
     </DeleteParameters>
     <UpdateParameters>
          <asp:Parameter Name="firstname" Type="String" />
          <asp:Parameter Name="lastname" Type="String" />
          <asp:Parameter Name="role" Type="Int32" />
          <asp:Parameter Name="phone" Type="String" />
          <asp:Parameter Name="address" Type="String" />
          <asp:Parameter Name="city" Type="String" />
          <asp:Parameter Name="state" Type="Int32" />
          <asp:Parameter Name="zipcode" Type="Int32" />
          <asp:Parameter Name="password" Type="String" />
          <asp:Parameter Name="id" Type="Int32" />
     </UpdateParameters>
     <InsertParameters>
          <asp:Parameter Name="firstname" Type="String" />
          <asp:Parameter Name="lastname" Type="String" />
          <asp:Parameter Name="role" Type="Int32" />
          <asp:Parameter Name="phone" Type="String" />
          <asp:Parameter Name="address" Type="String" />
          <asp:Parameter Name="city" Type="String" />
          <asp:Parameter Name="state" Type="Int32" />
          <asp:Parameter Name="zipcode" Type="Int32" />
          <asp:Parameter Name="password" Type="String" />
     </InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="roleData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     SelectCommand="SELECT [roleid], [roleName], [roleLevel] FROM [Roles]"></asp:SqlDataSource>
<asp:SqlDataSource ID="stateData" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
     SelectCommand="SELECT [stateid], [stateName] FROM [States]"></asp:SqlDataSource>
<asp:DetailsView ID="dataView" runat="server" AllowPaging="True" AutoGenerateRows="False"
     DataKeyNames="id" DataSourceID="employeeData" Height="50px" Width="125px" OnItemUpdating="OnItemUpdate">
     <Fields>
          <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
               SortExpression="id" />
          <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
          <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
          <asp:TemplateField HeaderText="role" SortExpression="role">
               <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" >
                    </asp:DropDownList>
               </EditItemTemplate>
               <InsertItemTemplate>
                    <asp:DropDownList ID="cmbInsertRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" >
                    </asp:DropDownList>
               </InsertItemTemplate>
               <ItemTemplate>
                    <asp:Label ID="lbTextRoleName" runat="server" Text='<%# Bind("roleName") %>'></asp:Label>
               </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
          <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
          <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
          <asp:TemplateField HeaderText="state" SortExpression="state">
               <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditState" runat="server" AutoPostBack="True" DataSourceID="stateData"
                         DataTextField="stateName" DataValueField="stateid" >
                    </asp:DropDownList>
               </EditItemTemplate>
               <InsertItemTemplate>
                    <asp:DropDownList ID="cmbInsertState" runat="server" AutoPostBack="True" DataSourceID="stateData"
                         DataTextField="stateName" DataValueField="stateid" >
                    </asp:DropDownList>
               </InsertItemTemplate>
               <ItemTemplate>
                    <asp:Label ID="lbTextStateName" runat="server" Text='<%# Bind("stateName") %>'></asp:Label>
               </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField DataField="zipcode" HeaderText="zipcode" SortExpression="zipcode" />
          <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
          <asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
     </Fields>
</asp:DetailsView>


using System;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace System.Web.Html
{
      /// <summary>
      /// Represents the usercontrol to create a new employee.
      /// </summary>
      public partial class CreateEmployee : System.Web.UI.UserControl
      {
            /// <summary>
            /// Raises the DetailsView.ItemUpdate event.
            /// </summary>
            /// <param name="sender">Object that raised the event.</param>
            /// <param name="e">DetailsViewUpdateEventArgs for handling the event.</param>
            public void OnItemUpdate(object sender, DetailsViewUpdateEventArgs e)
            {
                  //employeeData is the name of your sqlDataSource bounded to the detailsview
                  employeeData.UpdateParameters["role"].DefaultValue = ((DropDownList)dataView.Rows[3].Cells[1].Controls[1]).SelectedValue;
                  employeeData.UpdateParameters["state"].DefaultValue = ((DropDownList)dataView.Rows[7].Cells[1].Controls[1]).SelectedValue;
            }

            /// <summary>
            /// Raises the DetailsView.ItemInsert event.
            /// </summary>
            /// <param name="sender">Object that raised the event.</param>
            /// <param name="e">DetailsViewInsertEventArgs for handling the event.</param>
            protected void OnItemInsert(object sender, DetailsViewInsertEventArgs e)
            {
                  //employeeData is the name of your sqlDataSource bounded to the detailsview
                  employeeData.UpdateParameters["role"].DefaultValue = ((DropDownList)dataView.Rows[3].Cells[1].Controls[2]).SelectedValue;
                  employeeData.UpdateParameters["state"].DefaultValue = ((DropDownList)dataView.Rows[7].Cells[1].Controls[2]).SelectedValue;
            }
      }
}
0
Comment
Question by:developernetwork
1 Comment
 
LVL 9

Accepted Solution

by:
bele04 earned 2000 total points
ID: 16645124
Hi,

you can try this:
 <EditItemTemplate>
                    <asp:DropDownList ID="cmbEditRole" runat="server" AutoPostBack="True" DataSourceID="roleData"
                         DataTextField="roleName" DataValueField="roleid" SelectedValue='<%# Eval("roleID")%>' >
                    </asp:DropDownList>
 </EditItemTemplate>

and change the SelectCommand of your main sqlDatasource (employeeData)...add the roleID and stateid column to your selectcommand.  So what this does is it'll select the item in the dropdownlist whose value is equal to the current role or state.  if the current role or state is not found in the dropdownlist it will default to the first item in the dropdownlist.

hope it helps...
-bele04-
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 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