Parameter with ObjectDataSource - c#

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>

Related

ObjectDataSource calling multiple times in nested gridview

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.

VS 2012 and SelectParameter in asp .net

Hi I want to bind text string in SelectCommand code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:finchsize_polibudaConnectionString %>"
SelectCommand="SELECT ocena FROM Oceny AS lista WHERE przedmiot = #ID"/>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="0" Name="ID"
PropertyName="Text" Type="String" />
</SelectParameters>
But my VS 2012 said that: 1.Validation(XHTML5): Element SelectParameters is not supported. 2. Element 'ControlParameter' is not a known element. Do yuo know how to fix that, or is it any other way to bind some text in SelectCommand ?
P.S. I tried
<asp:QueryStringParameter />
as well but it gives me exactly the same error.
The SelectParameters element needs to be a child of SqlDataSource. Right now you have it as a peer
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:finchsize_polibudaConnectionString %>"
SelectCommand="SELECT ocena FROM Oceny AS lista WHERE przedmiot = #ID">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" DefaultValue="0" Name="ID"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Asp.net SelectMethod not firing on second submit click

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)
{ ... }

Running an SQL query with filterexpressions from textboxes

I'm currently running a query in an ASP.NET gridview that uses filters from textboxes. My current code looks like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ERPW2MIGConnectionString %>"
SelectCommand="SELECT TOP 1000000 * 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="TRANS_NUM" PropertyName="text" Type="String" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox3" Name="PART_KEY" PropertyName="text" Type="string" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox4" Name="TRANS_DATE" PropertyName="text" Type="DateTime" DefaultValue="" ConvertEmptyStringToNull="false"/>
<asp:ControlParameter ControlID="TextBox5" Name="TRANS_DATE2" PropertyName="text" Type="DateTime" DefaultValue="" ConvertEmptyStringToNull="false"/>
</FilterParameters>
</asp:SqlDataSource>
What I wanted help with is this: is there a way to run my filters in the select command directly as a WHERE statement? My query takes forever to run and I don't know the exact way the filterexpressions come into effect (i.e. is the query run, then filtered, or does it run with the filters from the start?). Also, could someone suggest any way to optimize the query?
Thanks.
Instead of FilterParameters use SelectParameters. Following is an example
SelectCommand="SELECT TOP 1000000 * FROM DWH.AIXW2S_T6946_TRANS_IMS
WHERE C6946_TRANS_NUM = #TRANS_NUM AND ...."
<selectparameters>
<asp:ControlParameter ControlID="TextBox2"
Name="TRANS_NUM" PropertyName="text" Type="String" DefaultValue=""
ConvertEmptyStringToNull="false"/>
...
</selectparameters>

Formview EditItemTemplate in asp.net c#

I have a product table that am updating. the fields updated are category_id, prod_name, prod_desc, prize, status. I am using a formview to do that updating. however i am having problems with the category_id. the category_id is a databound dropdownlist using a sqldatasource to fetch id and name from category table in the database. When a product has no category_id value, a null value is entered by default by sql.
The problem comes when i go to edititemtemplate mode in formview, the dropdownlist cannot show a null value from db so it throws an exception of:
'category_idDropdown' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value
this is my code in the updateprod.aspx page:
<asp:DropDownList ID="category_idDropdown" runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
DataSourceID="catnames"
DataTextField="category_name"
DataValueField="category_id" ViewStateMode="Enabled"
SelectedValue='<%# Bind("category_id") %>' >
<asp:ListItem Text="-- Choose Category--" Value="0" Selected="True">
</asp:ListItem>
</asp:DropDownList>
this is my sqldatasource code the dropdownlist is databound to:
<asp:SqlDataSource ID="catnames" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT [category_id], [category_name] FROM [category]">
</asp:SqlDataSource>
& finally this is the sqldatasource used to update the product details:
<asp:SqlDataSource ID="productUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT prod_id, category_id, prod_name, prod_desc, prod_price, img_name, img_contenttype, prod_img, status FROM product WHERE (prod_id = #prod_id)"
UpdateCommand="UPDATE product SET category_id = #category_id, prod_name = #prod_name, prod_desc = #prod_desc, prod_price = #prod_price, status = #status WHERE (prod_id = #prod_id)">
<SelectParameters>
<asp:QueryStringParameter Name="prod_id" QueryStringField="prod_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="category_id" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="prod_name" Type="String" />
<asp:Parameter Name="prod_desc" Type="String" />
<asp:Parameter Name="prod_price" Type="Decimal" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="prod_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
i looked up in the questions regarding the formview and edititemtemplate in this site, but none came close to give me any idea on how to approach this problem.
I tried to solve it programatically but not success in cracking this problem. Please help me, i looked for the solution for 3 weeks. no success :(
Try rebinding your datasource prior to the null value being set:
protected void myFormView_OnItemEditing(object sender, ListViewEditEventArgs e)
{
DropDownList category_idDropdown = (DropDownList)(myFormView.FindControl("category_idDropDown"));
category_idDropdown.DataBind();
category_idDropdown.Items.Add(0, new ListItem("Select", null));
}
The problem likely isn't that you're trying to select a value that doesn't exist, but rather that you're trying to bind a DropDownList that already has a null value to a datasource that doesn't contain that value.

Categories