I am facing a problem with my gridview as the Delete link is deleting all the rows where i want to delete one by one. I have record my screen that may exolain more about my problem
https://www.youtube.com/watch?v=VAh0A-UOafU&feature=youtu.be
<asp:GridView ID="FavoritGRDSHOW" runat="server" AllowPaging="True"
DataKeyNames="FavID" DataSourceID="FavoriteGRDView"
PageSize="6" Width="600px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ButtonType="Image"
DeleteImageUrl="~/iconsimg/Delete2.png" />
<asp:BoundField DataField="FavID" HeaderText="FavID"
InsertVisible="False" ReadOnly="True" SortExpression="FavID" />
<asp:BoundField DataField="AdsID" HeaderText="AdsID"
SortExpression="AdsID" />
<asp:BoundField DataField="UID" HeaderText="UID" SortExpression="UID" />
<asp:BoundField DataField="AdsTit" HeaderText="AdsTit"
SortExpression="AdsTit" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="FavoriteGRDView" runat="server"
ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
SelectCommand="SELECT [FavID], [AdsID], [UID], [AdsTit] FROM [favourite]
WHERE ([UID] = #UID)"
OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = FavID" >
<SelectParameters>
<asp:SessionParameter Name="UID" SessionField="UsrNme" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Your delete statement:
DELETE FROM [favourite] WHERE [FavID] = FavID
Is equivalent to this, because every record is going to match its own fields:
DELETE FROM [favourite] // deletes all rows in the table
You need to specify which ID to delete and pass it as a parameter:
<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = #FavID">
<DeleteParameters>
<asp:Parameter Name="FavID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
You can read more here.
use this:
<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = #FavID">
<DeleteParameters>
<asp:ControlParameter ControlID="FavoritGRDSHOW" PropertyName="SelectedValue" Name="FavID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
Related
I set up an Sqldatasource and a Gridview:
<asp:SqlDataSource ID="DetailsSQLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:aspnet-WebApplication6-20131007103938ConnectionString1 %>"
SelectCommand="SELECT [ProjectID], [ProjectName], [CreatorID], [Deadline] FROM [tProject] WHERE 1=1"
>
<SelectParameters>
<asp:Parameter Type="String" Name="ProjectID"></asp:Parameter>
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="DetailsSQLDataSource" AutoGenerateColumns="False" DataKeyNames="ProjectID" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ProjectID" ReadOnly="True" SortExpression="ProjectID"></asp:BoundField>
<asp:BoundField DataField="ProjectName" HeaderText="ProjectName" SortExpression="ProjectName"></asp:BoundField>
<asp:BoundField DataField="CreatorID" HeaderText="CreatorID" SortExpression="CreatorID"></asp:BoundField>
<asp:BoundField DataField="Deadline" HeaderText="Deadline" SortExpression="Deadline"></asp:BoundField>
</Columns>
</asp:GridView>
I have tested the selectcommand, and it returns a table with 4 rows. Yet there is no data to show in the gridview. The codebehind is almost empty and surely doesn't affect these controls. Can it be something with the connection? How can I test that?
Call .DataBind() to actually force the data to be bound to the grid, like this:
GridView1.DataBind();
I have been having issues with the update command on a page I have. I have 4 gridviews and 4 detailsviews and none of them will update with the update command. I can insert and delete but update doesn't work. The update button doesn't cause a post back, it just sits there.
I created a new page and added a detailsview and connected it to the same database and same table, and in that page the update command worked perfectly. I even copied and pasted the command queries to the detailsviews on the other page, but no success.
I'm thinking is an issue with the buttons, but why the default update link button doesn't work?
I am using ASP.NET, C# and SQL Server 2008.
I did notice that the update button on the detailsview that works shows: javascript:__doPostBack('DetailsView1$ctl02','') on the status bar.
But the update button for the one that doesn't work shows this: WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$DetailsView2$ctl02", "", true, "", "", false, true))
Here's the code for the detailsview that works
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="WorkshopName,WorkshopBeginingDate,WorkshopLocation"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="WorkshopName" HeaderText="WorkshopName"
ReadOnly="True" SortExpression="WorkshopName" />
<asp:BoundField DataField="WorkshopBeginingDate"
HeaderText="WorkshopBeginingDate" ReadOnly="True"
SortExpression="WorkshopBeginingDate" />
<asp:BoundField DataField="WorkshopEndingDate" HeaderText="WorkshopEndingDate"
SortExpression="WorkshopEndingDate" />
<asp:BoundField DataField="WorkshopLocation" HeaderText="WorkshopLocation"
ReadOnly="True" SortExpression="WorkshopLocation" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
This is the code for one that doesn't work
<asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False"
DataKeyNames="WorkshopName,WorkshopBeginingDate,WorkshopLocation"
DataSourceID="SqlDataSource20" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="WorkshopName" HeaderText="WorkshopName"
ReadOnly="True" SortExpression="WorkshopName" />
<asp:BoundField DataField="WorkshopBeginingDate"
HeaderText="WorkshopBeginingDate" ReadOnly="True"
SortExpression="WorkshopBeginingDate" />
<asp:BoundField DataField="WorkshopEndingDate" HeaderText="WorkshopEndingDate"
SortExpression="WorkshopEndingDate" />
<asp:BoundField DataField="WorkshopLocation" HeaderText="WorkshopLocation"
ReadOnly="True" SortExpression="WorkshopLocation" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
**This is the SQL Data source code for the one that doesn't work, it is also the same on the one that works, except for the Datasource ID.**
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:WildLife_EducationConnectionString %>"
DeleteCommand="DELETE FROM [tblWorkshop] WHERE [WorkshopName] = #original_WorkshopName AND [WorkshopBeginingDate] = #original_WorkshopBeginingDate AND [WorkshopLocation] = #original_WorkshopLocation AND (([WorkshopEndingDate] = #original_WorkshopEndingDate) OR ([WorkshopEndingDate] IS NULL AND #original_WorkshopEndingDate IS NULL))"
InsertCommand="INSERT INTO [tblWorkshop] ([WorkshopName], [WorkshopBeginingDate], [WorkshopEndingDate], [WorkshopLocation]) VALUES (#WorkshopName, #WorkshopBeginingDate, #WorkshopEndingDate, #WorkshopLocation)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [WorkshopName], [WorkshopBeginingDate], [WorkshopEndingDate], [WorkshopLocation] FROM [tblWorkshop]"
UpdateCommand="UPDATE [tblWorkshop] SET [WorkshopEndingDate] = #WorkshopEndingDate WHERE [WorkshopName] = #original_WorkshopName AND [WorkshopBeginingDate] = #original_WorkshopBeginingDate AND [WorkshopLocation] = #original_WorkshopLocation AND (([WorkshopEndingDate] = #original_WorkshopEndingDate) OR ([WorkshopEndingDate] IS NULL AND #original_WorkshopEndingDate IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_WorkshopName" Type="String" />
<asp:Parameter DbType="Date" Name="original_WorkshopBeginingDate" />
<asp:Parameter Name="original_WorkshopLocation" Type="String" />
<asp:Parameter DbType="Date" Name="original_WorkshopEndingDate" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="WorkshopName" Type="String" />
<asp:Parameter DbType="Date" Name="WorkshopBeginingDate" />
<asp:Parameter DbType="Date" Name="WorkshopEndingDate" />
<asp:Parameter Name="WorkshopLocation" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter DbType="Date" Name="WorkshopEndingDate" />
<asp:Parameter Name="original_WorkshopName" Type="String" />
<asp:Parameter DbType="Date" Name="original_WorkshopBeginingDate" />
<asp:Parameter Name="original_WorkshopLocation" Type="String" />
<asp:Parameter DbType="Date" Name="original_WorkshopEndingDate" />
</UpdateParameters>
</asp:SqlDataSource>
After weeks of researching I decided to remove all the ajax from my page, and update started working beautifully. Apparently the some code was conflicting with the update command on the page.
I had researched the issue for weeks, readded the datasource many times but no success. My last resource was to remove all of the ajax from the page and magically everything worked.
well I've stumbled upon similar cases of this problem, and also followed the solution approaches. But my case is quite strange, as I have 2 different DetailsView-controls (with different data), one works, the other doesn't.
So here's the problem in detail. I get the following error message:
DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I am aware of this thread, and maybe I'm stupid and not seeing something. But maybe you do. I have 2 Detailsviews, which bind their data based on a dataset for one user. Both DVs have DropdownList-Controls inside their EditItemTemplates, which bind the possible values for this very column. I use SelectedValue='<%# Bind("mycolumn") %>' for my DropDownList-template exactly the same way in the 2 DVs.
As said, I am aware of code-behind workarounds, but I want to avoid those, to keep my code clean and consistent. I can't really document why I use a workaround on one DetailsView, and why I don't on the other.
Here is the code of my 2 DetailsViews:
<asp:DetailsView ID="dv_theme_ava" runat="server" Height="50px" Width="125px" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" DefaultMode="Edit" AutoGenerateEditButton="True" DataKeyNames="Pat_ID">
<Fields>
<asp:TemplateField HeaderText="Theme">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="theme" DataValueField="theme"
SelectedValue='<%# Bind("theme") %>'>
</asp:DropDownList>
<asp:Label ID="lolbel2" runat="server" Text='<%# Bind("theme") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [theme] FROM [gui_themes]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Avatar">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"
DataTextField="avatar" DataValueField="avatar">
</asp:DropDownList>
<asp:Label ID="lolbel" runat="server" Text='<%# Bind("avatar") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [avatar] FROM [gui_avatars]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [pat_gui_config] WHERE ([Pat_ID] = #Pat_ID)" DeleteCommand="DELETE FROM [pat_gui_config] WHERE [Pat_ID] = #Pat_ID"
InsertCommand="INSERT INTO [pat_gui_config] ([Pat_ID], [theme], [avatar]) VALUES (#Pat_ID, #theme, #avatar)"
UpdateCommand="UPDATE [pat_gui_config] SET [theme] = #theme, [avatar] = #avatar WHERE [Pat_ID] = #Pat_ID">
<DeleteParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="Pat_ID" SessionField="sel_pat_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
<asp:Parameter Name="Pat_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="dv_treat_edit" runat="server" AutoGenerateEditButton="True"
AutoGenerateRows="False" DataKeyNames="Tr_ID" DataSourceID="sql_newcat" DefaultMode="Edit"
Height="50px" Width="90%" AllowPaging="True" CssClass="dv_details" Style="margin: 0 auto;">
<Fields>
<asp:BoundField DataField="Tr_ID" HeaderText="Tr_ID" InsertVisible="False" ReadOnly="True"
SortExpression="Tr_ID" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:BoundField DataField="syn_ger" HeaderText="Display Name (German)" SortExpression="syn_ger" />
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="ddl_type0" runat="server" DataSourceID="sql_ddl_type0" DataTextField="type"
DataValueField="type" SelectedValue='<%# Bind("type") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_type0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [type] FROM [entry_type]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exclusive for Patient_ID">
<EditItemTemplate>
<asp:DropDownList ID="ddl_excl_pat0" runat="server" DataSourceID="sql_ddl_exclpat0"
DataTextField="Pat_ID" DataValueField="Pat_ID" SelectedValue='<%# Bind("custom_cat_for_Pat") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_exclpat0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [Pat_ID] FROM [patients]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<CommandRowStyle CssClass="dv_footer" />
<RowStyle CssClass="dv_tr" />
<PagerSettings Mode="NumericFirstLast" Position="Top" Visible="False" />
</asp:DetailsView>
<asp:SqlDataSource ID="sql_newcat" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [treat_cat]" DeleteCommand="DELETE FROM [treat_cat] WHERE [Tr_ID] = #Tr_ID"
InsertCommand="INSERT INTO [treat_cat] ([description], [syn_ger], [type], [custom_cat_for_Pat]) VALUES (#description, #syn_ger, #type, #custom_cat_for_Pat)"
UpdateCommand="UPDATE [treat_cat] SET [description] = #description, [syn_ger] = #syn_ger, [type] = #type, [custom_cat_for_Pat] = #custom_cat_for_Pat WHERE [Tr_ID] = #Tr_ID">
<DeleteParameters>
<asp:Parameter Name="Tr_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
<asp:Parameter Name="Tr_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Note: the data model is quite simple. For comparison I used the 2 fields "theme" and "type". Both tables only have 1 column in my database, holding string entries.
Now, the "type"-DDL gets it's items perfectly fine, and has the SelectedValue bound to the value brought to the DetailsView by the datasource. When I bind the "theme"-DDL with SelectedValue, I get the error. Interesting: in the same EditItemTemplate I have set up a Label (ID "lolbel2" :p) to check the databinding. It works (of course, when I remove the SelectedValue from the DDL). So without the SelectedValue in the DDL my output is like
[DROPDOWNLIST] with items "space", "magic"
[LABEL] with text "magic" (since this is the value of my test-user).
Am I missing something? Am I completely nuts?
So, sorry for re-asking this question for like the 10th-time, but I want to UNDERSTAND what my code does.
Thanks in advance!
Konrad
Ooooohkay. Found the problem, that's what you get when you are stubborn as a goat. :)
When debugging a workaround with a HiddenField I noticed that the value, which gets bound the same way as the Label-Control, has some trailing whitespace. In particular: instead of "dog" I got "dog ". While this isn't shown in the asp:Label, I guess this was the reason why the value wasn't found in the DropDownList.
Where did the whitespace come from? In my SQL table I created the columns for "theme" and avatar as "nchar", and not "nvarchar". Apparently when using "nchar" as DataType the not-used chars of the tupels are filled with whitespace, or let's say the fields have fixed width (always x chars).
Changing the datatype to "nvarchar" helped me to get rid of the whitespace, and now the databinding of the DDLs works just fine.
I am documenting this, as maybe someone else will stumble over this aswell - and since there are 50 solutions and workarounds, maybe just taking a look at the database sometimes does the trick.
I am having some problems passing parameters to a DELETE command, and cant seem to get a good understanding on how it works.
<asp:SqlDataSource ID="sdsPropertyList"
runat="server"
ProviderName="<%$ appSettings:ProviderName %>"
ConnectionString="<%$ appSettings:ConnectionString %>"
SelectCommand="selPropertyByAcntID"
SelectCommandType="StoredProcedure"
OnSelecting="sdsPropertyList_Selecting"
OnSelected="sdsPropertyList_Selected"
DeleteCommand="delPropertyByPropID"
DeleteCommandType="StoredProcedure"
OnDeleting="sdsPropertyList_Deleting"
OnDeleted="sdsPropertyList_Deleted">
<SelectParameters>
<asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="in_propID" Type="Int32" DefaultValue="0" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList"
AutoGenerateColumns="false" CssClass="gvPropList">
<Columns>
<asp:BoundField HeaderText="ID" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="Property" DataField="prop_title"
ItemStyle-CssClass="gvPropTitle" >
<ItemStyle CssClass="gvPropTitle" />
</asp:BoundField>
<asp:BoundField HeaderText="Units" DataField="unitCount"
ItemStyle-CssClass="gvUnitCount" >
<ItemStyle CssClass="gvUnitCount" />
</asp:BoundField>
<asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified"
ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" >
<ItemStyle CssClass="gvDate" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerPropList"/>
<RowStyle CssClass="gvPropRow" />
</asp:GridView>
protected void sdsPropertyList_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
int userID = Convert.ToInt32(Page.User.Identity.Name);
if (userID != 0)
e.Command.Parameters["in_acntID"].Value = userID;
}
protected void sdsPropertyList_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
int userID = Convert.ToInt32(Page.User.Identity.Name);
if (userID != 0)
{
e.Command.Parameters["in_acntID"].Value = userID;
}
}
The SELECT statement is straightforward which requires one input parameter of userID.
However, the DELETE statement, requires 2 parameter inputs.
in_acntID = userID
in_propID = the boundfield datafield prop_id
What am I doing wrong? and should the CommandName and CommandArgument be passed at the ItemTemplate level if I have it defined at the SqlDataSource level?
I want the delete button to achieve the following:
delete records from table(s) in DB
remove the row from the gridview
UPDATE
After some additional research, I've found that, the NAME for parameters and HeaderText for Boundfield must be the same so that the values within your gridview can be used by the SQL commands of the datasource.
With the exception of the initial select commant, i have removed all the code behind references.
All is working corrently now.
According to the MSDN documentation, you need to specify the DataKeyNames on the gridView:
"Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. You must set the DataKeyNames property in order for the automatic update and delete features of the GridView control to work. The values of these key fields are passed to the data source control in order to specify the row to update or delete."
e.g. if the id was in a listbox or dropdown
<DeleteParameters>
<asp:ControlParameter ControlID="controlname" Name="id" PropertyName="SelectedValue" Type="Int32" />
</DeleteParameters>
i have used the above successfully for deleting . this could work for textboxes or labels. If you are handling the event , why not just take the entire delete process to the even handler ? Specify the entire sql setup including connection , command execution there . This method has also worked for me .
After some additional research, I've found that, the NAME for parameters and HeaderText for Boundfield must be the same so that the values within your gridview can be used by the SQL commands of the datasource.
With the exception of the initial select commant, i have removed all the code behind references.
All is working corrently now.
<asp:SqlDataSource ID="sdsPropertyList"
runat="server"
ProviderName="<%$ appSettings:ProviderName %>"
ConnectionString="<%$ appSettings:ConnectionString %>"
SelectCommand="selPropertyByAcntID"
SelectCommandType="StoredProcedure"
OnSelecting="sdsPropertyList_Selecting"
DeleteCommand="delPropertyByPropID"
DeleteCommandType="StoredProcedure"
OnDeleted="sdsPropertyList_Deleted" >
<SelectParameters>
<asp:Parameter Name="acnt_id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="acnt_id" Type="Int32" />
<asp:Parameter Name="prop_id" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList"
AutoGenerateColumns="false" CssClass="gvPropList" DataKeyNames="acnt_id, prop_id">
<Columns>
<asp:BoundField HeaderText="acnt_id" InsertVisible="true" DataField="acnt_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="prop_id" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" />
<asp:BoundField HeaderText="Property" DataField="prop_title">
<ItemStyle CssClass="gvPropTitle" />
</asp:BoundField>
<asp:BoundField HeaderText="Units" DataField="unitCount" >
<ItemStyle CssClass="gvUnitCount" />
</asp:BoundField>
<asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified"
ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" >
<ItemStyle CssClass="gvDate" />
</asp:BoundField>
<asp:BoundField HeaderText="Active" DataField="prop_active">
<ItemStyle CssClass="gvPropActive" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerPropList"/>
<RowStyle CssClass="gvPropRow" />
</asp:GridView>
I have a gridview that's populated by a stored procedure. On this dataset, the user should be able to filter it by selecting the LabID and/or SiteName from 2 dropdownlists. What is the best way to accomplish this?
<asp:DropDownList ID="ddlLabIDs" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource2" DataTextField="LabID"
DataValueField="LabID" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_LabIDs_Select;"></asp:SqlDataSource>
<asp:DropDownList ID="ddlSiteNames" runat="server"
DataSourceID="SqlDataSource3" DataTextField="SiteName"
DataValueField="SiteName" AppendDataBoundItems="True"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_SiteNames_Select;"></asp:SqlDataSource>
<asp:GridView ID="AllDataFlat" runat="server" AllowPaging="True" PageSize="20"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="LabID" HeaderText="LabID" SortExpression="LabID" />
<asp:BoundField DataField="SiteName" HeaderText="SiteName"
SortExpression="SiteName" />
<asp:BoundField DataField="StartDateTime" HeaderText="StartDateTime"
SortExpression="StartDateTime" />
<asp:BoundField DataField="FilterNumber" HeaderText="FilterNumber"
SortExpression="FilterNumber" />
<asp:BoundField DataField="AmtWaterFiltered" HeaderText="AmtWaterFiltered"
ReadOnly="True" SortExpression="AmtWaterFiltered" />
<asp:BoundField DataField="WaterTemp" HeaderText="WaterTemp" ReadOnly="True"
SortExpression="WaterTemp" />
<asp:BoundField DataField="pH" HeaderText="pH" SortExpression="pH" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="exec usp_AllDataFlat_Select;">
</asp:SqlDataSource>
Thank you.
you can try like this.....
NOTE : this is just example on how to filter the grid view with the selected values on two drop down lists...
<%# page autoeventwireup="true" codefile="FilterWithMultipleDropDownLists.aspx.cs"
inherits="GridView_FilterWithMultipleDropDownLists" language="C#" masterpagefile="~/MasterPages/Default.master"
title="GridView: Filter With Multiple DropDownLists" %>
<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
<div>
Select Supplier:
<asp:dropdownlist id="ddlSuppliers" runat="server" appenddatabounditems="True" autopostback="True"
datasourceid="sdsSuppliers" datatextfield="CompanyName" datavaluefield="SupplierID">
<asp:listitem text="All Suppliers" value="-1">
</asp:listitem>
</asp:dropdownlist>
<asp:sqldatasource id="sdsSuppliers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [SupplierID], [CompanyName] FROM [Suppliers] ORDER BY [CompanyName]">
</asp:sqldatasource>
</div>
<div style="margin-top: 12px;">
Select Category:
<asp:dropdownlist id="ddlCategories" runat="server" appenddatabounditems="True" autopostback="True"
datasourceid="sdsCategories" datatextfield="CategoryName" datavaluefield="CategoryID">
<asp:listitem text="All Categories" value="-1">
</asp:listitem>
</asp:dropdownlist>
<asp:sqldatasource id="sdsCategories" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER By [CategoryName]">
</asp:sqldatasource>
</div>
<asp:gridview id="gvProducts" runat="server" autogeneratecolumns="False" datakeynames="ProductID"
datasourceid="sdsProducts" style="margin-top: 24px;">
<columns>
<asp:boundfield datafield="ProductID" headertext="ProductID" insertvisible="False"
readonly="True" sortexpression="ProductID" />
<asp:boundfield datafield="ProductName" headertext="ProductName" sortexpression="ProductName" />
<asp:boundfield datafield="CompanyName" headertext="Supplier" sortexpression="CompanyName" />
<asp:boundfield datafield="CategoryName" headertext="Category" sortexpression="CategoryName" />
</columns>
</asp:gridview>
<asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT [P].[ProductID], [P].[ProductName], [P].[SupplierID], [P].[CategoryID], [S].[CompanyName], [C].[CategoryName] FROM [Products] AS [P] INNER JOIN [Suppliers] AS [S] ON [S].[SupplierID] = [P].[SupplierID] INNER JOIN [Categories] AS [C] ON [C].[CategoryID] = [P].[CategoryID] WHERE ([P].[SupplierID] = CASE WHEN #SupplierID = -1 THEN [P].[SupplierID] ELSE #SupplierID END AND [P].[CategoryID] = CASE WHEN #CategoryID = -1 THEN [P].[CategoryID] ELSE #CategoryID END) ORDER BY [P].[ProductName]">
<selectparameters>
<asp:controlparameter controlid="ddlSuppliers" name="SupplierID" propertyname="SelectedValue" type="Int32" />
<asp:controlparameter controlid="ddlCategories" name="CategoryID" propertyname="SelectedValue" type="Int32" />
</selectparameters>
</asp:sqldatasource>
</asp:content>
I hope it will helps you..
The most efficient way to do this would be to pass the values selected in the drop down controls to the stored procedure usp_AllDataFlat_Select. In this stored procedure you should be able to select only the rows that satisfy the filter values.
If you do not have access to the stored procedures (can't modify them) than in my oppinion the best way to achieve this is to wrap your call to the stored procedure in an object that retrieves all the rows from the data base and filters them in memory (not very efficient). Then you can use an OnjectDataSource instead of the SqlDataSource to populate the gridview. This ObjectDataSoruce could call the methods of the object that wraps your stored procedures.
You can find an example here. The example uses queries instead of stored procedures but replacing the queries with calls to stored procedures should be trivial.
Best of luck,