Question : How to pass a scalar variable from one query to another

How to pass a scalar variable from one query to another ??

do I need another select statement to obtain = @EquipmentID"
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
<asp:SqlDataSource ID="indexpagedatasource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Index2PageDataSource %>" 
        
        SelectCommand="SELECT  Equipment.Year, Equipment.Make, Equipment.Model, Equipment.Type, Equipment.MilesHours, Equipment.VinSerial, Equipment.Description,
                      Equipment.Location, Equipment.Price, Equipment.SignUpDate, UserProfile.Name, UserProfile.Company, UserProfile.Phone, UserProfile.WebsiteURL,
                      UserProfile.Email, Equipment.EquipmentID
                   FROM Equipment INNER JOIN
                      UserProfile ON Equipment.UserId = UserProfile.UserId 
                   GROUP BY Equipment.Year, Equipment.Make, Equipment.Model, Equipment.Type, Equipment.MilesHours, Equipment.VinSerial, Equipment.Description,
                      Equipment.Location, Equipment.Price, Equipment.SignUpDate, UserProfile.Name, UserProfile.Company, UserProfile.Phone, UserProfile.WebsiteURL,
                      UserProfile.Email, Equipment.EquipmentID
                   ORDER BY Equipment.SignUpDate DESC" >  
    </asp:SqlDataSource>
    
    <asp:SqlDataSource ID="indexpagedatasource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Index2PageDataSource %>"  
        SelectCommand="SELECT CustomerImages.ImageID, CustomerImages.EquipmentID AS Expr1, CustomerImages.Title,
                      CustomerImages.ImageData, CustomerImages.MIMEType
                      FROM CustomerImages WHERE CustomerImages.EquipmentID = @EquipmentID">  
    </asp:SqlDataSource>

Answer : How to pass a scalar variable from one query to another

Another option if you want the ListView2 to update when selecting a row in ListView1 is coded below.

Notice the line
indexpagedatasource2.SelectParameters("EquipmentID").DefaultValue = ListView1.DataKeys(ListView1.SelectedIndex).Value.ToString()
inside the indexpagedatasource2_Selecting, which is triggered when a selection is made in the listview by using OnSelectedIndexChanged.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
<%@ Page Language="VB" MasterPageFile="~/Nested Master Pages/annonymous.master" Title="Untitled Page" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
    
    'Dim currentUser As MembershipUser
    Public Sub Page_Load()
        'currentUser = Membership.GetUser()
    End Sub
    
    Protected Sub indexpagedatasource2_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs)
     
        ' Get a reference to the currently logged on user
        Dim currentUser As MembershipUser = Membership.GetUser()
        ' Determine the currently logged on user's UserId value

        Dim currentUserId As Guid = CType(currentUser.ProviderUserKey, Guid)
        
        ' Assign the currently logged on user's UserId to the @UserId parameter
        e.Command.Parameters("EquipmentID").Value = currentUserId.ToString()  
        
        indexpagedatasource2.SelectParameters("EquipmentID").DefaultValue = ListView1.DataKeys(ListView1.SelectedIndex).Value.ToString()
    End Sub
    
    Public Function ShowImg(ByVal obj As Object) As Boolean
        If IsDBNull(obj) Then
            ' Do the processing here...
            ShowImg = False
        Else
            ShowImg = True
        End If
        
    End Function
    
    'Dim currentUser As MembershipUser

    'Public Sub Page_Load()
    'currentUser = Membership.GetUser()
    'End Sub
 

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


    <asp:SqlDataSource ID="indexpagedatasource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Index2PageDataSource %>" 
        
        SelectCommand="SELECT  Equipment.Year, Equipment.Make, Equipment.Model, Equipment.Type, Equipment.MilesHours, Equipment.VinSerial, Equipment.Description,
                      Equipment.Location, Equipment.Price, Equipment.SignUpDate, UserProfile.Name, UserProfile.Company, UserProfile.Phone, UserProfile.WebsiteURL,
                      UserProfile.Email, Equipment.EquipmentID
                   FROM Equipment INNER JOIN
                      UserProfile ON Equipment.UserId = UserProfile.UserId 
                   GROUP BY Equipment.UserId, Equipment.Year, Equipment.Make, Equipment.Model, Equipment.Type, Equipment.MilesHours, Equipment.VinSerial, Equipment.Description,
                      Equipment.Location, Equipment.Price, Equipment.SignUpDate, UserProfile.Name, UserProfile.Company, UserProfile.Phone, UserProfile.WebsiteURL,
                      UserProfile.Email, Equipment.EquipmentID
                   ORDER BY Equipment.SignUpDate DESC" >  
    </asp:SqlDataSource>
    

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="EquipmentID" DataSourceID="indexpagedatasource" OnSelectedIndexChanged="indexpagedatasource2_Selecting">
           <LayoutTemplate>
                <asp:Placeholder runat="server" ID="itemPlaceholder"  ></asp:Placeholder>                           
                <br />
                <br />                                    
           </LayoutTemplate>
        
        <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("EquipmentID") %>' Font-Bold="True"></asp:Label>
            <asp:Label ID="YearLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("Year") %>' Font-Bold="True"></asp:Label>
            <asp:Label ID="MakeLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("Make") %>' Font-Bold="True"></asp:Label>
            <asp:Label ID="ModelLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("Model") %>' Font-Bold="True"></asp:Label>
            <asp:Label ID="TypeLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("Type") %>' Font-Bold="True"></asp:Label>
            &nbsp;-
            <span><asp:Label ID="PriceLabel" runat="server" 
                Font-Names="Arial" Font-Size="Small" 
                Text='<%# Eval("Price") %>' Font-Bold="True"></asp:Label>
            </span>
            <br />
            <span style="font-family:Arial; font-size:small">Miles / Hours:</span>
            <asp:Label ID="MilesHoursLabel" runat="server" Font-Names="Arial" 
                Font-Size="Small" Text='<%# Eval("MilesHours") %>'></asp:Label>
            <br />
            <span style="font-family:Arial; font-size:small">VIN / Serial No:</span>
            <asp:Label ID="VinSerialLabel" runat="server" Font-Names="Arial" 
                Font-Size="Small" Text='<%# Eval("VinSerial") %>'></asp:Label>
            <br />
            <span style="font-family:Arial; font-size:small">Description:</span>&nbsp;
            <asp:Label ID="DescriptionLabel" runat="server" Font-Names="Arial" 
                Font-Size="Small" Text='<%# Eval("Description") %>'></asp:Label>
            <br />
            <span style="font-family:Arial; font-size:small">Location:</span>&nbsp
            <asp:Label ID="LocationLabel" runat="server" Font-Names="Arial" 
                Font-Size="Small" Text='<%# Eval("Location") %>'></asp:Label>
            <br />
            <span style="font-family:Arial; font-size:small;font-weight:bold">For more info contact:</span> 
            <asp:Label ID="NameLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Font-Bold="True" Text='<%# Eval("Name") %>'></asp:Label>
               &nbsp;-
               <asp:Label ID="PhoneLabel" runat="server" Font-Names="Arial" Font-Size="Small" 
                Font-Bold="True" Text='<%# Eval("Phone") %>'></asp:Label>
                <br />
                <asp:HyperLink ID="Email" runat="server" Font-Names="Arial" 
                Font-Size="Small" NavigateURL='<%# Eval("Email") %>' 
                Text='<%# Eval("Email") %>'></asp:HyperLink>
                    <br />
                <asp:HyperLink ID="WebsiteURL" runat="server" Font-Names="Arial" 
                    Font-Size="Small" NavigateURL='<%# Eval("WebsiteURL") %>' 
                Text='<%# Eval("WebsiteURL") %>'></asp:HyperLink>
                    <br />
                    <asp:Label ID="SignUpDate" runat="server" Font-Names="arial" 
                Font-Size="Smaller" Text='<%# Eval("SignUpDate", "{0:d}") %>'></asp:Label>
                    <br />    
	    </asp:SqlDataSource>         
        </ItemTemplate>  
        <ItemSeparatorTemplate>
        <hr />
        <br />
        </ItemSeparatorTemplate>   
    </asp:ListView>
    
    
    <asp:ListView ID="ListView2" runat="server" DataKeyNames="EquipmentID" DataSourceID="indexpagedatasource2">
	   <LayoutTemplate>           
		<asp:Placeholder runat="server" ID="itemPlaceholder"  ></asp:Placeholder>                           
		<br />
		<br />                                    
	   </LayoutTemplate>
	<ItemTemplate> 
	<asp:Label ID="Label1" runat="server" Font-Names="Arial" Font-Size="Small" 
		Text='<%# Eval("EquipmentID") %>' Font-Bold="True"></asp:Label>

		 <asp:Label ID="Label2" runat="server" Font-Names="Arial" Font-Size="Small" 
		Text='<%# Eval("Title") %>' Font-Bold="True"></asp:Label>     

	       <asp:Image ID="ImageData1" runat="server" Height="100px" 
		ImageUrl='<%# Eval("ImageData") %>'  />   
	</ItemTemplate>  
	<ItemSeparatorTemplate>
	<hr />
	<br />
	</ItemSeparatorTemplate>   
     </asp:ListView>  
      <asp:SqlDataSource ID="indexpagedatasource2" runat="server" 
	ConnectionString="<%$ ConnectionStrings:Index2PageDataSource %>"  
	SelectCommand="SELECT CustomerImages.ImageID, CustomerImages.EquipmentID AS Expr1, CustomerImages.Title,
		      CustomerImages.ImageData, CustomerImages.MIMEType
		      FROM CustomerImages WHERE CustomerImages.EquipmentID = @EquipmentID">  
      <SelectParameters>
		    <asp:Parameter Name="EquipmentID" Type="Int32" />
      </SelectParameters>
    
     <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" 
        PageSize="5">
        <Fields>
            <asp:NextPreviousPagerField PreviousPageText="&lt;" ShowFirstPageButton="true"
                ShowNextPageButton="False" />
            <asp:NumericPagerField />
            <asp:NextPreviousPagerField NextPageText="&gt;" ShowLastPageButton="True" 
                ShowPreviousPageButton="False" />
        </Fields>
      </asp:DataPager>
</asp:Content>
Random Solutions  
 
programming4us programming4us