I have a gridview that contains search inputs and a submit button. The problem is that the SelectMethod is not firing if it is clicked twice with the same search criteria. Thus, am not able to fetch new data from the db. Am pretty sure it is caching the request. The problem is that I don't know how to fix it. I tried to disable the cache on the ObjectDataSource, but it didn't work
<asp:Button ID="btnSearch" runat="server" CssClass="button floatr" Text="Search" />
<asp:GridView ID="GridView1" runat="server" CssClass="extendedGridView" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="ObjectDataSource2"
EmptyDataText="No Records Found"> ... </asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetTransaction" TypeName="Tx.Class.HelperFunctions">
<SelectParameters>
<asp:ControlParameter ControlID="tbxSearchTransactionType" Name="transactionType" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="tbxStartDate" Name="startDate" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="tbxEndDate" Name="endDate" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
Code inside HelperFunctions.cs:
public IEnumerable<Transaction> GetTransaction(String transactionType,String startDate, String endDate)
{ ... }
Related
I have a gridview populated with an sqldatasource. One of the columns of the gridview is a templatefield which contains a label as ItemTemplate, but in the EditItemTemplate I have a dropdownlist, instead of a textbox. I wanted to be able to edit a value from a certain column by selecting from a list of possible values.
So I created a second sqldatasource in order to extract the values from the database. I bounded the 2 sqldatasources. Visually it's ok. When I click on edit, the dropdownlist appears with the values that I want inside of it. When I select one of the values and click update, I get the error "
Input string was not in a correct format." I think I know why this is happening. Probably the values from the dropdownlist are type string and what I need is integer because that is the type for this column. I have tried setting the UpdateParameter as type Int32 but no luck. Is there any way to do this ?
This is the code I have used, but I have changed the column names with "Column_1", "Column_2", etc. In this example, "Column_6" is the templatefield with the dropdownlist in the edit.
<asp:GridView ID="DataUploadGridView" runat="server" AutoGenerateColumns="False" DataSourceID="First_DB" DataKeyNames="Column_3" >
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Column_1" HeaderText="Column 1"/>
<asp:BoundField DataField="Column_2" HeaderText="Column 2"/>
<asp:BoundField DataField="Column_3" HeaderText="Column 3"/>
<asp:BoundField DataField="Column_4" HeaderText="Column 4"/>
<asp:BoundField DataField="Column_5" HeaderText="Column 5"/>
<asp:TemplateField HeaderText="Column 6">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Second_DB" DataTextField="Column_6_Description" DataValueField="Column_6_ID"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Column_6_ID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" HeaderText="Edit" />
<asp:CommandField ShowDeleteButton="true" HeaderText="Delete" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="First_DB" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" OnSelecting="First_DB_Selecting" OnSelected="First_DB_Selected" OnInserted="First_DB_Inserted" OnInserting="First_DB_Inserting" OnDeleting="First_DB_Deleting" OnUpdating="First_DB_Updating"
SelectCommand ="SELECT * FROM [tblFirst_DB]
UpdateCommand ="UPDATE [tblFirst_DB]
SET [Column_1] = #Column_1,
[Column_2] = #Column_2,
[Column_3] = #Column_3,
[Column_4] = #Column_4,
[Column_5] = #Column_5,
[Column_6] = #Column_6
WHERE [Column_3] = #Column_3"
DeleteCommand ="DELETE FROM [tblFirst_DB] WHERE [Column_3] = #Column_3"
<UpdateParameters>
<asp:Parameter Name="Column_1" Type="String" />
<asp:Parameter Name="Column_2" Type="String" />
<asp:Parameter Name="Column_3" Type="String" />
<asp:Parameter Name="Column_4" Type="String" />
<asp:Parameter Name="Column_5" Type="Int32" />
<asp:Parameter Name="Column_6" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Column_3" Type="String" />
</DeleteParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="Second_DB" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT [Column_6_ID], [Column_6_Description] FROM [tblSecond_DB]"></asp:SqlDataSource>
Hopefully you are still here. So just to recap the problem. I'm getting an "
Input string was not in a correct format. " error because I need the Column_6 parameter to be integer and the values are coming from a dropdownlist which is probably making each value a string.
What can I do so that the value for Column_6 stays int upon updating it ?
The only thing that comes to mind right now is to pass the parameter from the code behind somehow. When I select the dropdownlist value, I should trigger an OnSelectedIndexChanged event. I'm thinking here is where I should cast the Selected_ID to an integer and somehow feed it into the first sqldatasource. But the value would go into a label so that will turn it into a string again. So no use...I'm really stuck on this. Any help is welcome.
Thank you !
Update: I've noticed that if I remove all the UpdateParameters, except the one from Column_6, I'm not getting the error anymore. However the value is not going in the gridview. It's an empty cell. I looked in the DB and it's null.
I think it's null because in the UpdateParameters I now have:
<UpdateParameters>
<asp:Parameter Name="Column_6" Type="Int32" />
</UpdateParameters>
It's not getting the value from anywhere. It should get it from the Second_DB sqldatasource. I tried changing it to:
<UpdateParameters>
<asp:ControlParameter Name="Column_6" ControlID="Second_DB" Type="Int32" />
</UpdateParameters>
But it's not really working out like I expected. I feel like I'm getting closer but no idea why it's behaving like this.
I have nested GridView. When I expand row of external it shows internal GridView. Both gridviews are inside UpdatePanel and uses ObjectDataSource to populate data.
When I click on expand, I'm doing a post back by clicking a button via JQuery. Here, ObjectDataSource1 which is for external grid calls SelectMethod multiple times. I checked UpdatePanel UpdateMode is Conditional.
How can I prevent ObjectDataSource from fetching data multiple times?
ASPX:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectCountMethod="GetDevicesCount" SelectMethod="GetDevices" TypeName="Flows" SortParameterName="sortExpression" EnablePaging="True">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="searchTerm" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="hdnFieldFromDate" Name="fromDate" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="hdnFieldToDate" Name="toDate" PropertyName="Value" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectCountMethod="GetFlowDetailsCount" SelectMethod="GetFlowDetails" OnSelecting="ObjectDataSource2_Selecting" TypeName="Flows" EnablePaging="True">
<SelectParameters>
<asp:ControlParameter ControlID="HiddenDeviceId" Name="deviceId" PropertyName="Value" Type="String" />
<asp:ControlParameter ControlID="hdnFieldFromDate" Name="fromDate" PropertyName="Value" Type="DateTime" />
<asp:ControlParameter ControlID="hdnFieldToDate" Name="toDate" PropertyName="Value" Type="DateTime" />
</SelectParameters>
</asp:ObjectDataSource>
I have handled this in two ways.
1: In the ASPX side of your page set select method to SelectMethod = "" and assign it if the page is postback.
if (Page.IsPostBack)
{
//Always set the select methods.
SetSelectMethods();
}
else
{
ODSGetOptionSearchDataCS.SelectMethod = string.Empty;
ODSWatchlistCS.SelectMethod = string.Empty;
}
private void SetSelectMethods()
{
ODSGetOptionSearchDataCS.SelectMethod = "GetOptionCondors";
ODSWatchlistCS.SelectMethod = "GetOptionWLCondors";
}
I really do not care for how I handled this above, so going forward in my select method I cache the data for 10 seconds (longer if I need to) I allow that method to run again, but returned the cached data vs. hitting the database again.
I have a asp GridView in my page.I am using asp ObjectDataSource to bind data to my grid.I am calling a method from data layer to get the data.method have a parameter(userid).I want to pass the parameter from ObjectDataSource.user id is in session variable.How can i pass the parameter in ObjectDataSource?
code
<asp:ObjectDataSource ID="_allSitesDataMgr" runat="server" ConvertNullToDBNull="True"
OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" TypeName="PWRWebData.SiteCreation.SitesDataMgr">
</asp:ObjectDataSource>
Take a <SelectParameters> in your object data source
<SelectParameters>
<asp:SessionParameter Name="MyUserID" Type="Int32" SessionField="UserID" />
</SelectParameters>
Update:
<asp:ObjectDataSource ID="_allSitesDataMgr" runat="server" ConvertNullToDBNull="True"
OldValuesParameterFormatString="{0}" SelectMethod="GetAllSitesByUser" TypeName="PWRWebData.SiteCreation.SitesDataMgr">
<SelectParameters>
<asp:SessionParameter Name="MyUserID" Type="Int32" SessionField="UserID" />
</SelectParameters>
</asp:ObjectDataSource>
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.
Here's my problem: I built a small ASP.Net site to display some data from one of our internal databases. I have it set that the Query runs on a button click and takes into account parameters entered into 4 different text boxes. This all works just fine if I limit the query to the top 1 million or so rows. The issue is that my table has a bit over 55 million rows. If I take off the limiter I'm constantly getting overflow and memory errors. I'm not surprised, but I was wondering if there was a way to fix this. I was wondering if maybe the parameters are only applying themselves AFTER the query has been run. Sorry if I'm being inexact, I only started doing this stuff last week. Here's my code:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<span class="style1">
<strong>Date </strong>(dd/mm/yyyy) </span>
<asp:TextBox ID="TextBox4" runat="server" Text="14/08/2009" style="margin-left: 4px"
Width="125px"></asp:TextBox>
TO
<asp:TextBox ID="TextBox5" runat="server" Width="125px" Text="30/12/2012" ></asp:TextBox>
<br />
<span class="style1"><strong>Trans # </strong></span>
<asp:TextBox ID="TextBox2" runat="server" style="margin-left: 94px"
Width="125px"></asp:TextBox>
<br />
<strong><span class="style1">Part </span></strong>
<asp:TextBox ID="TextBox3" runat="server" style="margin-left: 117px"
Width="125px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" HorizontalAlign="Left"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="text-align: center" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="C6946_TRANS_TYPE" HeaderText="Type de Transaction/Transaction Type"
SortExpression="C6946_TRANS_TYPE" />
<asp:BoundField DataField="C6946_TRANS_DATE"
HtmlEncode="false" HeaderText="Date de Transaction/Transaction Date" ConvertEmptyStringToNull="false"
SortExpression="C6946_TRANS_DATE" FooterStyle-HorizontalAlign="Right" DataFormatString="{0:d}"
>
"
SelectCommand="SELECT C6946_TRANS_TYPE, C6946_TRANS_DATE, C6946_SO, C6946_LATE_FLAG, C6946_TRANS_NUM, ALIGNED_PART_NUMBER, C1001_ENG_PART_NUMBER, C6946_PART_KEY, PART_DESCRIPTION, C6946_RECORD FROM DWH.AIXW2S_T6946_TRANS_IMS"
FilterExpression= "C6946_TRANS_NUM LIKE '%{0}%' AND (C6946_PART_KEY LIKE '%{1}%' OR ALIGNED_PART_NUMBER LIKE '%{1}%' OR C1001_ENG_PART_NUMBER LIKE '%{1}%') AND C6946_TRANS_DATE >= #{2}# AND C6946_TRANS_DATE <= #{3}#">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox2" Name="C6946_TRANS_NUM" PropertyName="text" Type="String" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox3" Name="C6946_PART_KEY" PropertyName="text" Type="string" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox4" Name="C6946_TRANS_DATE" PropertyName="text" Type="DateTime" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox5" Name="C6946_TRANS_DATE" PropertyName="text" Type="DateTime" DefaultValue="" ConvertEmptyStringToNull="false"/>
</FilterParameters>
</asp:SqlDataSource>
<br />
</asp:Content>
You could be hitting the memory limit in a 32-bit worker process (if that is what you are using). Even if this went through, the browser would probably crash. This would be better suited and more realistic for a desktop application.
You may need an architecture redesign. A paged query to the database (via a REST API call perhaps?) to pull in the desired records, or perhaps a NoSQL alternative if you are running into query performance issues when performing JOINs between tables of such size.