Parameterized Query OR Control - c#

I want to be able to use a QueryString parameter with a control parameter in an OR Clause on the ASPX page.
The below works for QueryString Parameter
Select * From Data1
WHERE ([Customer Name] = #C)
AND ([Order Number] = #O)
AND ('AD\' + Salesperson_AD_User_ID = #U)
AND ([Warehouse Id] = #W)
AND ([Use Description] is NOT NULL)
<SelectParameters>
<asp:querystringparameter QueryStringField="C" Name="C" />
<asp:querystringparameter QueryStringField="O" Name="O" />
<asp:querystringparameter QueryStringField="U" Name="U" />
<asp:querystringparameter QueryStringField="W" Name="W" />
</SelectParameters>
Below does not work
select * From Data1
WHERE ([Customer Name] = #Customer_Name OR [Customer Name] = #C)
AND ([Order Number] = #Order_Number OR [Order Number] = #O)
AND ('AD\' + Salesperson_AD_User_ID = #username OR'AD\' + Salesperson_AD_User_ID = #U)
AND ([Warehouse Id] = #Warehouse_Id OR [Warehouse Id] = #W)
AND ([Use Description] IS NOT NULL)
The full name is the control parameter (textbox, dropdown, etc.)
(#Customer_Name, #Order_Number, #username, #Warehouse_Id)
and the letter is the Querystring
(#C,#O,#W, #U)
<SelectParameters>
<asp:controlparameter ControlID="DropDownListCustomer" PropertyName="SelectedValue" Name="Customer_Name" />
<asp:querystringparameter QueryStringField="C" Name="C" />
<asp:controlparameter ControlID="DropDownListContract" PropertyName="SelectedValue" Name="Order_Number" />
<asp:querystringparameter QueryStringField="O" Name="O" />
<asp:controlparameter ControlID="UserID" PropertyName="Text" Name="username" />
<asp:querystringparameter QueryStringField="U" Name="U" />
<asp:controlparameter ControlID="DropDownListLocation" PropertyName="SelectedValue" Name="Warehouse_Id" />
<asp:querystringparameter QueryStringField="W" Name="W" />
</SelectParameters>
How can I add An OR Clause to default to the control if the QueryString is not provided or if the QueryString contains a value use it?

Related

Advance Searching concept using ASP.NET Web Form

Advance Searching concept using ASP.NET Web Form, I need like below URL type requirement.
https://www.naukri.com/top-company-jobs
I tried below SqlDataSource but when I fill all text boxes then only Its search sorting. I need if any of textbox type something then search should work. I do not know I have to write algorithm or any stored procedure.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CRM %>"
SelectCommand ="SELECT FirstName, LastName, Title, City, HomePhone
FROM Employees WHERE
(#FirstName IS NULL OR FirstName LIKE '%' + #FirstName + '%') AND
(#LastName IS NULL OR LastName LIKE '%' + #LastName + '%') AND
(#Title IS NULL OR Title LIKE '%' + #Title + '%') AND
(#City IS NULL OR City LIKE '%' + #City + '%');"
CancelSelectOnNullParameter="False"
>
<SelectParameters>
<asp:ControlParameter
ControlID="txtFirstName"
Name="FirstName"
Type="String" />
<asp:ControlParameter
ControlID="txtLastName"
Name="LastName"
Type="String" />
<asp:ControlParameter
ControlID="txtTitle"
Name="Title"
Type="String" />
<asp:ControlParameter
ControlID="txtCity"
Name="City"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Can I use the update statement with the select statement together in an SqlDataSource?

My query looks like this:
<asp:SqlDataSource ID="UpdateFullNameSQL" runat="server" ConnectionString="<%$ ConnectionStrings:UserQueries %>" ProviderName="<%$ ConnectionStrings:UserQueries.ProviderName %>"
UpdateCommand="update users set firstname = :changefirstname, lastname = :changelastname where username = :currentusername">
<UpdateParameters>
<asp:ControlParameter ControlID="ChangeFirstNameBox" Name="changefirstname" PropertyName="Text" Type="Empty" />
<asp:ControlParameter ControlID="ChangeLastNameBox" Name="changelastname" PropertyName="Text" Type="Empty" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="UsernameBox" Name="currentusername" PropertyName="Text" Type="Empty" />
</SelectParameters>
</asp:SqlDataSource>
So I have two parameters that I want to update from and one parameter that I want to use to change the data where it matches withthe selectparameter's data. And when I want to execute it, it shows an ORA-01008 exception.
My code-behind only has a updatefullnamesql.update(); function.
What am I missing here/doing wrong?
In update command you should use # instead of :. I.E.:
UpdateCommand="update users set firstname = #changefirstname,
lastname = #changelastname where username = #currentusername;"
Additionally you have to specify the #currentusername as update parameter
<UpdateParameters>
...
<asp:ControlParameter ControlID="UsernameBox"
Name="currentusername" PropertyName="Text" Type="Empty" />
</UpdateParameters>

SQL Datasource Nulling Update Parameter values?

I have a data source that has the update statement:
UpdateCommand="Update Apps Set AppDate = Convert(Datetime,#AppDate,103), Slot = #Slot,
Duration = #Duration, TPMId = #TPMId, AppTypeId = #AppTypeId,
AppStatusId = #AppStatusId, Location = #Location,
OtherLocation = #OtherLocation, Comments = #Comments
where AppId = #AppId;
update Profile Set PrimaryContact = #PrimaryContact,
ContactTel = #ContactTel, ContactMob = #ContactMob,
ContactEmail = #ContactEmail where CompanyID = #CompanyID"
and my update parameters are set as such:
<UpdateParameters>
<asp:Parameter Name="AppDate" />
<asp:Parameter Name="Slot" />
<asp:ControlParameter Name="Duration" ControlID="HFDuration" />
<asp:Parameter Name="TPMId" />
<asp:Parameter Name="AppTypeId" />
<asp:Parameter Name="AppStatusId" />
<asp:Parameter Name="Location" />
<asp:Parameter Name="OtherLocation" />
<asp:Parameter Name="Comments" />
<asp:QueryStringParameter Name="AppId" QueryStringField="AppID" />
<asp:Parameter Name="PrimaryContact" />
<asp:Parameter Name="ContactTel" />
<asp:Parameter Name="ContactMob" />
<asp:Parameter Name="ContactEmail" />
<asp:QueryStringParameter Name="CompanyID" QueryStringField="CompanyID" />
</UpdateParameters>
I have all the controls on the form view set for two way binding and are sitting with bind and not eval eg (<
asp:TextBox ID="LocationTB" runat="server" Text='<%# Bind("Location") %>'
TextMode="MultiLine"></asp:TextBox>
)
yet when it comes to running the update query the only values that actually get pulled through are the query string and control parameters the rest of the fields all get a null value.
I have had multiple form views with more than one update query before so I know this is not likely to be the issue.
Anyone have any suggestions as to why my datasource update is leaving my table full of nulls?

Getting error "Input string was not in a correct format." in SelectParameters

Query:
SELECT [VehicleId], [VehicleNumber], [VehicleBrand], [VehicleName], [ModelYear], [PurchasePrice]
FROM [VehicleMaster]
WHERE ([VehicleType]=#VehicleType or #VehicleType = '') and
([VehicleNumber]=#VehicleNumber or #VehicleNumber is null) and
([SaleStatus] = #SaleStatus or #SaleStatus = '-Select-') and
([OwnershipStatus]=#OwnershipDetail or #OwnershipDetail = '-Select-') and
([ModelYear] between #YearFrom and #YearTo or (#YearFrom = 'Year' and #YearTo = 'Year'))
Markup:
<SelectParameters>
<asp:SessionParameter Name="VehicleType" SessionField="VehicleType" Type="String" />
<asp:SessionParameter Name="VehicleNumber" SessionField="VehicleNumber" Type="String" />
<asp:SessionParameter Name="SaleStatus" SessionField="SalesStatus" Type="String" />
<asp:SessionParameter Name="OwnershipDetail" SessionField="OwnershipStatus" Type="String" />
<asp:SessionParameter Name="YearFrom" SessionField="YearFrom" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:SessionParameter Name="YearTo" SessionField="YearTo" Type="Int32" ConvertEmptyStringToNull="true" />
</SelectParameters>
Error:
Input string was not in a correct format.e....
(#YearFrom = 'Year' and #YearTo = 'Year')
would imply that the varaibles #YearFrom and #YearTo are strings, but you declare them as Int32
<asp:SessionParameter Name="YearFrom" SessionField="YearFrom" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:SessionParameter Name="YearTo" SessionField="YearTo" Type="Int32" ConvertEmptyStringToNull="true" />
Which will fail during execution since the string 'Year' isn't an int type.

SCOPE_IDENTITY returning wrong ID

I'm attempting to return the ID of the newly created row. I've tried doing it with SCOPE_IDENTITY, but it returns a 3. I've searched around and can't determine what I'm doing incorrect.
Here is the code:
<asp:SqlDataSource ID="sourceRecruit" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = #ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = #FirstName, [LastName]=#LastName, [Height]=#Height, [Weight]=#Weight, [FortyYardDash] = #FortyYardDash, [CombineVerified] = #CombineVerified, [HomeNumber] = #HomeNumber, [CellNumber] = #CellNumber, [Email] = #Email, [Position] = #Position, [Commitment] = #Commitment, [GraduationYear] = #GraduationYear, [LastUpdate] = #LastUpdate, [NetworkUpdate] = #NetworkUpdate, [DecisionDate] = #DecisionDate, [MiscContact] = #MiscContact, [RivalsID] = #RivalsID, [ScoutID] = #ScoutID, [ESPNID] = #ESPNID, [t247SportsID] = #t247SportsID, [PictureID] = #PictureID, [StarRating] = #StarRating, [NationalHot100Rank] = #NationalHot100Rank, [PositionRank] = #PositionRank, [JanuaryEnrollee] = #JanuaryEnrollee, [Recruiter] = #Recruiter, [RecruitedPosition] = #RecruitedPosition, [SummerCamp] = #SummerCamp, [JuniorDay] = #JuniorDay, [SpringGame] = #SpringGame, [UnofficialVisit] = #UnofficialVisit, [FootballGame] = #FootballGame, [OfficialVisitDate] = #OfficialVisitDate, [Status] = #Status, [Updates] = #Updates, [Favorites] = #Favorites, [Visits] = #Visits, [Odds] = #Odds WHERE [ID] = #ID"
InsertCommand="INSERT INTO Players ([FirstName], [LastName], [Height], [Weight], [FortyYardDash], [CombineVerified], [HomeNumber], [CellNumber], [Email], [Position], [Commitment], [GraduationYear], [LastUpdate], [NetworkUpdate], [DecisionDate], [MiscContact], [RivalsID], [ScoutID], [ESPNID], [t247SportsID], [PictureID], [StarRating], [NationalHot100Rank], [PositionRank], [JanuaryEnrollee], [Recruiter], [RecruitedPosition], [SummerCamp], [JuniorDay], [SpringGame], [UnofficialVisit], [FootballGame], [OfficialVisitDate], [Status], [Updates], [Favorites], [Visits], [Odds]) VALUES (#FirstName, #LastName, #Height, #Weight, #FortyYardDash, #CombineVerified, #HomeNumber, #CellNumber, #Email, #Position, #Commitment, #GraduationYear, #LastUpdate, #NetworkUpdate, #DecisionDate, #MiscContact, #RivalsID, #ScoutID, #ESPNID, #t247SportsID, #PictureID, #StarRating, #NationalHot100Rank, #PositionRank, #JanuaryEnrollee, #Recruiter, #RecruitedPosition, #SummerCamp, #JuniorDay, #SpringGame, #UnofficialVisit, #FootballGame, #OfficialVisitDate, #Status, #Updates, #Favorites, #Visits, #Odds); SET #ID=SCOPE_IDENTITY()"
OnInserted="reloadPage">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DecisionDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="NetworkUpdate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="OfficialVisitDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="LastUpdate" ConvertEmptyStringToNull="true" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" DefaultValue="Int32" Direction="Output" />
<asp:Parameter Name="DecisionDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="NetworkUpdate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="OfficialVisitDate" ConvertEmptyStringToNull="true" />
<asp:Parameter Name="LastUpdate" ConvertEmptyStringToNull="true" />
</InsertParameters>
</asp:SqlDataSource>
Here is my code behind:
protected void reloadPage(object sender, SqlDataSourceStatusEventArgs e)
{
object newid = e.Command.Parameters["#ID"].Value;
lblResults.Text = "Test "+Convert.ToString(newid);
}
Like I've mentioned, every time I've ran the code, lblResults displays a "3," but the ID is in the 3000 range.
You need to use the OnInserted event, and specify the output direction of the parameter:
<asp:Parameter Direction="Output" Name="ID" Type="Int32" />
Code-behind:
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
int id = Convert.ToInt32(e.Command.Parameter["#ID"].Value);
}
I also noticed that you set DefaultValue="Int32". You should change that to Type="Int32".
If you still have problems after applying the above changes, try changing SET #ID = SCOPE_IDENTITY to SELECT #ID = SCOPE_IDENTITY. Shouldn't make a difference, but if all else fails...
Did you try
SELECT #ID = SCOPE_IDENTITY()
instead of
SET #ID = SCOPE_IDENTITY()
that is what I have and works perfect.

Categories