Adding ControlParameter to SqlDataSource prevents query and databinding? - asp.net

I have a SqlDataSource that calls a stored procedure and it works fine. If I add a <ControlParameter> tag to add an additional argument, then the query never fires and the databinding never occurs. Suggestions?
This works:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
When I add the ControlParameter, the databinding no longer occurs:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultDB %>"
SelectCommand="SP_WHATEVER" SelectCommandType="StoredProcedure"
UpdateCommand="SP_WHATEVER2" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="UserName" SessionField="RP_Program" Type="String" />
<asp:ControlParameter Name="SprocArgName" ControlID="ddlFilter" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<snip...>
</UpdateParameters>
</asp:SqlDataSource>
The ControlParameter refers to a valid object on the page. Any other suggestions?

Most likely one of the parameter is empty or null. Add CancelSelectOnNullParameter="false" to the asp:SqlDataSource and ConvertEmptyStringToNull="true" to both parameters. Once it works, tweak the parameters so that SP gets what it expects.

Related

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>

UpdateCommand with function

I have an UpdateCommand for a grid as follows:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KruSQL %>"
UpdateCommand="UPDATE [dbo].[Microbiology] SET RoomNum=#RoomNum, CollDate=#CollDate, WaterFixure=#WaterFixure Where ID=#ID">
<UpdateParameters>
<asp:Parameter Name="RoomNum" Type="String" />
<asp:Parameter Name="WaterFixure" Type="String" />
<asp:Parameter Name="CollDate" Type="DateTime" />
</UpdateParameters>
When the user enters a date I need to convert that to UTC. Is there a way to do this from the UpdateCommand - add a function. It didn't work when I tried it.
you could write a stored procedure and use that as your UpdateCommand ... inside the stored procedure you could convert the date to utc

<asp:ControlParameter - pass null value to DB via sqldatasource

I'm having problems passing a null value to a stored procedure, e.g. if an option isn't selected on a dropdown how can this be passed as null to through the sqdatasource to the database.
I currently have a number of dropdown controls on my page which are holding paramater values to get passed to a stored procedure.
When I select some values from these controls the gridview will display the results that it should, but what I'm having a problem with is when no values are selected is passing a null value to the SP. I've checked the SP and when I execute it and pass in null values it gets me the correct results so I'm happy with the SP. I've tried
ConvertEmptyStringToNull="true" DefaultValue=""
settings in the control paramater with no luck, and the dropdown's "ALL" option has a value of ""
The code for the sqldatasource is:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:scConnString %>"
SelectCommand="spGetOrgTickets" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:SessionParameter Name="org_id" Type="Int32" SessionField="org_id" DefaultValue="" ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="drpPriority" Name="priority_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue="" />
<asp:ControlParameter ControlID="drpStatus" Name="status_id" PropertyName="SelectedValue"
Type="Int32" ConvertEmptyStringToNull="true" DefaultValue=""/>
</SelectParameters>
</asp:SqlDataSource>
One of the dropdown's is :
<asp:DropDownList Style="width: 100%" ID="drpStatus" runat="server" class="field select"
AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="SqlDataSource2_Status"
DataTextField="status" DataValueField="status_id">
<asp:ListItem Value="">All</asp:ListItem>
</asp:DropDownList>
Any ideas?
Thanks
Sorted...just need CancelSelectOnNullParameter="false" within sqldatasource.

empty record when binding sqldatasource from c# code

i am trying to bind an sqldatasource from the code by passing variable from another page, but when i run the report i am getting 0 records,
, my code as fllow
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WorkflowConnStr %>"
SelectCommand="SELECT * FROM tbl_ServiceTracking WHERE (ActorUID = #strUserId) AND (RequestedDate >= #DateFrom) AND (RequestedDate <= #DateTo)">
<SelectParameters>
<asp:Parameter Name="strUserId" />
<asp:Parameter Name="DateFrom" />
<asp:Parameter Name="DateTo" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" />
c# code
string strUserId = Request.QueryString["userid"];
string DateFrom = Request.QueryString["dtfrom"];
string DateTo = Request.QueryString["dtto"];
SqlDataSource1.SelectParameters.Add("strUserId", strUserId);
SqlDataSource1.SelectParameters.Add("DateFrom", DateFrom);
SqlDataSource1.SelectParameters.Add("DateTo", DateTo);
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
did i miss something?
I solved this problem by changing my code
SqlDataSource1.SelectParameters["strUserId"].DefaultValue = strUserId;
SqlDataSource1.SelectParameters["DateFrom"].DefaultValue = DateFrom;
SqlDataSource1.SelectParameters["DateTo"].DefaultValue = DateTo;
You can also set select parameter in design mode:
<SelectParameters>
<asp:QueryStringParameter Name="UserID" QueryStringField="userid"
Type="String" DefaultValue="userid" />
<asp:QueryStringParameter DefaultValue="dtfrom" Name="dtfrom"
QueryStringField="dtfrom" Type="String" />
<asp:QueryStringParameter DefaultValue="dtto" Name="dtto"
QueryStringField="dtto" Type="String" />
</SelectParameters>

ASP VB .NET SqlDataSource Update Error Fails

In VS 2005, using VB, page has a FormView linked to an SqlDataSource. When data is changed and Update button pressed, changed data is cleared in FormView but database table is not updated. Below is the SqlDataSource code. Any ideas why the Update doesn't work?
<asp:SqlDataSource ID="SqlDataDetails" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ALFSConnectionString %>"
...
...
OldValuesParameterFormatString="original_{0}"
ProviderName="<%$ ConnectionStrings:ALFSConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [Resident] WHERE ([Resident_ID] = ?)"
UpdateCommand="UPDATE [Resident] SET [Resident_Company_ID] = ?, ..., [Resident_Diet] = ?, [Resident_Social_Security] = ? WHERE [Resident_ID] = ?" >
<UpdateParameters>
<asp:SessionParameter Name="Resident_ID" SessionField="Resident_ID" Type="String" />
<asp:Parameter Name="Resident_Company_ID" Type="Int32" /> ...
...
...
<asp:Parameter Name="original_Resident_Diet" Type="String" />
<asp:Parameter Name="original_Resident_Social_Security" Type="Int32" />
</UpdateParameters>
...
...
<SelectParameters>
<asp:SessionParameter Name="Resident_ID" SessionField="Resident_ID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Have a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx
What is your DataSourceMode, and/or are you calling .Update() if applicable?

Resources