asp.net Can a GridView have two DataSources? - asp.net

Afternoon All,
I have a gridview that i am using to display a list of 'Actions' from a database.
I have a dropdown list that is connected to this gridview, this enables a user to filter the data via the 'Action Status' in the dropdown list ('Assigned', 'Inprogress' & 'Completed'). This works perfectly fine....
What i am trying to do is have another filter option available for the user, they would like to also filter by user name. I have the datasource and the dropdown list set up for this but i can only have one datasource connected to my gridview?
Does anyone have a suggestionon how to enable the users to also filter by username as well as 'action status'?
Here is my code if you need to take a peek....
<asp:SqlDataSource ID="dsActions" runat="server"
ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>"
SelectCommand="Populate_grdAllActions_Filter"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddFilterStatus" Name="ActionStatusID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsActionsByUser" runat="server"
ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>"
SelectCommand="Populate_grdAllActions_Filter_By_User"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddFilterUsers" Name="UserID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsFilterList" runat="server"
ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>"
SelectCommand="SELECT * FROM [ActionStatus]"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsFilterUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:SMCConnectionString %>"
SelectCommand="SELECT * FROM [UserSimpleList]"></asp:SqlDataSource>
<table style="width: 400px">
<tr>
<td colspan="2" style="height: 25px"><b>Filter Options:</b></td>
</tr>
<tr>
<td style="width: 158px">
Select Action Status:</td>
<td>
<asp:DropDownList ID="ddFilterStatus" runat="server"
DataTextField="ActionStatus" DataValueField="ActionStatusID"
AutoPostBack="True" DataSourceID="dsFilterList"></asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 158px">
Select Actions by User:</td>
<td>
<asp:DropDownList ID="ddFilterUsers" runat="server"
DataTextField="UserFullName" DataValueField="UserID"
AutoPostBack="True" DataSourceID="dsFilterUsers"></asp:DropDownList></td>
</tr>
</table>
<br/>
<asp:GridView ID="grdActions" runat="server" AutoGenerateColumns="False"
DataSourceID="dsActions" CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowPaging="True" PageSize="6" DataKeyNames="ActionID" Width="68%" >
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ActionID" DataNavigateUrlFormatString="~/UpdateAction.aspx?Edit={0}"
DataTextField="ActionID" HeaderText="Action ID" >
<HeaderStyle HorizontalAlign="Center" Wrap="True" />
<ItemStyle HorizontalAlign="Center" />
</asp:HyperLinkField>
<asp:BoundField DataField="Action" HeaderText="Action"
SortExpression="Action" >
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Owner" HeaderText="Owner"
SortExpression="Owner">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="TargetDate" HeaderText="Target Date"
SortExpression="TargetDate" DataFormatString="{0:dd-MM-yyyy} " >
<HeaderStyle HorizontalAlign="Center" Wrap="True" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Action Status" HeaderText="Action Status"
SortExpression="Action Status" >
<HeaderStyle HorizontalAlign="Center" Wrap="True" />
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
<PagerStyle CssClass="pgr" />
</asp:GridView>
Any help is much appriechiated in advance.
Regards
Betty

You can determine which option the user selected, then set the Datasource in code-behind rather than wired them up in the aspx.
Pseudocode assumes that if the FilterUsers dropdown was selected, then bind to the Action By User data source:
if(IsPostBack){
if(ddlFilterUsers.SelectedValue <> ""){
grdActions.DataSource = dsActionsByUser;
dsFilterUsers.DataBind();
//etc
}
}

In the code behind part, On ddFilterStatus_SelectIndexChanged() Event, Check for appropriate "Action Status" and Select the Data source as follows,
if(ddFilterStatus.SelectedValue == someid1)
{
grdActions.DataSource = appropriate datasource
}
else
{
grdActions.DataSource = appropriate datasource
}
grdActions.DataBind()
Hope this helps...

Related

Using Eval If/Else/EndIf in GridView causes error Databinding methods such as Eval(), XPath(), and Bind()

I am trying to pro grammatically alter a value in a GridView based on a returned database value.
The database value "is_zero_minutes_task" is Boolean. If True I wish to display a "0" and if False I wish to display the value returned in "MinutesTaken".
My code:
<asp:SqlDataSource ID="SqlDataSourceRecentJobs" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support %>"
ProviderName="<%$ ConnectionStrings:ConnectionString_LIVE_customer_support.ProviderName %>"
SelectCommand="SELECT customer_name, start_time, end_time, is_zero_minutes_task, TIMESTAMPDIFF(MINUTE,start_time,end_time) AS MinutesTaken FROM time_recorder_jobs WHERE (time_recorder_jobs.deleted = #deleted) AND (time_recorder_users.company_id = #companyid) ORDER BY end_time">
<SelectParameters>
<asp:Parameter Name="#deleted" DefaultValue="0" Type="Object" />
<asp:SessionParameter Name="companyid" SessionField="CompanyID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridViewRecentJobs" runat="server"
AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSourceRecentJobs" ForeColor="#333333"
GridLines="None" CellSpacing="4" ShowHeaderWhenEmpty="True"
AllowSorting="True" AllowPaging="True">
<Columns>
<asp:BoundField DataField="customer_name" HeaderText="customer_name"
SortExpression="customer_name">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="start_time" HeaderText="start_time"
SortExpression="start_time">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="end_time" HeaderText="end_time"
SortExpression="end_time">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
<ItemTemplate>
<%If Eval("is_zero_minutes_task").ToString = True Then%>
<asp:Label ID="MinutesTaken" runat="server" Text="0"></asp:Label>
<%Else%>
<asp:Label ID="MinutesTaken" runat="server" Text='<%# Bind("MinutesTaken") %>'></asp:Label>
<%End If%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also tried using:
<%If DataBinder.Eval(GridViewRecentJobs.DataItem, "is_zero_minutes_task") = True Then%>
But that presents the error of "'DataItem' is not a member of System.Web.UI.WebControls.GridView'"
I worked it out!
<asp:TemplateField HeaderText="MinutesTaken" SortExpression="MinutesTaken">
<ItemTemplate>
<asp:Label ID="MinutesTaken" runat="server" Text='<%# If((Eval("is_zero_minutes_task") = 1), "0", Eval("MinutesTaken"))%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Need a Gridview to Change when DropDownList changes

I have a dropdownlist in ASP.Net (I am using VB.Net) and my ASP.Net code follows. What I would like to do is have that Girdview populate dynamically off of the DropDownList. If the first option (showing nothing) is chosen, I would like everything possible in the Gridview to show. If one of the other options is chosen, I would like the GridView to filter based on that option. I can see the DropDownList, but nothing shows up in the Gridview. I imagine the issue has to do with binding the DropDownList to the Gridview somehow.
Thanks in advance.
<%# Page Title="Home Page" Language="VB" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="SAP._Default" %>
<%# Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Tab Container Tips & Tricks</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">
<cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="Screening">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Pull For Screening "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"><asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Add Applicants" />
<asp:Label ID="Label3" runat="server" Text="Choose Campus: "></asp:Label>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Text="All" Value="%"></asp:ListItem>
<asp:ListItem>RC</asp:ListItem>
<asp:ListItem>HS</asp:ListItem>
<asp:ListItem>TL</asp:ListItem>
<asp:ListItem>PH</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ScreenerView %>" ProviderName="<%$ ConnectionStrings:ScreenerView.ProviderName %>"
SelectCommand="SELECT [ScreenerID], [LEGAL_FNAME], [LEGAL_LNAME], [APPL_PERSON_ID], [AAMC_ID], [GENDER], [URM], [RecMCAT], [SciGPA], [LEGAL_RES_STATE_CD], [PullDate], [First] FROM [vw_Screener]"
FilterExpression="Screener ID LIKE '?' AND First LIKE '?'">
<SelectParameters>
<asp:Parameter Name="intS" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter ControlID="DropDownList2" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ScreenerID" HeaderText="ScreenerID" SortExpression="ScreenerID" Visible="False"></asp:BoundField>
<asp:BoundField DataField="LEGAL_FNAME" HeaderText="First Name" SortExpression="LEGAL_FNAME" />
<asp:BoundField DataField="LEGAL_LNAME" HeaderText="Last Name" SortExpression="LEGAL_LNAME" />
<asp:BoundField DataField="APPL_PERSON_ID" HeaderText="APPL_PERSON_ID" SortExpression="APPL_PERSON_ID" Visible="False" />
<asp:BoundField DataField="AAMC_ID" HeaderText="AAMC" SortExpression="AAMC_ID" />
<asp:BoundField DataField="GENDER" HeaderText="Gender" SortExpression="GENDER" />
<asp:BoundField DataField="URM" HeaderText="URM" SortExpression="URM" />
<asp:BoundField DataField="RecMCAT" HeaderText="MCAT" SortExpression="RecMCAT" />
<asp:BoundField DataField="SciGPA" HeaderText="Sci. GPA" SortExpression="SciGPA" />
<asp:BoundField DataField="LEGAL_RES_STATE_CD" HeaderText="State" SortExpression="LEGAL_RES_STATE_CD" />
<asp:BoundField DataField="PullDate" HeaderText="Date Pulled" DataFormatString = "{0:d}" SortExpression="PullDate" />
<asp:BoundField DataField="First" HeaderText="First Choice CC" SortExpression="First" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ContentTemplate>
</cc1:TabPanel>
You need to fix your FilterExpression and FilterParements in your SQLDataSource:
In your ASPX File you need to make the following changes:
FilterParameters: The items in this list will be used by the FilterExpression. If this is emtpy then your FilterExpression will have nothing to work with. However, you have ONE item in there, now you must link it to the FilterExpression with its index. Since you have ONE item then its index will be 0 (see filterexpression below). I assume its the [ScreenerID] so its probably an Int32? Then you need to specify that and set a defaultvalue:
<FilterParameters>
<asp:ControlParameter Name="ScreenerID" ControlID="DropDownList2"
PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</FilterParameters>
FilterExpression: The way you have it now is not correct because it is NOT linked with any real value say User-Interace (Dropdown) . You must link it based on its index/position in the FilterParameter items. Like we said above, since ScreenerID is the first item then it has index 0:
FilterExpression="ScreenerID = {0}"
DropDownList2: Since you already have AutoPostBack="True" you just need add event OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
If ScreenID is an integer, you need to remove the % from the value field in the ALL item like so: <asp:ListItem Value="" Text="ALL>
VB.NET Code-Behind: In your DropDownList2_SelectedIndexChanged event dont put any code in it. Like so:
Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
'Leave empty
End Sub
Let me know if this is not working for you, I can update this answer as I find out more from you.

How to generate checkbox for every field in database using GridView

I have limited knowledge on VB coding. I am now creating a system which allow customer to select exhibition event and booth number they want and next reserved which day they want to rent.
By default, in GridView control can add checkbox Field but it only generate 1 checkbox for 1 row of data.
As state in the title, I have no idea how to generate the checkboxes for every field in database call D1,D2,D3,D4,D5,D6 and D7, each carry value 0 by default.
Now I want every single field have a checkbox to allow customer select which day they want to reserve and retrieve their checked value to stole into corresponding D1-D7 field, checked value will update value 0 to 1.
Next, how should I coding to store the checked value into database? in default.aspx or default.aspx.vb?
Or any other suggestion to generate checkbox instead of using gridview?
The default view using GridView
What I want
My coding:
<%# Page Language="VB" MasterPageFile="~/MasterPageMember.master" AutoEventWireup="false" CodeFile="member_view_event_list.aspx.vb" Inherits="member_view_event_list" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.style8
{
width: 46px;
}
.style9
{
}
.style10
{
width: 86px;
}
</style>
</asp:Content>
<asp:Content ID="ContentPlaceHolder2" runat="server"
contentplaceholderid="ContentPlaceHolder2">
<table width="80%">
<tr><td class="style10"> </td><td class="style8"> </td><td> </td></tr>
<tr><td class="style10">
<asp:Label ID="Label1" runat="server" Text="Select Event:"></asp:Label>
</td><td class="style8">
<asp:DropDownList ID="ddlEventList" runat="server"
DataSourceID="SqlDataSourceEvent" DataTextField="eventTitle"
DataValueField="eventID" AutoPostBack="True" Width="200">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceEvent" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT DISTINCT [eventTitle], [eventID] FROM [booth_eventinfo]">
</asp:SqlDataSource>
</td><td>
</td></tr>
<tr><td class="style10">
<asp:Label ID="Label2" runat="server" Text="Select Booth:"></asp:Label>
</td><td class="style8">
<asp:DropDownList ID="ddlBoothList" runat="server"
DataSourceID="SqlDataSourceBooth" DataTextField="boothAlias"
DataValueField="boothID" AutoPostBack="True" Width="200">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceBooth" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [boothAlias], [boothID] FROM [booth_eventinfo] WHERE ([eventID] = #eventID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlEventList" Name="eventID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td><td>
</td></tr>
<tr><td class="style10">
</td><td class="style8">
</td><td>
</td></tr>
<tr><td class="style9" colspan="3">
<asp:GridView ID="GridViewDay" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSourceDay" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="D1" HeaderText="Day1" SortExpression="D1" />
<asp:BoundField DataField="D2" HeaderText="Day2" SortExpression="D2" />
<asp:BoundField DataField="D3" HeaderText="Day3" SortExpression="D3" />
<asp:BoundField DataField="D4" HeaderText="Day4" SortExpression="D4" />
<asp:BoundField DataField="D5" HeaderText="Day5" SortExpression="D5" />
<asp:BoundField DataField="D6" HeaderText="Day6" SortExpression="D6" />
<asp:BoundField DataField="D7" HeaderText="Day7" SortExpression="D7" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceDay" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [D1], [D7], [D6], [D5], [D4], [D3], [D2] FROM [booth_eventinfo] WHERE ([boothID] = #boothID)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlBoothList" Name="boothID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td></tr>
<tr><td class="style10">
</td><td class="style8">
<asp:CheckBox ID="CheckBox1" runat="server" />
</td><td>
</td></tr>
</table>
</asp:Content>
You need to change your 'BoundField' to a 'TemplateField' for each CheckBox control you desire. The code example below shows you how to do this. Note the binding expression syntax for your DataField value:
<asp:TemplateField HeaderText="Day1" SortExpression="D1" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("D1") %>' />
</ItemTemplate>
</asp:TemplateField>
This may help you out. Instead of using a grid view you can use a asp:CheckBoxList, if the requirement is to only place checkboxes for the seven days. Once the list is created, you can style it horizontally using css so that it adheres to the appearance as shown in the image. This link gives a preview of a checkboxlist.
For the second part of the question, you can save it in default.aspx.vb during the final submit.
In case you want to use a gridview, then you would have to add seven templated columns and bind it to the data source.

AjaxControlToolkit - GridView wont select - don't understand viewstate

Overview:
I have two text boxes at the top of this form for inputting a beginning/ending date.
In the middle of the form I have a gridview that is populated with rows based on those dates.
At the bottom of the form I have a report generated based on which row is selected in the grid.
I selected the BeginningDate and the EndingDate and the gridview becomes populated (working). When I click the "Select" button on a row in the grid something is happening that makes it break. The row isn't selected. I know this because the CSS thats usually applied (by default) to a selected row in the gridview is not applied.
Additionally in the code-behind I'm setting...
lblGridError.Text = gridJobsReport.SelectedIndex.ToString()
The page load even has the same code, so the label reads "-1" when the page loads but never changes when I click the select buttons on the gridview. Something is breaking my Async postbacks. I wish I understood viewstate better, I'm always suspicious of it being the culprit but honestly I have no idea.
<%# Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="BidReport.aspx.vb" Inherits="BidReport" %>
<%# Register assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<ajaxToolkit:ToolkitScriptManager ID="tsmgrJobsReport" runat="server"
CombineScripts="True">
</ajaxToolkit:ToolkitScriptManager>
<p>
Enter a date range to populate the Jobs Grid.</p>
<asp:UpdatePanel ID="uPanelForm" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="text-align: right">
<asp:Label ID="lblBeginningDate" runat="server" Text="Beginning Date:"></asp:Label></td>
<td>
<asp:TextBox ID="txtBeginningDate" runat="server" AutoPostBack="True"
CausesValidation="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtBeginningDate_CalendarExtender"
runat="server"
TargetControlID="txtBeginningDate">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvBeginningdate" runat="server"
ControlToValidate="txtBeginningDate"
ErrorMessage="You must enter the Beginning Date" Font-Bold="True"
ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revBeginningDate" runat="server"
ControlToValidate="txtBeginningDate"
ErrorMessage="Posted Date must be in the format "DD/MM/YYYY""
Font-Bold="True" ForeColor="Red"
ValidationExpression="^([1-9]|1[012])/([1-9]|[12][0-9]|3[01])/(20\d\d)$">*</asp:RegularExpressionValidator>
</td>
<td rowspan="2">
<asp:CompareValidator ID="cvBeginDateLTEndDate" runat="server"
ControlToCompare="txtEndingDate" ControlToValidate="txtBeginningDate"
ErrorMessage="Beginning Date must be earlier than the Ending Date."
Font-Bold="True" ForeColor="Red" Operator="LessThan">*</asp:CompareValidator>
</td>
</tr>
<tr>
<td style="text-align: right">
<asp:Label ID="lblEndingDate" runat="server" Text="Ending Date:"></asp:Label></td>
<td>
<asp:TextBox ID="txtEndingDate" runat="server" AutoPostBack="True"
CausesValidation="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="txtEndingDate_CalendarExtender"
runat="server" TargetControlID="txtEndingDate">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvEndingDate" runat="server"
ControlToValidate="txtEndingDate"
ErrorMessage="You must enter the EndingDate Date" Font-Bold="True"
ForeColor="Red">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="revEndingDate" runat="server"
ControlToValidate="txtEndingDate"
ErrorMessage="Posted Date must be in the format "DD/MM/YYYY""
Font-Bold="True" ForeColor="Red"
ValidationExpression="^([1-9]|1[012])/([1-9]|[12][0-9]|3[01])/(20\d\d)$">*</asp:RegularExpressionValidator>
</td>
</tr>
</table>
<asp:ValidationSummary ID="valsumDates" runat="server" ForeColor="Red" />
<p>Click or Tap the "SELECT" button in the right column next to the Job you want and
a report will be generated below.</p>
<asp:GridView ID="gridJobsReport" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="PostingID" DataSourceID="sqlDSReportGrid"
ForeColor="#333333" GridLines="Horizontal"
ShowHeaderWhenEmpty="True" Width="100%">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="PostingID" HeaderText="PostingID" ReadOnly="True"
SortExpression="PostingID" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" />
<asp:BoundField DataField="JobName" HeaderText="JobName"
SortExpression="JobName" />
<asp:BoundField DataField="Shift" HeaderText="Shift" SortExpression="Shift" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Needs" HeaderText="Needs"
SortExpression="Needs" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PostedDate" HeaderText="PostedDate"
SortExpression="PostedDate" DataFormatString="{0:d}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="PostedTime" HeaderText="Time"
SortExpression="PostedTime" DataFormatString="{0:t}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ClosingDate" HeaderText="ClosingDate"
SortExpression="ClosingDate" DataFormatString="{0:d}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ClosingTime" HeaderText="Time"
SortExpression="ClosingTime" DataFormatString="{0:t}" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:CommandField ButtonType="Button" CausesValidation="True"
ShowSelectButton="true" />
</Columns>
<EditRowStyle BackColor="#999999" />
<EmptyDataRowStyle HorizontalAlign="Center" />
<EmptyDataTemplate>
There is currently no data to display based on the date range provided.
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:Label ID="lblGridError" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<ajaxToolkit:UpdatePanelAnimationExtender ID="uPanelForm_UpdatePanelAnimationExtender"
runat="server" TargetControlID="uPanelForm">
</ajaxToolkit:UpdatePanelAnimationExtender>
<p>You can print or save the report using the buttons at the top of the report.</p>
<asp:UpdatePanel ID="uPanelJobsReport" runat="server">
<ContentTemplate>
<rsweb:ReportViewer ID="rvJobsReport" runat="server" Width="100%">
</rsweb:ReportViewer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gridJobsReport"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="sqlDSReportGrid" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT dbo.tblJobPostings.PostingID, dbo.tblDepartments.Department, dbo.tblJobs.JobName,
dbo.tblJobPostings.Shift, dbo.tblJobPostings.Needs, dbo.tblJobPostings.PostedDate,
dbo.tblJobPostings.PostedTime, dbo.tblJobPostings.ClosingDate, dbo.tblJobPostings.ClosingTime
FROM dbo.tblJobPostings
INNER JOIN dbo.tblJobs ON dbo.tblJobPostings.JobID = dbo.tblJobs.JobID
INNER JOIN dbo.tblDepartments ON dbo.tblJobPostings.DeptID = dbo.tblDepartments.DeptID
WHERE dbo.tblJobPostings.ClosingDate >= #BeginningDate
AND dbo.tblJobPostings.ClosingDate <= #EndingDate
ORDER BY dbo.tblJobPostings.ClosingDate Asc">
<SelectParameters>
<asp:ControlParameter ControlID="txtBeginningDate" Name="BeginningDate"
PropertyName="Text" ConvertEmptyStringToNull="False" DefaultValue="" />
<asp:ControlParameter ControlID="txtEndingDate" Name="EndingDate"
PropertyName="Text" ConvertEmptyStringToNull="False" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sqlDSJobReport" runat="server"></asp:SqlDataSource>
</asp:Content>
I really don't understand why this worked, but I decided to try handling the dates manually with session variables and its working perfectly now. It doesn't matter if I mess with the session state on the update panels or any of the form elements it seems to work no matter what now.
I guess that after the initial asyncpostback the sqldatasource wasn't reading the values in the textboxes, or something. If anyone can explain what was going on in another "Answer Question" section I'll accept it.
If Page.IsPostBack Then
Session("sBeginningDate") = txtBeginningDate.Text
Sessoin("sEndingDate") = txtEndingDate.Text
txtBeginningDate.Text = Session("sBeginningdate")
txtEndingDate.Text = Session("sEndingDate")
Else
Session("sBeginningDate") = Now().AddDays(-14)
Session("sEndingDate") = Now()
txtBeginningDate.Text = Session("sBeginningDate").ToShortDateString
txtEndingDate.Text = Session("sEndingDate").ToShortDateString
End If

ASP.Net SQLDataSource Filter - Input string was not in a correct format

I have a ASP.Net web page with a grid view. I want to filter the grid view data based on the selection of a drop down list. I have this working fine on another page. The difference on the problem page is that the column I need to filter against is an int rather than a varchar. When I load the page I get the error message: Input string was not in a correct format.
I have pasted code for the dropdownlist and datasource below. This must be a common requirement so I guess I'm missing something obvious...? On attempting to find a resolution via Google, other have the same problem. A post on very similar lines can be found at http://www.velocityreviews.com/forums/t123088-problem-in-filterparameters.html, unfortunately Gavin's resolution didn't work for me.
Many thanks,
Rob.
<%# Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="BacheAccountSettings.aspx.cs"
Inherits="RDM.BacheTradeLoad" Title="Untitled Page" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<tr>
<td style="width: 100px;">
Filter:
</td>
<td style="width: 170px;">
<asp:DropDownList ID="ddlAccountSourceId" DataSourceID="dsAccountSourceId" AutoPostBack="true"
DataValueField="AccountId" runat="server" Width="130px" Font-Size="11px" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="%"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="1">
<asp:GridView ID="grdRefBacheAccount" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderWidth="1px" CellPadding="3" DataSourceID="dsArcReference"
GridLines="Horizontal" BorderStyle="None" EnableModelValidation="True" Style="position: static"
OnRowDataBound="grdRefBacheAccount_RowDataBound" AllowPaging="True" PageSize="25"
CssClass="Grid" AllowSorting="True">
<Columns>
<asp:CommandField ShowEditButton="True" ShowCancelButton="False" />
<asp:BoundField DataField="SourceID" HeaderText="SourceID" SortExpression="SourceID" />
<asp:BoundField DataField="GroupCompany" HeaderText="GroupCompany" SortExpression="GroupCompany" />
<asp:BoundField DataField="Portfolio" HeaderText="Portfolio" SortExpression="Portfolio" />
<asp:BoundField DataField="OnBehalfComp" HeaderText="OnBehalfComp" SortExpression="OnBehalfComp" />
<asp:BoundField DataField="AssignedTrader" HeaderText="AssignedTrader" SortExpression="AssignedTrader" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" ForeColor="#F7F7F7" Font-Bold="True" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</td>
</tr>
<div id="divImportButton" align="right">
<asp:Button ID="btnImport" runat="server" Text="Import Trade" OnClick="btnImport_Click"
CssClass="Button" />
</div>
<asp:SqlDataSource ID="dsArcReference" runat="server" ConnectionString="<%$ ConnectionStrings:ARC_REFERENCEConnectionString %>"
SelectCommand="select ACCOUNT_SOURCE_ID as 'AccountId',
[ACCOUNT_BTF_GROUP_COMPANY] as 'GroupCompany',
[ACCOUNT_BTF_PORTFOLIO] as 'Portfolio',
[ACCOUNT_BTF_ON_BEHALF_COMPANY] as 'OnBehalfComp',
[ACCOUNT_BTF_ASSIGNED_TRADER] as 'AssignedTrader'
from dbo.REF_BACHE_ACCOUNT" UpdateCommand="update dbo.REF_BACHE_ACCOUNT
set [ACCOUNT_BTF_GROUP_COMPANY] = #GroupCompany,
[ACCOUNT_BTF_PORTFOLIO] = #Portfolio,
[ACCOUNT_BTF_ON_BEHALF_COMPANY] = #OnBehalfComp,
[ACCOUNT_BTF_ASSIGNED_TRADER] = #AssignedTrader
where [ACCOUNT_SOURCE_ID]=#SourceID" FilterExpression="SourceID={0}">
<FilterParameters>
<asp:ControlParameter Type="Int32" ControlID="ddlAccountSourceId" PropertyName="SelectedValue" />
</FilterParameters>
<UpdateParameters>
<asp:Parameter />
<asp:Parameter Name="ACCOUNT_SOURCE_ID" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="dsAccountSourceId" runat="server" ConnectionString="<%$ ConnectionStrings:ARC_REFERENCEConnectionString %>"
SelectCommand="SELECT DISTINCT [ACCOUNT_SOURCE_ID] as AccountId FROM [REF_BACHE_ACCOUNT]"
DataSourceMode="DataSet"></asp:SqlDataSource>
You pass a value that is not Int32.
<asp:ListItem Text="All" Value="%"></asp:ListItem>
Remove this line and its going to work. For show them all you need to find an other way...

Resources