Passing a Guid to SQL code within Asp.Net - sql

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.

Related

Display ContactName from Guid in Gridview using SQLDataSource / Asp.Net

I have set the following code up to display player1 v player2 in a Gridview. However, It currently displays tblFixtures.player1 v tblFixtures.player2 which are UniqueIdentifiers, I need it to display their corresponding contact names instead. tblaccounts has columns accountID(uniqueidentifier) and contactName(varchar) but not sure how to join this in such a way that I can display them in the Gridview below.
<asp:SqlDataSource ID="DSFixtures" runat="server" ConnectionString="
<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT
tblFixtures.player1, tblFixtures.player2, tblFixtures.compID,
tblFixtures.round
FROM tblFixtures INNER JOIN tblCompetitions ON tblFixtures.compID =
tblCompetitions.compID WHERE tblFixtures.compID = #Event_ID and round =
#Round ">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="compID" Name="Event_ID" />
<asp:QueryStringParameter QueryStringField="round" Name="Round" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Gridview ID="gdvFixtures" visible="false" width="100%"
runat="server" AllowPaging="True" AutoGenerateColumns="False"
CssClass="mGrid" DataKeyNames="compID" DataSourceID="DSFixtures"
PageSize="20" AllowSorting="True">
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:BoundField DataField="player1" HeaderText="player1" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
V
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="player2" HeaderText="player2" />
</Columns>
</asp:GridView>
You can do a nested select
SELECT tblFixtures.player1, (SELECT name FROM tblaccounts WHERE
(accountID = tblFixtures.player1)) AS player1_name, ...
Or create another 2 joins, one for each player. You can do that by renaming the tables with AS so you can join the same table multiple times.
INNER JOIN tblaccounts AS acc1 ON acc1.accountID = tblFixtures.player1 INNER JOIN
tblaccounts AS acc2 ON acc1.accountID = tblFixtures.player2
UPDATE
Your entire query should look like this. And player1_name and player2_name become the column names you can use in a GridView.
SELECT
tblFixtures.player1,
(SELECT name FROM tblaccounts WHERE (accountID = tblFixtures.player1)) AS player1_name,
tblFixtures.player2,
(SELECT name FROM tblaccounts WHERE (accountID = tblFixtures.player2)) AS player2_name,
tblFixtures.compID,
tblFixtures.round
FROM tblFixtures INNER JOIN tblCompetitions ON tblFixtures.compID =
tblCompetitions.compID WHERE tblFixtures.compID = #Event_ID and round =
#Round

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

Changing SqlDataSource SelectCommand based on control parameters

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>

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.