How do I get the SQL Where clause to handle TWO StringVariables? - asp.net

I pass TWO StringQueries to new page: /Find.aspx?Color=Blue&Shape=Round
In my Products.aspx I have a GridView with a SelectCommand:
SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo]
FROM [MSD_Store] WHERE ([Color] = ?)"
How do I get the Where clause to handle TWO StringVariables?

Just add necessary part of the SQL query and define another query string parameter:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo] FROM [MSD_Store] WHERE ([Color] = ?) AND ([Shape] = ?)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="Color" Name="color" />
<asp:QueryStringParameter QueryStringField="Shape" Name="shape" />
</SelectParameters>
</asp:SqlDataSource>

Simply doing
SelectCommand: SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo] FROM [MSD_Store] WHERE ([Color] = ? AND [Shape = ?])
and then you add another parameter definition, reading your querystring value

Do you mean this
SELECT DISTINCT [Name], [Price], [ProductNo]
FROM [MSD_Store] WHERE ([Color] = ?) AND ([Shape] = ?)
by "two" string variables?

Related

Order By Desc is not working in SqlDataSource

I'm using repeaters to show data in webform. But I have a problem with this code. when I select * data from BidTAble, it shows in Desc order, but when I use INNER JOIN, Desc doesn't work.! :/
Here i've uploaded a picture of BidTable content, i want Saim data on the top of table instead of Awais
Code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:CONN %>"
SelectCommand="SELECT BidTable.BidID, BidTable.RegID, BidTable.CarID, Reg.username, BidTable.BidAmount FROM [BidTable] INNER JOIN Reg ON BidTable.RegID = Reg.ID WHERE ([CarID] = #CarID) ORDER BY [BidTable].[BidAmount] DESC">
<SelectParameters>
<asp:QueryStringParameter Name="CarID"
QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

FilterExpression search bar - multiple data types (int and varchar)

I have a textbox that I want to make into a search bar. My table has an int "id" and varchar(50) "name". I want to search for either the name or the id in the same textbox. Is that even possible? I've tried many things but haven't gotten to a solution yet.
Here's what I have:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:New_QuickBooksConnectionString %>"
SelectCommand="SELECT * FROM [Testing1]" FilterExpression="([id] = {0}) OR ([name] LIKE '%(CAST {0} AS VARCHAR(50))%')">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" Type="int32" />
</FilterParameters>
</asp:SqlDataSource>
This just returns the id when I type in a number. It doesn't work for name. What can I do?
Try changing the type = "String" and convert ID to String, like CONVERT([Id],'System.String') like '{0}'

QueryStringField: When value is not specified

I am using querystring for passing a value which is used to select the subscription category (variable name subno) on second page.
Based on the subscription category (subno is specified as integer in sql server as part of table categories) second page is invoked using the following URL-
http://localhost:53175/v1/ProductsList.aspx?subno=1
or
http://localhost:53175/v1/ProductsList.aspx?subno=2
In the second page subno is used to query the items based on subno passed.
<asp:SqlDataSource ID="subscription" runat="server"
ConnectionString="<%$ ConnectionStrings:aimnbde3ConnectionString %>"
SelectCommand="SELECT [subscription] FROM [subscription] WHERE ([subno] = #subno)">
<SelectParameters>
<asp:QueryStringParameter Name="subno" QueryStringField="subno" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
This code works well if category(varibale subno) is passed.
In case second page is called directly like
http://localhost:53175/v1/ProductsList.aspx
Then value of subno is interpreted as 0. Do we have a way to change the query(SelectCommand="SELECT [subscription] FROM [subscription] ) based on value of subno ?
Or please suggest the right way to handle the case where subno is not passed
Editing -
Sorry for missing an important part of question-
When http://localhost:53175/v1/ProductsList.aspx is accessing i want to display all the products so the query will be
SELECT [subscription] FROM [subscription]
and in case a value is passed query will be
SELECT [subscription] FROM [subscription] WHERE ([subno] = #subno)
I would change the query to this:
SELECT [subscription] FROM [subscription] WHERE (#subno is null or [subno] = #subno)
and then add the following to the parameter
ConvertEmptyStringToNull = "TRUE"
and the following to the SQLDataSource
CancelSelectOnNullParameter = "FALSE"
So your code would look like this:
<asp:SqlDataSource ID="subscription" runat="server" CancelSelectOnNullParameter="false"
ConnectionString="<%$ ConnectionStrings:aimnbde3ConnectionString %>"
SelectCommand="SELECT [subscription] FROM [subscription] WHERE (#subno is null or [subno] = #subno)">
<SelectParameters>
<asp:QueryStringParameter Name="subno" QueryStringField="subno" Type="Int32" ConvertEmptyStringToNull = "true"/>
</SelectParameters>
</asp:SqlDataSource>
You could simply change where part of SQL query to :
WHERE (#subno = 0 OR [subno] = #subno)

SqlDataSource ControlParameter Null Value

I am using SqlDataSource control to list out search result when user choose the date, if the date is null, then it list out all record.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" meta:resourcekey="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:Test_ConnectionString %>"
SelectCommand="select MeetingID, MeetName as MeetingName, MeetDate, MeetTime from Meeting where Status ='Recorded' and Case when #sel_to_date ='' then MeetDate <= '2200-12-31' else MeetDate = #sel_to_date end order by MeetDate desc, Meettime desc ">
<SelectParameters>
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="datepicker" Name="sel_to_date" DefaultValue="" PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
But it return syntax error.
I want all to list all records when user leave the textbox blank. How to do that?
regards,
Joe
<asp:SqlDataSource ID="SqlDataSource1" runat="server" meta:resourcekey="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:Test_ConnectionString %>"
SelectCommand="select MeetingID, MeetName as MeetingName, MeetDate, MeetTime from Meeting where Status ='Recorded' and Case when #sel_to_date is null then MeetDate <= '2200-12-31' else MeetDate = #sel_to_date end order by MeetDate desc, Meettime desc ">
<SelectParameters>
<asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="datepicker" Name="sel_to_date" DefaultValue="" PropertyName="Text" Type="String"/>
</SelectParameters>
</asp:SqlDataSource>
I did not check it but if you pass empty value than it convert to null so you have to check
#sel_to_date is null instead #sel_to_date = ''
use my snippet of code.
Your case statement is incorrect. The case statement in SQL should return a value, not perform a comparison. See CASE (Transact-SQL)
A solution would be to replace the case with a conditional like so:
select
MeetingID, MeetName as MeetingName, MeetDate, MeetTime
from Meeting
where Status ='Recorded'
and
(
(#sel_to_date ='' and MeetDate <= '2200-12-31' )
or
MeetDate = #sel_to_date
)
order by MeetDate desc, Meettime desc
For your convenience, here it is on a single line so you can copy-paste it into the data source declaration:
select MeetingID, MeetName as MeetingName, MeetDate, MeetTime from Meeting where Status ='Recorded' and ((#sel_to_date ='' and MeetDate <= '2200-12-31' ) or MeetDate = #sel_to_date) order by MeetDate desc, Meettime desc

ASP.NET DataSource with parameters for SQL IN clause

<asp:SqlDataSource ID="DataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DbConnection %>"
ProviderName="<%$ ConnectionStrings:DbConnection.ProviderName %>"
SelectCommand="select somefield from sometable where somefield in (?)">
<SelectParameters>
<asp:Parameter DefaultValue="" Name="SomeFiedValue" />
</SelectParameters>
</asp:SqlDataSource>
How (from my codebehind) do I allocate a list of values for the parameter in the above SQL query?
try this.
protected void DataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["SomeFieldValue"].Value = yourdata;
}
EDIT:
Based on your answers, please have a look at the following website. It explains one way to solve this problem with DB2.
http://www.tek-tips.com/viewthread.cfm?qid=1443561
Based on the comments on that page, a possible solution for your problem could be:
select somefield from sometable where somefield in
(select ? from SYSIBM.SYSDUMMY1)

Resources