Change Data Sources on ASP.net - asp.net

Issue: I have 2 datasources declared on my .aspx file as shown below:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="UsuarioDelete" SelectMethod="SelectMyAllNotME_Data" TypeName="BLLayer.Usuarios" OnInserted="ObjectDataSource_Inserted">
<DeleteParameters>
<asp:ControlParameter ControlID="ConfirmDialogBox1" Name="id_usuario" PropertyName="Aceptar_CommandName" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:SessionParameter DefaultValue="1" Name="id_usuario" SessionField="id_usuario" Type="Int32" />
<asp:SessionParameter DefaultValue="1" Name="id_pais" SessionField="id_pais" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="UsuarioDelete" SelectMethod="SelectMyAllNotME_Data" TypeName="BLLayer.Usuarios" OnInserted="ObjectDataSource_Inserted">
<DeleteParameters>
<asp:ControlParameter ControlID="ConfirmDialogBox1" Name="id_usuario" PropertyName="Aceptar_CommandName" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0" Name="id_emp" QueryStringField="id_emp" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
The method "SelectMyAllNotME_Data" is an overloaded method that in one he receives 2 values and in the other just one value, it is used to filter the information.
What I want to do is than in my onLoadPage method is something like this:
if (Request.QueryString["id_emp"] != null)
{
GridViewUsers.DataSource = ObjectDataSource2.SelectMethod;
GridViewUsers.DataMember = ObjectDataSource2.SelectMethod;
}
else
{
GridViewUsers.DataSource = ObjectDataSource1.SelectMethod;
GridViewUsers.DataMember = ObjectDataSource1.SelectMethod;
}
}
I've already tried this, but it isn't working, can anyone help me please?

You should call
GridViewUsers.DataBind();

on the datasource in aspx just do
EnablePaging="true"

Related

Go to Event or SelectMethod definition from markup

Similar to F12 hotkey from code, I would like to go to the method that is used in the markup, eg.
<asp:ObjectDataSource ID="dsADDOCs" runat="server" SelectMethod="DataSet_SelectForProject"
TypeName="DataLogic.tADDOCList" OnSelecting="dsADDOCs_Selecting">
<SelectParameters>
<asp:Parameter DefaultValue="" Name="iORDRID" Type="Int32" />
<asp:Parameter DefaultValue="" Name="iAccesses" ConvertEmptyStringToNull="true" Type="String" />
<asp:Parameter Name="iTracer" Type="String" ConvertEmptyStringToNull="true" DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
So here if I would ctrl+click or F12 on dsADDOCs_Selecting, than it would go to the codebehind to the dsADDOCs_Selecting method, or when I do the same on DataSet_SelectForProject, than it would open DataSet_SelectForProject method in DataLogic.tADDOCList
Is there an option, plugin or anything to do this? It would be really helpful.

How do I force refresh an ObjectDataSource?

In my ASP.NET web forms I find that the ObjectDataSource won't update/refresh when the SelectParameter values do not change.
How do I force refresh an ObjectDataSource regardless of whether or not the parameter values change?
Example code:
<asp:ObjectDataSource ID="odsUserSearchResults" runat="server"
SelectMethod="GetData" EnablePaging="false"
TypeName="MySolution.ObjectDataSources.Users">
<SelectParameters>
<asp:ControlParameter Name="name" ControlID="txtName" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="statusId" ControlID="ddlStatus" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
You need to disable the object data source view state by setting the EnableViewState property to false, like so:
<asp:ObjectDataSource ID="odsUserSearchResults" EnableViewState="false" runat="server"
SelectMethod="GetData" EnablePaging="false"
TypeName="MySolution.ObjectDataSources.Users">
<SelectParameters>
<asp:ControlParameter Name="name" ControlID="txtName" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="statusId" ControlID="ddlStatus" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

How to configure SqlDataSource to handle a return value from Stored Procedure

I have a listview and in the listview Update commmand I am attempting to set the parameters defined in my SqlDataSource UpdateParameters property. When Update command fires, I recieve an error message stating
Procedure or function 'MyStoredProcedure' expects parameter '#ReturnResult', which was not supplied.
Here's the aspx with the SQL DataSource:
<asp:SqlDataSource ID="sqlDataForCustomers" runat="server"
ConnectionString="<%$ ConnectionStrings:GS_ConnectionString %>"
CancelSelectOnNullParameter="False"
SelectCommand="usp_WDE_Get_DG"
SelectCommandType="StoredProcedure"
UpdateCommand="usp_WDE_Save"
UpdateCommandType="StoredProcedure" >
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="GID" Type="Int32" />
<asp:Parameter DefaultValue="0" Name="Order" Type="Int32" />
<asp:Parameter DefaultValue="0" Name="PID" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="PID" Type="Int32" />
<asp:Parameter Name="ProID" Type="Int32" />
<asp:Parameter Name="GID" Type="Int32" />
<asp:Parameter Name="DGapID" Type="Int32" />
<asp:Parameter Name="DEntryID" Type="Int32" />
<asp:Parameter Name="DEntrySetID" Type="Int32" />
<asp:Parameter Name="DEntryResult" Type="String" Size="250"/>
<asp:Parameter Name="DEServiceDate" Type="DateTime" />
<asp:Parameter Name="UserLoginID" Type="String" Size="50"/>
<asp:Parameter Name="UserPersonID" Type="Int32" />
<asp:Parameter Name="ServiceDate" Type="DateTime" />
<asp:Parameter Name="DGRowId" Type="Int32" />
<asp:Parameter Name="SRowId" Type="Int32" />
<asp:Parameter Direction="ReturnValue" Name="RetResult" Type="Boolean" DefaultValue="True" />
<asp:Parameter Direction="ReturnValue" Name="RetMsg" Type="String" Size="50" DefaultValue=""/>
</UpdateParameters>
Here is the ItemUpdating event of the LV in the code behind:
Sub CustomersGroupedByDataField_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles CustomersGroupedByDataField.ItemUpdating
Dim rowValue As Integer = e.ItemIndex
Dim DataGapTypeBeingUpdated As String = CustomersGroupedByDataField.DataKeys.Item(rowValue).Value.ToString()
Dim boolTest As Boolean
boolTest = True
GetRowValues(DataGapTypeBeingUpdated)
CheckOOS()
sqlDataForCustomers.UpdateParameters("PID").DefaultValue = intPID.Value
sqlDataForCustomers.UpdateParameters("ProID").DefaultValue = intProID.Value
sqlDataForCustomers.UpdateParameters("GID").DefaultValue = intGID.Value
sqlDataForCustomers.UpdateParameters("DGapID").DefaultValue = intDGapID.Value
sqlDataForCustomers.UpdateParameters("DEntryID").DefaultValue = intDEntryID.Value
sqlDataForCustomers.UpdateParameters("DEntrySetID").DefaultValue = intDEntrySetID.Value
sqlDataForCustomers.UpdateParameters("DEntryResult").DefaultValue = strDEntryResultValue
sqlDataForCustomers.UpdateParameters("DEServiceDate").DefaultValue = CDate(dtServiceDate.Text)
sqlDataForCustomers.UpdateParameters("UserLoginID").DefaultValue = Convert.ToString(Session("LoginID"))
sqlDataForCustomers.UpdateParameters("UserPersonID").DefaultValue = Convert.ToString(Session("PersonID"))
sqlDataForCustomers.UpdateParameters("ServiceDate").DefaultValue = CDate(dtServiceDate.Text)
sqlDataForCustomers.UpdateParameters("DGRowID").DefaultValue = intDGapRowID.Value
sqlDataForCustomers.UpdateParameters("SRowID").DefaultValue = intSRowID.Value
End Sub
I'm at a loss as to how I should specify the return value as a parameter?
How to handle the Return value (RetResult & RetMsg)?
Thanks for looking,
There is a direction attribute of select parameters that marks an input parameter as a reference parameter. The same property is accessible in your code.
<SelectParameters>
<asp:Parameter Direction="ReturnValue" Name="MyReturnParameter" Type="Int32" />
</SelectParameters>
After the command executes you can retrieve the value using parameter.Value or bind it to a ui element.
I think you can find your solution here,
https://forums.asp.net/t/1049910.aspx?How+to+get+return+value+or+output+value+from+stored+procedure+with+sqldatasource+control+
In order to get the return value you need to declare a return paramater in sqldatasource... something like this
<InsertParameters>
Your Parameters
<asp:Parameter Direction="ReturnValue" Type="Int16" Name="ReturnValue" />
</InsertParameters>
Then you need to override sqldatasource's Inserted event like this ( here you can get the return value)
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
Response.Write(e.Command.Paramaters["ReturnValue"].Value);
}

order gridview list decending

I currently have a table of data which is ordered in ID order, but i want the list to be ordered in descending order. This i am sure is a simple question but i just can not figure out how to do it.
Here is the ObjectDataSource etc. I am just not sure where to change the code...
Thanks
<asp:ObjectDataSource
ID="odsTours"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Tour"
SortParameterName="sortExpression">
<SelectParameters>
<asp:Parameter
Name="isActive"
ConvertEmptyStringToNull="true"
DefaultValue="" />
<asp:Parameter
Name="isInThePast"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource
ID="odsBookings"
runat="server"
SelectMethod="GetFiltered"
TypeName="blive.Shop.Booking"
SortParameterName="sortExpression">
<SelectParameters>
<asp:ControlParameter
ControlID="ddlTourFilter"
PropertyName="SelectedValue"
Name="tourId"
Type="Int32"
ConvertEmptyStringToNull="true" />
<asp:Parameter
Name="username"
ConvertEmptyStringToNull="true"
DefaultValue="" />
</SelectParameters>
</asp:ObjectDataSource>
It should be done in the select method of the object data source.
In your case it should be in
GetFiltered method.
You can apply the order by in this method.

How to instruct ObjectDataSource to match form parameter with method parameter

When working with ObjectDataSource I have following select:
<asp:DropDownList runat="server" SelectedValue='<%# Bind("InceptionCycle.ID") %>' ID="InceptionCycle"
DataSourceID="odsAllCycles" DataTextField="CycleName" DataValueField="ID" AppendDataBoundItems="true">
<asp:ListItem Text="Choose..." Value=""></asp:ListItem>
</asp:DropDownList>
and ObjectDataSource configured with UpdateMethod and InsertMethod. However insert/update methods have parameters with InceptionCycle name (because InceptionCycle.ID is not valid identifier).
Is there a way to instruct ObjectDataSource to take InceptionCycle.ID form parameter and place in as InceptionCycle method parameter?
Here's full ObjectDataSource code:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Sources.DomainSource"
SelectMethod="FindById" InsertMethod="Add" UpdateMethod="Update"
OnInserted="ods_Inserted" OnUpdated="ods_Updated">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
</SelectParameters>
<InsertParameters>
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</InsertParameters>
<UpdateParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="ShortTitle" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="WMRId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="MDSId" />
<asp:Parameter ConvertEmptyStringToNull="true" Name="Status" />
</UpdateParameters>
</asp:ObjectDataSource>
You can only use the Bind syntax with a top level property - it doesn't support nested properties (VS 2012 will, though).
So, you should create a new property on your data source like below and bind the SelectedValue to this new property instead
public string InceptionCycleID
{
get
{
return InceptionCycle.ID;
}
set
{
InceptionCycle.ID = value;
}
}

Resources