SelectCommand doesn't work when ControlParameter is not given - asp.net

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.

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

Passing Context.User.Identity.GetUserId() to an SQL statement returns no data

I have the following code but it doesn't seem to work. The <%= Context.User.Identity.GetUserId() %> has a value in the database but it's not returning any data..... Thanks
<asp:SqlDataSource ID="owner_information" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT TOP 1 [imgMain_user], [IDname] FROM [View-EditorLinks] WHERE ([IDuser] = #IDowner)">
<SelectParameters>
<asp:Parameter Name="IDowner" Type="String" DefaultValue="<%= Context.User.Identity.GetUserId() %>" />
</SelectParameters>
</asp:SqlDataSource>

Update dropDownList using SQL query

I am trying to update my dropdownlistB according to the categoryId chosen in dropdownlistA Using this code:
<asp:DropDownList ID="DropDownListA" runat="server" DataSourceID="SqlDataSourceA" DataTextField="Description" DataValueField="Description" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownListB" runat="server" DataSourceID="SqlDataSourceB" DataTextField="Title" DataValueField="Title"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceA" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Description] FROM [BookCategory]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceB" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Title] FROM [BooksInfo] WHERE ([CategoryId] = #CId)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="CId" QueryStringField="SELECT [CategoryId] FROM [BookCategory]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I am new to using SQL and queries in ASP.NET and cant figure out what Im doing wrong, dropdownlistB stays empty. (AutoPostBack = true in dropdownlistA, so it should update?)
I think what you are looking for is the asp:ControlParameter like this
<asp:ControlParameter ControlID="DropDownListA" PropertyName="SelectedValue"
Name="EmpID" Type="Int32" DefaultValue="0" />
So the the query is based on the selection of DropDownListA.

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

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