Set SqlDataSource parameter to null - asp.net

How do you set a parameter passed to a SqlDataSource to be null? I have the code below where the parameter "#var1" is set from a TextBox. However, if that value is not defined I need to set "#var1" to be null so that the coalesce returns true.
Is there a way to do this or am I going about it the wrong way?
I've also tried setting the DefaultValue="null" and dsABC.SelectParameters["#var1"]=null which both lead to "Input string was not in a correct format" errors.
<asp:SqlDataSource ID="dsABC" runat="server" ConnectionString="<%$ ConnectionStrings:abc123 %>"
SelectCommand=" SELECT col1, col2, col3
FROM table1 as T1
WHERE COALESCE(#var1, T.ID) = T.ID">
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>

Add the ConvertEmptyStringToNull and the CancelSelectOnNullParameter attributes. The first sets the parameter to null when empty, the second lets the query run (default behavior is to not run a query with a null parameter).
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16"
ConvertEmptyStringToNull="true"
CancelSelectOnNullParameter="false" />
</SelectParameters>

Related

Passing null in textboxes not working

Hi I m trying to pass null values in my date textboxes Textbox 3 and Textbox 4 but am not able to do so. Please let me know what I am doing wrong. Basically what I need is when the page load and the textboxes are empty I need Gridview to load the entire table . at present it only loads when I enter a date range but if I leave the date textboxes empty I get an empty page.I have an Ajax calendar extension for a drop down calendar control also attached to these two text boxes. Don't know if that is the problem. Please help..
Here is the code
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
SelectCommand="SELECT ID, Story_number, Date, Memory_card, Story_Name FROM Library WHERE (Story_Name LIKE '%' + #Story_Name + '%') AND (Story_number LIKE '%' + #Story_number + '%') AND (#startdate IS NULL OR #startdate = '' OR Date >= #startdate) AND (#enddate IS NULL OR #enddate = '' OR Date <= #enddate)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1"
Name="Story_Name"
PropertyName="Text"
DefaultValue="%" />
<asp:ControlParameter ControlID="TextBox2"
DefaultValue="%"
Name="Story_number"
PropertyName="Text" />
<%--<asp:ControlParameter ControlID="DropDownList1"
DefaultValue="%"
Name="Memory_card"
PropertyName="SelectedValue" />--%>
<asp:ControlParameter ControlID="TextBox3"
Name="startdate"
ConvertEmptyStringToNull="true"
PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox4"
Name="enddate"
ConvertEmptyStringToNull="true"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
It works when I put some ridiculous date range in the default values as under but I am not able to get it to pass null to the database so that I see the full table when the page loads
<asp:ControlParameter ControlID="TextBox3"
Name="startdate"
ConvertEmptyStringToNull="true"
DefaultValue="1/1/1977"
PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox4"
Name="enddate"
ConvertEmptyStringToNull="true"
DefaultValue="1/1/2100"
PropertyName="Text" />
Update:
Where do I add this code? I am adding it in the control parameter like this
<asp:ControlParameter ControlID="TextBox4"
Name="enddate"
SqlDataSource2.CancelSelectOnNullParameter="False"
PropertyName="Text" />
It gives me the following error when I do it
Literal content ('<asp:ControlParameter ControlID="TextBox4" Name="enddate" SqlDataSource2.CancelSelectOnNullParameter="False" PropertyName="Text" />') is not allowed within a 'System.Web.UI.WebControls.ParameterCollection'
CancelSelectOnNullParameter is a property of the SQLdatasource control, not the parameters contained in the control:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
SelectCommand="SELECT ID, Story_number, Date, Memory_card, Story_Name FROM Library WHERE (Story_Name LIKE '%' + #Story_Name + '%') AND (Story_number LIKE '%' + #Story_number + '%') AND (#startdate IS NULL OR #startdate = '' OR Date >= #startdate) AND (#enddate IS NULL OR #enddate = '' OR Date <= #enddate)">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" ...
or this property: DefaultValue="%"
<asp:ControlParameter ControlID="TxtSearch" Name="Title" PropertyName="Text" Type="String" DefaultValue="%" />

Why isn't my SqlDataSource's UpdateCommand working?

<asp:SqlDataSource ID="HopefulDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand= "SELECT id, regGroupID, amountReceived, other FROM table"
UpdateCommand="UPDATE table
SET [amountReceived] = #amountReceived
WHERE [regGroupID] = #regGroupID">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCourses" Name="ddlSelectedCourse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
other parameters
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
The above works when I change "WHERE [regGroupID] = #regGroupID" to either
WHERE [id] = #id
or
WHERE [regGroupID] = 2
You need to add "regGroupID" to the DataKeyNames collection in your GridView declaration. Something like this:
<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
...
</asp:GridView>
See the DataKeyNames documentation:
You must set the DataKeyNames property in order for the automatic
update and delete features of the GridView control to work. The values
of these key fields are passed to the data source control in order to
specify the row to update or delete.
Note: it's been a while since I used this, so you might need to include both the primary key, and your other key field. Like this:
DataKeyNames="id,regGroupID"

<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.

Using Query String Parameter to Select Column in asp.net

I am trying to use asp:QueryStringParameter to change witch SQL table column I want to get. But when I try, I just get the Query String Parameter as every row of a new column.
Here is what I have so far.
<asp:SqlDataSource ID="getContact" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>"
SelectCommand="
SELECT
[fName],
[lName],
#c as contact
FROM
RidesMaster
WHERE
[userID] = #ID">
<SelectParameters>
<asp:QueryStringParameter Name="c" QueryStringField="c" Type="String" />
<asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
You could do it using a CASE statement so long as your dynamic columns are of the same type or could be cast to the same type:
SELECT
[fName],
[lName],
CASE #c
WHEN 'column1' THEN [column1]
WHEN 'column2' THEN [column2]
WHEN 'column3' THEN [column3]
ELSE 'column4'
END as contact
FROM
RidesMaster
WHERE
[userID] = #ID

Select parameter not working on SqlDataSource

I am using the stored procedure below for the select command of a SqlDataSource, and I'm trying to pass a query string parameter, but I get this error:
Procedure or function 'ActivationCampaignGetById' expects parameter '#campaignId', which was not supplied.
The parameter is present in the query string:
http://localhost:62681/Activations/ActivationCampaignDetails.aspx?id=98
Here is my DataSource code:
<asp:SqlDataSource ID="campaignDataSource" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ProVantageMediaManagement %>"
SelectCommand="ActivationCampaignGetById" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="98" Name="campaignId"
QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And my stored procedure declaration:
ALTER procedure [dbo].[ActivationCampaignGetById]
#campaignId int
as
select
Not sure this will helps, but you need to set the parameter direction to Input:
<asp:QueryStringParameter DefaultValue="98" Name="campaignId"
Direction="Input" QueryStringField="id" Type="Int32" />

Resources