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?
Related
I'm trying to get a checkbox in a gridview to be an update parameter in a gridview. The field I want updated, author_approved_updated, is a binary field and sometimes it's null so I had to create a field in the select query string to make sure it always returns a non-null value.
But every time I try the code I get the error message about how it can't find the control in the control parameter. I checked here and many people suggested putting $ between the gridview name and the parameter name. Sadly, that doesn't work either.
Could not find control 'gvSops$chkAAU' in ControlParameter 'author_approved_updated'.
<asp:GridView runat="server" ID="gvSops" DataSourceID="dsSops" AutoGenerateColumns="false" AllowSorting="true" AutoGenerateEditButton="true" DataKeyNames="sopID">
<Columns>
...
<asp:TemplateField HeaderText="Author Approved Updated" SortExpression="author_approved_updated">
<EditItemTemplate>
<asp:CheckBox runat="server" ID="chkAAU" Checked='<%# Bind("boolAAU") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAAU" Checked='<%# Bind("boolAAU") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
...
</gridview>
<asp:SqlDataSource ID="dsSops" runat="server" ConnectionString="<%$ ConnectionStrings:AMS %>"
SelectCommandType="Text"
SelectCommand="SELECT *, isnull(author_approved_updated, 0) as boolAAU FROM AMS_SOPs"
UpdateCommandType="Text"
updatecommand="Update AMS_SOPs SET
author_approved_updated = #author_approved_updated
WHERE (sopID = #sopID)"
>
<UpdateParameters>
<asp:ControlParameter ControlID="gvSops$chkAAU" PropertyName="Checked" Name="author_approved_updated" Type="Boolean" ConvertEmptyStringToNull="false" />
</UpdateParameters>
I'm not sure what else to do. Does the fact the variable in the bind function doesn't match up to the filed in the database query? Would this work better as a stored procedure instead of and update command? Does anyone know how to fix it?
Thanks in advance!
I have this asp in a formview insert template.
<asp:FormView ID="FormViewReport" runat="server">
<InsertItemTemplate>
GAME_ID:
<asp:DropDownList ID="DropDownListReportIns" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourceGetGame"
DataTextField="GAME" DataValueField="ID" SelectedValue='<%# Bind("GAME_ID") %>' AppendDataBoundItems="True">
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" />
</asp:DropDownList>
HOME_PLAYER:
<asp:DropDownList ID="DropDownListRepo" runat="server"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" SelectedValue='<%# Bind("HOME_PLAYER_ID") %>' >
</asp:DropDownList>
The sql datasource of the second dropdownlist:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="FormViewReport" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
In the second Dropdownlist the query needs the GAME_ID parameter and this is the DatavalueField of the first dropdownlist.How I can get the selected value from the first dropdownlist and give it to the second dropdownlist?
Put your SqlDatasource control inside the InsertItemTemplate of your FormView (with the DropDownLists), and then change it to this:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListReportIns" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource
That way your ControlParameter is referencing the selected value of the 1st drop down.
Finally I did it using asp for the second dropdownlist:
<asp:DropDownList ID="DropDownListRepIns" runat="server" AppendDataBoundItems="True" EnableViewState="false"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" >
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" Value="0" />
</asp:DropDownList><br />
and a c# code for event item inserting that binds manually:
protected void FormViewReport_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values["HOME_PLAYER_ID"] = ((DropDownList)((FormView)sender).FindControl("DropDownListRepIns")).SelectedValue;}
Also the solution this solution works without needing code behind:
SelectedValue='<%# DataBinder.Eval (Container.DataItem, "HOME_PLAYER_ID") %>'
You have to use this in the second dropdownlist in asp.
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"
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
I have a standard DataGrid that looks like this:
<asp:GridView id="MyGridView"
DataSourceID="MyDataSource1"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
DataKeyNames="Id"
Runat="Server">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("State") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="IdState1" DataSource='<%# GetCategoryNames() %>' DataTextField="State" DataValueField="State" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
When updating the grid I have an UpdateCommand that looks like this:
UpdateCommand= "UPDATE [MauriceBlackburnOffices] SET [Name] = #Name, [Address1] = #Address1, [TheStates] = #State WHERE [Id] = #Id"
However the #State field is not recognized.
Must declare the scalar variable "#State".
What should the # value be?
How can I get the DropDownList value into the update statement?
I'm going to guess that you're using a SQLDataSource object to populate the GridView. If so, you're probably haven't set the UpdateParameters (MSDN Link).
Update your SQLDataSource as follows (you'll need to modify it slightly for your code):
<asp:SqlDataSource
id="MyDataSource1"
runat="server"
ConnectionString="MyDataConnectionString"
SelectCommand="SELECT * FROM Table"
UpdateCommand="UPDATE [MauriceBlackburnOffices] SET [Name] = #Name, [Address1] = #Address1, [TheStates] = #State WHERE [Id] = #Id">
<UpdateParameters>
<asp:ControlParameter Name="Name" ControlId="NameControl" PropertyName="Text"/>
<asp:ControlParameter Name="AddressID" ControlId="AddressControl" PropertyName="SelectedValue"/>
<asp:ControlParameter Name="Name" ControlId="StateControl" PropertyName="SelectedValue"/>
<asp:ControlParameter Name="ID" ControlId="IDControl" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
You can actually do this without the UpdateParameters
By carefully looking at the full source here http://msdn.microsoft.com/en-us/library/ms972948.aspx I found the issue.
Its real wierd.
Where I say:
[TheStates] = #State
"State" MUST be the SelectedValue value in EditItemTemplate and it must be attached by the "Bind" method.
Bind("State") creates #State.
Try this one
<asp:DropDownList ID="IdState1" runat="server" DataTextField="State" DataValueField="State" SelectedValue='<%# Bind("State") %>'></asp:DropDownList>