I am trying to make a dropdownlist work for me. A user can be in many 'Field1's, so I wanted the dropdown to show those Field1s.
Currently what happens is I just get a blank list. Here is a sample of the code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT DISTINCT Field1 FROM table
WHERE (Field3= #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the select parameters and the WHERE clause the list of all DHBs appears - it just seems to have issues with #Field3.
Edit: I have tested the query in SQL Server and it works for me (by replacing #Field3 with a known value)
Any suggestions? I am very new to asp.net, and have next to nothing in the .cs file (besides some authorisation and such), so if people could point me in the right direction that would be fantastic.
Edit 2: It would probably help if I gave you the whole template:
<asp:TemplateField HeaderText="Field1" SortExpression="Field2">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="Field1 FROM table
WHERE (Field3 = #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
You're populating another control using Dropdownlist selectedValue as a parameter, that is dictated by your code. In this case
You're missing property in the controlParameter
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Property="SelectedValue" Name="Field3" Type="String" />
</SelectParameters>
If you're trying to populate DropDownList1 you're completely derailed. In this case you need to show up more markup code and details.
Update: You cannot take the parameters value from the DropDownList control which you're populating. Either remove the parameter or use another control to provide the value
Related
I need to populate the data from database in Dropdown using sqlDataSource. The SqlDataSource is using a querystring. The data is not being populated in dropdown. Can you please suggest what I am doing wrong here?
Code for Dropdown:
<ajaxToolkit:ComboBox ID="SelectDropDown1" runat="server" DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend" AppendDataBoundItems="true" Width="200px" Height="16pt"
Font-Size="8pt" DataSourceID="SqlDataSource1" DataTextField="Rubric"
DataValueField="Rubric">
<asp:ListItem Value="all">All</asp:ListItem>
</ajaxToolkit:ComboBox>
Code for SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Education_Data %>"
SelectCommand="SELECT DISTINCT [Rubric] FROM [table1] WHERE ([Program] = #Program)">
<SelectParameters>
<asp:QueryStringParameter Name="Program" QueryStringField="Program"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The #program needs to be set somehow.
This MSDN article will show you how to do it. Go to section "Passing Parameters to SQL Statements"
This is my dropdown list code........
<td valign="top" align="center">
<asp:DropDownList ID="StudentNameDropDownList" runat="server" Width="150px"
DataSourceID="SqlDataSource2" DataTextField="StudentName"
DataValueField="StudentName" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:dbbilling2.0ConnectionString3 %>"
SelectCommand="SELECT [StudentID], [StudentName] FROM [tblStudentInfo] WHERE ([Class] = #Class)">
<SelectParameters>
<asp:ControlParameter ControlID="ClassDropDownList" Name="Class"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
Now I want to access the Student ID field in my code behind file..How can i achieve this?? What syntax i must use[like dropdownlist.selecteditem] ??
First of all in your DropDownList asp component you must set the property DataValueField="StudentID", then in your code behind you can get the Id of the selected student by writing : StudentNameDropDownList.SelectedValue
For Id You can try this code
StudentNameDropDownList.SelectedValue
Here's a dropdown list I have...
<asp:DropDownList
ID="selectTimeFrame"
runat="server"
AutoPostBack="true"
DataTextField="Increment"
DataValueField="Increment"
DataSourceID="SqlTimeFrame"
</asp:DropDownList>
And its datasource:
<asp:SqlDataSource
ID="SqlTimeFrame"
runat="server"
ConnectionString="<% connectionstring %>"
SelectCommand="Select [IncrementID], [Increment] FROM [TimeFrame] ORDER BY [IncrementID]" >
</asp:SqlDataSource>
and then I have a gridview, whos datasource looks like:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<% connectinstring %>"
ProviderName="<% connectionstring %>"
SelectCommand="SELECT * FROM #TimeFrame">
<SelectParameters>
<asp:ControlParameter ControlID="selectTimeFrame"
Name="TimeFrame"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
And obviously the place where I'm having problems is the fact that " FROM #TimeFrame " doesn't do what I want to. I have Different views whose names correspond to different timeframes, and I want to be able to change the gridview to populate with that information based off of the option a user selects via the dropdown menu. Any insight would be much appreciated... THANKS!! :D
You could use dynamic SQL to achieve what you're looking for although you would need to test this very thorughly to prevent SQL injection attacks as we can never trust the input being received from users.
I've created a simple stored proc which checks whether the table exists in the db and if so it constructs and executes your dynamic SQL statement:
Stored procedure:
CREATE PROCEDURE dbo.GetData
#TableName NVARCHAR(200)
AS
BEGIN
IF OBJECT_ID(#TableName , N'U') IS NOT NULL
BEGIN
EXEC('SELECT * FROM ' + #TableName);
END
END
ASPX:
<asp:DropDownList ID="selectTimeFrame" runat="server" AutoPostBack="true" DataTextField="Increment"
DataValueField="Increment" DataSourceID="SqlTimeFrame" />
<asp:SqlDataSource ID="SqlTimeFrame" runat="server" ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="Select [IncrementID], [Increment] FROM [TimeFrame] ORDER BY [IncrementID]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dynamicDS" runat="server" ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="GetData" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="selectTimeFrame" Name="TableName" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvData" DataSourceID="dynamicDS" runat="server">
</asp:GridView>
Ok guys, so I read this:
How to set SelectedValue of DropDownList in GridView EditTemplate
and I'm having the same issue. However, I don't want to bind the selected value to the displayed text, but instead the values. They are different attributes selected from a SQLDataSource. Here is my DDL code with its SQLDataSource:
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List"
DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID"
SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="Plant_Carton_Food_List" runat="server"
ConnectionString="<%$ ConnectionStrings:OMSConnectionString %>"
SelectCommand="SELECT P.PlantMaterial_FoodID,
M.Material_SAP_Value + ' - ' + MD.SAP_Long_Description AS Food_Description
FROM Plant_Carton_Food AS P
LEFT OUTER JOIN Material_Descriptions AS MD
ON P.PlantMaterial_FoodID = MD.Material_ID
LEFT OUTER JOIN Materials AS M
ON P.PlantMaterial_FoodID = M.Material_ID">
</asp:SqlDataSource>
Here is the (abridged) SQLDataSource of the GridView:
<asp:SqlDataSource ID="Plant_Carton_Table" runat="server"
OldValuesParameterFormatString="old_{0}"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
OnInserting="Plant_Carton_Food_Table_Inserting"
OnInserted="Plant_Carton_Food_Table_Inserted"
InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure"
SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure"
UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Active_Case" Type="Boolean" />
<asp:Parameter Name="PlantMaterial_FoodID" Type="String" />
<asp:Parameter Name="PlantMaterial_CaseID" Type="String" />
...
</UpdateParameters>
...
</asp:SqlDataSource>
And, finally, my exception:
DataBinding: 'System.Data.DataRowView'
does not contain a property with the
name 'PlantMaterial_FoodID'.
I really don't have much knowledge on how databinding through the GridView Edit templates work, but I was able to see that the correct values pass through the OnRowCommand event handler for updates. How do I propagate these values to the SQLDataSource without getting NULL?
Right now, I guess any explanation on how databinding with GridView templates work in general would be beneficial as well. Thanks!
This isn't really an answer to the question, but instead a workaround... but it works for me.
I added PlantMaterial_FoodID as a hidden column into the GridView and changed the SelectedValue binding on the DropDownList to reference this new column, as shown below.
New Column
<asp:TemplateField HeaderText="Food ID" SortExpression="Food_ID">
<EditItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
...and here is the new binding
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List" DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID" SelectedValue='<%# Bind("Food_ID") %>'
</asp:DropDownList>
This effectively sets the selected dropdownlist index to the correct value.
Have you checked that PlantMaterial_FoodID is spelled exactly as in the SqlDataSource SelectCommand?
I'm getting the dreaded 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value error when trying to filter a drop down list in a templatefield using one of the other boundfield values (I'm trying to get a list of employees based on their department - i.e. the user can change the employee but only to another member of the same department).
Here's the code:
<asp:GridView ID="Rotas" runat="server" AllowSorting="True"
DataSourceID="SqlDataSource3" AutoGenerateEditButton="True" DataKeyNames="DateFrom,DateTo,DepartmentId"
AutoGenerateColumns="False" OnRowUpdating="Rotas_RowUpdating">
<Columns>
<asp:BoundField DataField="DateFrom" HeaderText="DateFrom" ReadOnly="True"
SortExpression="DateFrom" />
<asp:BoundField DataField="DateTo" HeaderText="DateTo" ReadOnly="True"
SortExpression="DateTo" />
<asp:TemplateField HeaderText="Employee Name" SortExpression="EmployeeName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource4" DataTextField="EmployeeName"
DataValueField="EmployeeName" SelectedValue='<%# Bind("EmployeeName") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DepartmentId" HeaderText="DepartmentId"
ReadOnly="True" SortExpression="DepartmentId" />
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" ReadOnly="False"
SortExpression="EmployeeId" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
SelectCommand="SELECT r.DateFrom, r.DateTo, e.EmployeeName, e.EmployeeId, r.departmentid
FROM
dbo.[Rota] r INNER JOIN
dbo.[Employee] AS e ON r.EmployeeId = e.EmployeeId
WHERE (r.DateTo >= GETDATE()) "
UpdateCommand="UPDATE [Rota] SET [EmployeeId] = (select employeeid from employee where employeename = #EmployeeName),
[departmentid] = (select departmentid from employee where employeename = #EmployeeName)
WHERE [DateFrom] = #DateFrom AND [DateTo] = #DateTo AND [DepartmentId] = #DepartmentId">
<UpdateParameters>
<asp:Parameter Name="DateTo" Type="DateTime" />
<asp:Parameter Name="DateFrom" Type="DateTime" />
<asp:Parameter Name="DepartmentId" Type="Int16" />
<asp:Parameter Name="EmployeeId" Type="Int16" />
<asp:Parameter Name="EmployeeName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
</p>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
onselecting="SqlDataSource4_Selecting"
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=#DepartmentId"
>
<SelectParameters>
<asp:ControlParameter ControlID="Rotas" Name="DepartmentId"
PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Really can't see what I'm doing wrong. If I don't use the Select Parameter and just a 'select employeename from employee' then the whole list of employees is displayed fine. As soon as I try and use a controlparameter it falls over. Help! :)
Thanks in advance for any assistance offered.
I think the problem is that your SqlDataSource that returns the list of employees for a department isn't returning any rows, and I think this is because the ControlParameter isn't right. Although you've done the right thing by pointing it at the Rotas GridView and the SelectedValue property, there are three fields used in the DataKeyNames property (DateFrom, DateTo, DepartmentId), and I believe you're currently passing the DateFrom value into your query - hence, no results.
What I think you need to use in the PropertyName of the ControlParameter instead of the SelectedValue property of Rotas is the SelectedDataKey - there's details on MSDN here although the demo code there isn't particularly useful. However the important line is:
If you are creating a ControlParameter
object and want to access a key field
other than the first field, use the
indexed SelectedDataKey property in
the PropertyName property of the
ControlParameter object
So from that I think what you need is:
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:OnCallRotaConnectionString %>"
onselecting="SqlDataSource4_Selecting"
SelectCommand="SELECT [EmployeeName] FROM [Employee] where DepartmentId=#DepartmentId"
>
<SelectParameters>
<asp:ControlParameter ControlID="Rotas" Name="DepartmentId"
PropertyName="SelectedDataKey[2]" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Without a copy of your data I can't test it, but give it a go and see what you get...