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
Related
I have two databases with the following schema:
tbl_users(userID, fullName, emailID, password)
userID is the Primary Key
tbl_savings(savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, ownerID, dataEnteredByID)
savingsID is the Primary Key.
ownerID is a Foreign Key on userID of tbl_users with a one-to-many relationship.
ownerID specifies the ID of the owner of the saving. Similarly, dataEnteredByID specifies the ID of the person who entered the data.
I have a GridView with the following specification:
<asp:GridView ID="GridViewSavingsTracker" AutoGenerateColumns="false" runat="server" DataKeyNames="savingsID" OnRowCommand="GridViewSavingsTracker_RowCommand" AllowPaging="true" OnSelectedIndexChanged="GridViewSavingsTracker_SelectedIndexChanged" OnPageIndexChanging="GridViewSavingsTracker_PageIndexChanging" PageSize="10">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="creditorName" HeaderText="Creditor Name" />
<asp:BoundField DataField="amount" HeaderText="Principal Amount" />
<asp:BoundField DataField="interestRate" HeaderText="Interest Rate" />
<asp:BoundField DataField="interestType" HeaderText="Interest Type" />
<asp:BoundField DataField="interestCalculationMethod" HeaderText="Interest Calculation Method" />
<asp:BoundField DataField="insertedDate" HeaderText="Date" />
<asp:BoundField DataField="ownerID" HeaderText="Owner" />
<asp:BoundField DataField="dataEnteredByID" HeaderText="Data Entered By" />
</Columns>
</asp:GridView>
What I want to do: Instead of printing the ownerID and dataEnteredByID, I want to print the fullNames of the corresponding IDs. How can I do that?
What I already have: I have the following server side code for GridView. I have a method ShowGridView() that populates the data into the GridView from the database(s).
protected void ShowGridView()
{
string connectionString = GetConnString();
OleDbConnection con = new OleDbConnection(connectionString);
con.Open();
string showGridViewQuery = "(SELECT s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName FROM tbl_savings s LEFT JOIN tbl_users u ON s.ownerID = u.usersID) UNION (select s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName from tbl_savings s LEFT JOIN tbl_users u ON s.dataEnteredByID = u.usersID)";
OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
showGridViewCommand.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
olda.Fill(dt);
GridViewSavingsTracker.DataSource = dt;
GridViewSavingsTracker.DataBind();
con.Close();
}
You can create a separate code-behind method that gets the full name and call it from the gridview.
Create an ItemTemplate for the field. The Text property calls the method. Replace UserID with your field's name.
<ItemTemplate>
<asp:Label ID="lblUserNameItem" runat="server"
Text='<%# GetUserNameByID(Eval("UserID").ToString()) %>'></asp:Label>
</ItemTemplate>
Code behind:
protected string GetUserNameByID(string userID)
{
// your data-code...
... uname = GetUserByID(int.Parse(userID));
return uname;
}
Do the same with the other id, with the same method or another.
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.
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;
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
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>