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!
Related
I have got very basic Product data table, I set up my grid view data source to sqlDataSoruce with paging and sorting options true.
But I also have a DropDownList and if a user chooses a certain product group ( Vegetables, Fruits, Frozen etc), grid view only shows that product group items.
String sqlSearchByProduct = " SELECT ID, pID,pGroupID, pName, pPrice, Status, Stock FROM productsTable WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)";
sqlDataSource.SelectCommand = sqlSearchByProduct ;
The grid shows all products from groupID selected from dropdownlist but the issue is when I want to sort it by name or price, grid view shows all products again.
I will be very grateful if you know how to fix this issue.
Thank you.
You need to keep SelectedValue of the dropdown and the easiest way to do so is to bind all data declaratively. Something like this:
<asp:DropDownList ID="ddCategory" runat="server" DataTextField="categoryname"
DataValueField="categoryid"
AppendDataBoundItems="true" DataSourceID="sqlCategory" AutoPostBack="true">
<asp:ListItem Value="0" Text="- Select -"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sqlCategory" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
SelectCommand="select categoryid,categoryname from dbo.categories">
</asp:SqlDataSource>
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="ProductID" DataSourceID="sqlNWind">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlNWind" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
SelectCommand="SELECT Products.ProductID, Products.ProductName FROM Products where (#categoryid=0 or CategoryID=#categoryId)">
<SelectParameters>
<asp:ControlParameter Name="categoryid" ControlID="ddCategory" Type="Int32" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
No code behind required.
Side note: never build sql query string like this
" ... WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)"
Such code is prone to SQL Injection attack. Always use parameters.
Have you rebound your data to the gridview, e.g. below
GridView1.DataBind();
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 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>
I have an ASP.NET GridView in a Web Form. This GridView using a SqlDataSource and utilizes paging and searching. For the sake of reference, here is the trimmed down version of it:
<asp:SqlDataSource ID="myDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM [Person] p, [Location] l WHERE p.[LocationID]=l.[ID] ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
/>
<asp:GridView ID="myGridView" runat="server" DataSourceID="myDataSource"
AllowPaging="true" AllowSorting="true" PageSize="25" AutoGenerateEditButton="true"
DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Person" SortExpression="Name">
<ItemTemplate>
<asp:Literal ID="nameLit" runat="server" Text='<%# Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Email" SortExpression="Email" HeaderText="Email Address" />
<asp:TemplateField HeaderText="Location" SortExpression="City">
<ItemTemplate>
<asp:Literal ID="cityLit" runat="server" Text='<%# Bind("City") %>' />
<asp:Literal ID="stateLit" runat="server" Text='<%# Bind("State") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The data source has a lot of records. At least 25,000. Because of this, I want to allow the user to filter by state. I want to provide a drop down list with a list of states. When a user changes the drop down list, the results in the grid view will be filtered by the selected state. However, I do not know how to do this with a SqlDataSource. Can someone please explain to me how to accomplish this?
Thank you!
First of all, let's add the DropDownList, and a SqlDataSource to populate it:
<asp:dropdownlist runat="server" id="StateDropDownList" DataSourceId="StateDataSource" DataTextField="State" DataValueField="State" />
<asp:sqldatasource runat="server" ConnectionString="<%$ ConnectionStrings:DB %>" SelectCommand="SELECT DISTINCT State FROM Location" />
Then we change your existing data source to use a State parameter and read the parameter's value from the DropDownList:
<asp:SqlDataSource ID="myDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:DB %>"
SelectCommand="SELECT p.[ID], p.[Name], p.[Email], l.[State], l.[City] FROM
[Person] p INNER JOIN [Location] l ON p.[LocationID]=l.[ID]
WHERE (l.State = #State) ORDER BY p.[Name]"
UpdateCommand="UPDATE [Person] SET [Email] = #Email WHERE [ID] = #ID"
<SelectParameters>
<asp:ControlParameter ControlID="StateDropDownList" Name="State"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
/>
That should be all you need.
BTW, top tip: I changed your query slightly to use INNER JOIN syntax rather than doing the table join in a WHERE clause. There's some great info here on INNER JOIN.
Top tip #2: Paging in the GridView is a bit naive - every time you change pages, the GridView is reading all 25 000 records in your table, then throwing away the ones it doesn't need. There's a great article here (and plenty of questions on SO!) on doing custom paging with a Gridview.
i have used aggregate function in List Page of dynamic datawebsite. When it execute it gives me,
DataBinding: 'DynamicClass1' does not contain a property with the name 'EmployeeID'.
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource"
AllowPaging="True" AllowSorting="True" CssClass="gridview">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="EditHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>'
Text="Edit" /> <asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete"
CausesValidation="false" Text="Delete"
OnClientClick='return confirm("Are you sure you want to delete this item?");'
/> <asp:HyperLink ID="DetailsHyperLink" runat="server"
NavigateUrl='<%# table.GetActionPath(PageAction.Details, GetDataItem()) %>'
Text="Details" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="footer"/>
<PagerTemplate>
<asp:GridViewPager runat="server" />
</PagerTemplate>
<EmptyDataTemplate>
There are currently no items in this table.
</EmptyDataTemplate>
</asp:GridView>
<asp:LinqDataSource ID="GridDataSource" runat="server"
ContextTypeName="DataClassesDataContext"
TableName="Employees"
GroupBy="EmployeeNo"
Select="new(Key,
Max(Version) As VersionNo)"
EnableDelete="true">
<WhereParameters>
<asp:DynamicControlParameter ControlID="FilterRepeater" />
</WhereParameters>
</asp:LinqDataSource>
I have not changed any of the default code except i have added ContextTypeName,TableName, GroupBy and select in Linq Data source...
Table "Employees" has "EmployeeID, EmployeeNo, EmployeeName, Department, Address, City, State, Country, Version" as columns..
Any idea how to solve this?
Thanks in advance!
Regards,
Bala
The problem is that you're grouping doesn't create the type of data that the grid is expecting to receive. GetDataItem() is expecting a particular type of data and that's not what you've set up in your LinqDataSource.
Your select needs to include all the items that GetDateItem() needs, right now it doesn't:
new(Key, Max(Version) As VersionNo) //EmployeeID needs to be included here
new(Key, Max(Version) As VersionNo) is fine.
To Get the Employee... use: Key.Employee
The Key is a dynamic class containing all the properties specified in the group by statement.
Anything else (ie EmployeeNo, EmployeeName, Department, Address, City, State, Country, Version) will need an aggregate statement (ie Sum, Max, Min).