SqlDataSource ControlParameter Null Value - asp.net

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

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}'

SqlDataSource Populate dropdownlist with or without parameter

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>

Syntax error using INNER JOIN and SqlDataSource with MySQL

I am trying to write an update command that joins 2 tables using an SqlDataSource. I have it working with 1 table, but when I put my INNER JOIN syntax in I get thrown an error. It says "My syntax is wrong. Check MySql manual for correct syntax"
Here is my Code from my ASPX page.:
<asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server"
ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>"
ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>"
SelectCommand="individual_AddressByIndividualID"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE individual SET
FarmName = #FarmName,
FirstName = #FirstName,
MiddleName = #MiddleName,
Address1 = #Address1,
City = #City
INNER JOIN address a ON i.IndividualID = a.IndividualID,
WHERE IndividualID=#IndividualID">
<SelectParameters>
<asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
<asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
</UpdateParameters>
</asp:SqlDataSource>
UPDATE
I have it working with 2 tables now. However it updates every Individual in the Table. For example: I am trying to update 1 person and I change the first name to Mark. When I click update , it changes everyone in the Database First Name to Mark.
Here is my new Code from my ASPX page.:
<asp:SqlDataSource ID="AdminSalesmanDetailDS" runat="server"
ConnectionString="<%$ ConnectionStrings:intelliairConnectionString %>"
ProviderName="<%$ ConnectionStrings:intelliairConnectionString.ProviderName %>"
SelectCommand="individual_AddressByIndividualID"
SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE individual i
inner join address a
on a.individualID = i.individualID
set
i.FarmName = #FarmName,
i.FirstName = #FirstName,
i.LastName = #LastName,
i.MiddleName = #MiddleName,
i.Phone = #Phone,
i.PhoneExtention = #PhoneExtention,
i.MobilPhone = #MobilPhone,
i.Fax = #Fax,
i.Email = #Email,
a.Address1 = #Address1,
a.Address2 = #Address2,
a.City = #City,
a.State = #State,
a.Zip = #Zip,
a.Country = #Country
where
i.IndividualID = i.IndividualID">
<SelectParameters>
<asp:ControlParameter ControlID="gvSalesman" Name="oIndividualID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="FarmName" ControlId="fvAdminSalesmanDetail$CompanyTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="FirstName" ControlId="fvAdminSalesmanDetail$FirstNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="MiddleName" ControlId="fvAdminSalesmanDetail$MiddleNameTextBox" PropertyName="Text"/>
<asp:ControlParameter Name="Address1" ControlId="fvAdminSalesmanDetail$Address1TextBox" PropertyName="Text"/>
<asp:ControlParameter Name="City" ControlId="fvAdminSalesmanDetail$cityTextBox" PropertyName="Text"/>
</UpdateParameters>
</asp:SqlDataSource>
You have your JOIN after the SET, and you're not being specific about which table each field is referencing. I think, to use JOIN in an UPDATE, you need syntax more like this:
UpdateCommand=" UPDATE
individual i
INNER JOIN
address a
ON i.IndividualID = a.IndividualID
SET
i.FarmName = #FarmName,
i.FirstName = #FirstName,
i.MiddleName = #MiddleName,
a.Address1 = #Address1,
a.City = #City
WHERE
i.IndividualID=#IndividualID" >
Edit: Based on the update to your question, it looks like you have (in your WHERE clause)
i.IndividualID=i.IndividualID
This is what's causing all your records to be updated (because that statement is always true). As in my above example, you need to have
i.IndividualID=#IndividualID
This way only the row(s) whose ID matches your parameter gets updated (presumably just one).

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?

Resources