Changing SqlDataSource SelectCommand based on control parameters - sql

I'm working on a web app using vb.net and SQL Server 2008 R2
In one of the aspx file, I have this:
<asp:SqlDataSource ID="dsParentUnit" runat="server"
ConnectionString="<%$ ConnectionStrings:CN %>"
SelectCommand="SELECT * FROM [t_Unit] WHERE ([UnitTypeID] = (#UnitTypeID) - 1) AND NOT [UnitTypeID] = 1 ORDER BY UnitName ASC">
<SelectParameters>
<asp:ControlParameter ControlID="cboUnitType" Name="UnitTypeID" PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
in which the datasource dsParentUnit will change based on selected value of a dropdown list cboUnitType.
The datasource sdParentUnit is used for another dropwdown list item.
Now, I want to change the WHERE ([UnitTypeID] = (#UnitTypeID) - 1 so that it becomes something like this pseudo code:
If cboUnitType.SelectedValue <= 5 Then
WHERE ([UnitTypeID] = (#UnitTypeID) - 1
Else
WHERE ([UnitTypeID] = (#UnitTypeID) - 3
Can I do that?
And if possible, I would like to avoid building any stored function in the database, since the required paperwork for modifying database is really long...
Of course, only if it possible.
Thank you :)

Try this:
<asp:SqlDataSource ID="dsParentUnit" runat="server"
ConnectionString="<%$ ConnectionStrings:CN %>"
SelectCommand="SELECT * FROM [t_Unit] WHERE ([UnitTypeID] = (#UnitTypeID) -
<%IIF (cboUnitType.SelectedValue <= 5, "1", "3")%>
) AND NOT [UnitTypeID] = 1 ORDER BY UnitName ASC">
<SelectParameters>
<asp:ControlParameter ControlID="cboUnitType" Name="UnitTypeID" PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>

Related

Procedure has no parameters and arguments were supplied (literally no parameters)

I've been getting errors with this stored procedure so I tried simplifying it right down. For some reason I'm still getting an error saying the SP is expecting a parameter when I'm not using any parameters at all.
I should note that I'm trying to get this working via a Telerik RadDataform in ASP. Could it be that Telerik is adding a sneaky parameter here?
Here's the SP:
ALTER procedure [dbo].[NewEmployee_Update]
as
insert
NewEmployeeDetails
select top 1
NE.FormHeaderID,
NE.VersionNumber + 1,
getdate(), -- AddDate
'vibribbon',
'Bart',
'Simpson',
'El Barto',
'Rascal'
from
OnlineFormHeader H
inner join NewEmployeeDetails NE on NE.FormHeaderID = H.RecID
where
H.RecID = 3
order by
NE.VersionNumber desc
And the ASP DataSource:
<asp:SqlDataSource ID="dsEmployeeDetails" runat="server" ConnectionString="<%$ ConnectionStrings:OnlineStaffFormsConnectionString %>" InsertCommand="NewEmployee_Insert"
InsertCommandType="StoredProcedure" UpdateCommand="NewEmployee_Update" UpdateCommandType="StoredProcedure" SelectCommand="select top 1
NE.*
from
OnlineFormHeader H
inner join NewEmployeeDetails NE on NE.FormHeaderID = H.RecID
where
H.RecID = #formHeaderID
order by
NE.VersionNumber desc">
<SelectParameters>
<asp:QueryStringParameter Name="formHeaderID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:SessionParameter Name="currentUser" SessionField="currentUser" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="PreferredName" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</asp:Content>
It seems the Telerik DataForm automatically adds the unique id of the record to be updated to the parameter set.
Probably more noticeable here because I'm fudging the "update" command to insert instead.

Passing a Guid to SQL code within Asp.Net

This SQL code is in the Asp.Net code itself so how can I set the WHERE to equal the Guid which I have passed over in the requestQuery String?
E.g I want to put something like WHERE tblEntrants.compID = Request.QueryString("Comp") but obviously I can't. The Guid in the QueryString need passing in to this SQL query somehow. I'm fairly new to all this so if anyone can help I would much appreciate it.
<asp:SqlDataSource ID="DSEntrants" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT tblEntrants.accountID, tblEntrants.compID, tblaccounts.accountID, tblaccounts.contactName, tblEntrants.paid
FROM tblAccounts INNER JOIN
tblEntrants ON tblAccounts.accountID = tblEntrants.accountID
WHERE tblEntrants.compID = ?????????????????
ORDER BY tblEntrants.accountID DESC"></asp:SqlDataSource>
I have just worked out how to do this. You can directly set a querystring parameter like this..
<asp:SqlDataSource ID="DSEntrants" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT tblEntrants.accountID, tblEntrants.compID, tblaccounts.accountID, tblaccounts.contactName, tblaccounts.skypeUserName, tblEntrants.paid
FROM tblAccounts INNER JOIN
tblEntrants ON tblAccounts.accountID = tblEntrants.accountID
WHERE tblEntrants.compID = #Event_ID
ORDER BY tblEntrants.accountID DESC">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="compID" Name="Event_ID" />
</SelectParameters>
All works fine, thanks for reading.

Use a selected item from a dropdownlist in a SELECT query

I have following code which is a drop down list of flight locations:
Where to:<br />
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1"
DataTextField="Destination"
DataValueField="Destination" Width="222px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Destination] FROM [tblFlight]">
</asp:SqlDataSource>
I want to be able to use the selected value from the drop down list above (one of them is Corfu) as the WHERE condition of a query that will only select the flights going to Corfu.
Any suggestions?
basically:
string dest = yourDropDown.SelectedItem;
SELECT [Destination] FROM [tblFlight] WHERE [Destination] = " + dest;

How to call User.Identity.GetUserId() inside a SQL statement?

Language: ASP.NET (VB)
I have the following code:
<asp:SqlDataSource ...
SelectCommand="SELECT * FROM cust WHERE cust_id='C001';">
Now I want to modify it to become something like the following pseudocode:
<asp:SqlDataSource ...
SelectCommand="SELECT * FROM cust WHERE cust_id='" & User.Identity.GetUserId() & "';">
where cust_id is a NVARCHAR(128), equivalent to the user ID data type in the AspNetUsers SQL Server Database.
Obviously it is wrong in syntax because it is not correct in syntax. How can I get the above idea to work?
Thank you.
Change your markup to:
<%# Page Language="VB" AutoEventWireup="true" ... %>
<asp:SqlDataSource ConnectionString="YourConnStr" ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM cust WHERE cust_id = #CustId">
<SelectParameters>
<asp:Parameter Name="CustId" DbType="String" />
</SelectParameters>
</asp:SqlDataSource>
Then in your Page_Load in code behind:
Sub Page_Load()
SqlDataSource1.SelectParameters("CustId").DefaultValue = User.Identity.GetUserId()
End Sub

delete from gridview using stored procedure

Hi i have a gridview, linked to a sqldatasource. I have added a stored procedure to delete from multiple tables, and then enabled deleting on the gridviews smart tag.
When i click the delete button i get an error message, "Object must implement IConvertible". I read that it is a problem passing the parameter to the stored procedure, possibly the wrong datatype being passed. Im not sure if i am passing the parameter to the stored procedure at all. The parameter should be the gridviews datakeyname, in this case it is "UserId".
The stored procedure works i fine in management studio, so i think it is just the parameter being passed (or possibly not being passed)
Do i have to code the parameter in the code behind to be passed to the stored procedure?
<asp:SqlDataSource ID="selectUsers" runat="server" ConnectionString="<%$ ConnectionStrings:CASSFConnectionString %>"
SelectCommand="SELECT aspnet_Membership.UserId, aspnet_Membership.IsLockedOut, aspnet_Membership.Email, aspnet_Membership.CreateDate, aspnet_Membership.LastLoginDate, aspnet_Membership.LastPasswordChangedDate, aspnet_Profile.PropertyValuesString, aspnet_Users.UserName, aspnet_Membership.IsApproved FROM aspnet_Membership INNER JOIN aspnet_Profile ON aspnet_Membership.UserId = aspnet_Profile.UserId INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId AND aspnet_Profile.UserId = aspnet_Users.UserId WHERE (CONVERT (nvarchar(256), aspnet_Profile.PropertyValuesString) = #district)"
DeleteCommand="DeleteUsers" DeleteCommandType="StoredProcedure" UpdateCommand="UPDATE [aspnet_Membership] SET [IsApproved] = #IsApproved, [Email] = #email, [IsLockedOut] = #IsLockedOut WHERE [aspnet_Membership].[UserId] = #UserID">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="district" PropertyName="SelectedValue" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="UserId" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="IsApproved" />
<asp:Parameter Name="email" />
<asp:Parameter Name="IsLockedOut" />
<asp:Parameter Name="UserID" />
</UpdateParameters>
</asp:SqlDataSource>
Found the answer, i had just to remove Type="String" from the delete parameter.