vadim vadim - 2 months ago 21
ASP.NET (C#) Question

Sql concatenate dropdownlist

I'm having trouble pulling sql data when I concatenate my drop down list with two columns. I would like to combine the Last Name (lName) + First Name (fName) and pull the data on those names. Here is what I have so far.

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource3" DataTextField="lName" DataValueField="lName"
AppendDataBoundItems="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="Select">Select</asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
SelectCommand="SELECT (lName + fName) AS zName FROM CoaTest"></asp:SqlDataSource>


Then I have a formview with all the Databounds in it and it's sql connection looks like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString1 %>"
DeleteCommand="DELETE FROM [CoaTest] WHERE [Id] = @Id"
SelectCommand="SELECT Id, fName, lName, agency, title, gen, hired, AppS,
course, Location, SessNum, SDate, EDate, Hours, SitUps, pushUps, rTime,
Grade FROM CoaTest WHERE (lName = @lName)"


UpdateCommand="UPDATE [CoaTest] SET [fName] = @fName, [lName] = @lName, [agency] = @agency, [title] = @title, [gen] = @gen, [hired] = @hired, [AppS] = @Apps, [course] = @course, [Location] = @Location, [SDate] = @SDate, [EDate] = @EDate, [Hours] = @Hours WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="lName"
PropertyName="SelectedValue" Type="String" />

</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="fName" Type="String" />
<asp:Parameter Name="lName" Type="String" />
<asp:Parameter Name="Agency" Type="String" />
<asp:Parameter Name="title" />
<asp:Parameter Name="gen" />
<asp:Parameter Name="hired" />
<asp:Parameter Name="Apps" />
<asp:Parameter Name="course" />
<asp:Parameter Name="Location" />
<asp:Parameter Name="SDate" />
<asp:Parameter Name="EDate" />
<asp:Parameter Name="Hours" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>


This works if I just have it pull by Last Name (lName) but when I try to concatenate it doesn't pull the data from the formview. Thank you in advance!!

Answer

Change select command for dropdown to SELECT Id, (lName + fName) AS zName FROM CoaTest, change dropdown's DataValueField value to Id and selecting data to FormView by Id instead of by name