SqlDataSource Populate dropdownlist with or without parameter - asp.net

I use SqlDataSource control to list out search result when user choose a date, but if the date is null, then it will list out all record. How to do the sql command?
SELECT Ref_No as name, Job_Order_ID as value
FROM Job_Order
WHERE (Status <> 'JO_Completed') AND (Delivery_Date = #jaDate)
OR (Status <> 'JO_Completed') ORDER BY Ref_No
code to generate dropdownlist
<asp:SqlDataSource ID="NewJobOrderDS" runat="server" SelectCommand="SELECT Ref_No as name, Job_Order_ID as value FROM Job_Order WHERE (Status <> 'JO_Completed') AND (Delivery_Date = #jaDate) ORDER BY Ref_No" OnSelecting="NewJobOrderDS_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="txtJADate2" Name="jaDate" PropertyName="Text" Type="DateTime"/>
</SelectParameters>
</asp:SqlDataSource>

Related

Using the same DataSource by some DropDownList

I have a stored procedure that gets a parameter and retrieves a list of suppliers from the database:
Create proc spGetSuppliers
(#RowMeterialID int)
as
begin
select distinct sName
from Supplier, RawMaterials, SupplierRawMaterials
where RawMaterials.rmID = #RowMeterialID
and RawMaterials.rmID = SupplierRawMaterials.rmID
end
A datasource :
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:igroup9_test1ConnectionString %>"
SelectCommand="spGetSuppliers" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="" Name="RowMeterialID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And about 10 DropDownList that retrieve data from the database using the DataSource.
I want the first DropDownList to use the DataSource with Value=1 which should be sent to the stored procedure and the second DDL use the value=2 and so on.

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)

How do I get the SQL Where clause to handle TWO StringVariables?

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?

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

Resources