Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
hi i am using datalist in asp.net page,that display data from SQL DB at design time.
datalist show those candidate who live in Punjab state,but now after that i want more sorting in that data, i have a dropdownlist that display punjab state city,when user select a sepecific city
datalist show only that city candidate.
 
plz help
 

till i am using this code
<div id="dr1" style="width: 300px; padding-bottom: 1px; height: 630px; overflow: scroll">
             <asp:UpdatePanel ID="UpdatePanel3" runat="server">
           <ContentTemplate>
                       <asp:DataList ID="DataList1" runat="server" CellPadding="4" RepeatLayout="Flow" DataSourceID="SqlDataSource1"
                           ForeColor="#333333" Width="300px" RepeatColumns="2" RepeatDirection="Horizontal">
                           <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
                           <FooterStyle Font-Bold="True" ForeColor="White" BackColor="#5D7B9D" />
                           <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#5D7B9D" />
                           <ItemStyle ForeColor="#333333" BackColor="#F7F6F3" />
                           <ItemTemplate>
                               <a class='iframe' href="my-business.aspx?nm='<%# Eval("Name") %>'">
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyArea %>"
    SelectCommand="Sp_State_candidate" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:QueryStringParameter Name="state" QueryStringField="id" Type="String" />
     </SelectParameters>
</asp:SqlDataSource>
Posted 19-Apr-13 21:15pm
Edited 19-Apr-13 21:17pm
v2
Comments
RedDK at 22-Apr-13 14:14pm
   
There is no query block in the above code. See the difference, as posted in Solution 01 or Solution 02. Solution 04 is essentially repost and etc ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
       DataTextField="city_name" DataValueField="city_id" AutoPostBack="true" BackColor="IndianRed"
       ForeColor="Snow">
   </asp:DropDownList>
   <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT City_Id, City_Name FROM City"
       ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"></asp:SqlDataSource>
 

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2">
        <ItemTemplate>
            <ul>
                <li>name:
                    <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></li>
                <li>category:
                    <asp:Label ID="categoryLabel" runat="server" Text='<%# Eval("category") %>' /></li>
 
                <li>City_Id:
                    <asp:Label ID="City_IdLabel" runat="server" Text='<%# Eval("City_Id") %>' /></li>
            </ul>
 
        </ItemTemplate>
    </asp:DataList>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" DataSourceMode="DataReader"
        SelectCommand="SELECT * FROM [Products] WHERE ([City_Id] = @City_Id)" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>">
        <SelectParameters>
            <asp:ControlParameter Name="City_Id" ControlID="DropDownList1" PropertyName="SelectedValue"
                DefaultValue="0" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
  Permalink  
v2
Comments
BALBINDER4u at 22-Apr-13 5:35am
   
both answer are wrong.
plz read question carefully?
 
on the page load datalist show all candidate and after button click show only those whose belong to particular city ?
 
Plz...................
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Collapse | Copy Code
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource2">
        <ItemTemplate>
            <ul>
                <li>name:
                    <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></li>
                <li>category:
                    <asp:Label ID="categoryLabel" runat="server" Text='<%# Eval("category") %>' /></li>
 
                <li>City_Id:
                    <asp:Label ID="City_IdLabel" runat="server" Text='<%# Eval("City_Id") %>' /></li>
            </ul>
 
        </ItemTemplate>
    </asp:DataList>
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" DataSourceMode="DataReader"
        SelectCommand="SELECT * FROM [Products] WHERE ([City_Id] = @City_Id)" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>">
        <SelectParameters>
            <asp:ControlParameter Name="City_Id" ControlID="DropDownList1" PropertyName="SelectedValue"
                DefaultValue="0" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
  Permalink  
Comments
BALBINDER4u at 22-Apr-13 5:35am
   
both answer are wrong .
Plz read question carefully.
 
on the page load datalist show all candidate and after that button click, datalist show only particular city candidate.
 
Plz >>>>>>>>>>>>>>>
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

u have to add 2 sql datasources one without where condition and the other with where condition. Then on dropdown selection changed u should change sqldatasource id then it works as u wish
 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" DataSourceMode="DataReader"
        SelectCommand="SELECT * FROM [Products]" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource3" runat="server" DataSourceMode="DataReader"
        SelectCommand="SELECT * FROM [Products] WHERE ([City_Id] = @City_Id)" ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>">
        <SelectParameters>
            <asp:ControlParameter Name="City_Id" ControlID="DropDownList1" PropertyName="SelectedValue"
                DefaultValue="0" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
-------------------------------------------
in .cs of dropdown chage
-------------------------------------------
protected void DropDownList1_OnSelectedIndexChanged(object sender, EventArgs e)
   {
       DataList1.DataSourceID = "SqlDataSource3";
   }
  Permalink  
Comments
BALBINDER4u at 23-Apr-13 0:40am
   
Plz check my answer
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I think this code work also !
 
Design Mode
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyArea %>"
                SelectCommand="Select id,name,address,mobile,office,catg_ID,claim_status,keyword from famous">
            </asp:SqlDataSource>
 

Code File
 
<pre lang="cs">protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
 
       try
       {
 
           string str = DropDownList1.SelectedItem.ToString();
           Session[&quot;tblnm&quot;] = str;
           SqlDataSource1.SelectCommand = &quot;myarea1&quot;;
           SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
           SqlDataSource1.SelectParameters.Add(&quot;tablename&quot;, TypeCode.String, str);
           str = &quot;&quot;;
 
       }</pre>
 

 
myarea1 is a  DB SP Name
 
<pre lang="sql">create procedure [dbo].[myarea1](@cityname varchar(250))
as
begin
EXEC(&#39;Select  id,name,address,mobile,office,catg_ID,claim_status,keyword from &#39;+@cityname)
end</pre>
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100