Passing null in textboxes not working - asp.net

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="%" />

Related

How do I filter on 5 parameters, including a dropdown list using filter expression?

I'm trying to filter my gridview with 5 different parameters. I can get it to work fine but the issue arises when I add the "All" into the dropdownlist. If I select All in the dropdownlist I can't filter anymore. It will show the results for everything but filtering doesn't work. Is there something wrong in my filter expression or control parameter?
This is my current dropdownlist:
<asp:DropDownList ID="DDL1" runat="server" AutoPostBack="true"
DataTextField="Tab" DataValueField="Tab" AppendDataBoundItems="true" >
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="Tr">Tr</asp:ListItem>
<asp:ListItem Value="Out">Out</asp:ListItem>
<asp:ListItem Value="In">In</asp:ListItem>
</asp:DropDownList>
This is my source for the filtering:
<asp:SqlDataSource
SelectCommand="SELECT * FROM Log ORDER BY TimeStamp DESC"
FilterExpression="Com LIKE '%{0}%' AND Usr LIKE '%{1}%'
AND Tab = '{2}' AND TimeStamp >= '#{3}#' AND TimeStamp <= '#{4}#'">
<FilterParameters>
<asp:ControlParameter ControlID="TB1" Name="Com" PropertyName="Text" Type="String"
ConvertEmptyStringToNull="false" />
<asp:ControlParameter ControlID="TB2" Name="Usr" PropertyName="Text" Type="String"
ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="Tab" ControlID="DDL1"
PropertyName="SelectedValue" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="Date" ControlID="DateFrom" Type="DateTime" PropertyName="Text"
ConvertEmptyStringToNull="false" />
<asp:ControlParameter Name="Date" ControlID="DateTo" Type="DateTime"
PropertyName="Text" ConvertEmptyStringToNull="false" />
</FilterParameters> </asp:SqlDataSource>
Update the Value of your ALL option in dropdown list
<asp:ListItem Value="-1">All</asp:ListItem>
Update your control parameter setting default value and removing ConvertEmptyStringToNull=false
<asp:ControlParameter Name="Tab" ControlID="DDL1"
PropertyName="SelectedValue" Type="String" DefaultValue="-1"/>
Update your filter expression for Tab
FilterExpression="Com LIKE '%{0}%' AND Usr LIKE '%{1}%'
AND (Tab = '{2}' or '{2}' = '-1') AND TimeStamp >= '#{3}#' AND TimeStamp <= '#{4}#'">

Set SqlDataSource parameter to null

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>

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"

SelectCommand doesn't work when ControlParameter is not given

I have a textbox with id = txt_SearchLibrary which is also my controlparameter that I am using to filter my sqldatasource, I want to get all the results when I don't type something in my searchtext box, but below codes results 0 rows. I tried 2 way but both didn't work.
first one:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>"
SelectCommand="SELECT * FROM [Books] WHERE ([BookName] LIKE '%' + #searchText + '%') OR #searchText IS NULL">
<SelectParameters>
<asp:ControlParameter ControlID="txt_SearchLibrary" Name="searchText"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
second one:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>"
SelectCommand="SELECT * FROM [Books] WHERE ([BookName] LIKE '%' + #searchText + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txt_SearchLibrary" DefaultValue="" Name="searchText"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The Text property is probably "" or String.Empty, rather than NULL which you're checking for in your SQL statement.

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