473,434 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

DataList / PagedDataSource / DataTable - How to allow for filter Control Parameter?

Prior to adding paging using PagedDataSource I had dropdownlist and search box filters for my DataList. The guts of the code in aspx page for those parameters is below:

Expand|Select|Wrap|Line Numbers
  1.     <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  2.         ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  3. SelectCommand="SELECT [ProductCode], [ImgUrl], [ImageField], [Featuring], [ProdCategoryID], [Title] FROM [TableSG1] WHERE (([ProdCategoryID] = @ProdCategoryID) AND ([Featuring] LIKE '%' + @Featuring + '%')) ORDER BY [Featuring]">
  4.  
  5.         <SelectParameters>
  6.             <asp:ControlParameter ControlID="DropDownList1" Name="ProdCategoryID" 
  7.                 PropertyName="SelectedValue" Type="String" />
  8.  
  9.             <asp:ControlParameter ControlID="TextBox1" Name="Featuring" PropertyName="Text" 
  10.                 Type="String" />
  11.  
When creating the pager using PagedDataSource I did a tutorial which did not have parameters and all the code went in the code behind page. The guts of it is below:

Expand|Select|Wrap|Line Numbers
  1.      private void BindGrid()
  2.      {
  3.          string sql = "SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] DESC";
  4.          SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=xxxxx;Integrated Security=False;");
  5.          DataTable dt = new DataTable();
  6.          da.Fill(dt);
  7.  
  8.          pds.DataSource = dt.DefaultView;
  9.          pds.AllowPaging = true;
  10.          pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
  11.          pds.CurrentPageIndex = CurrentPage;
  12.          lnkbtnNext.Enabled = !pds.IsLastPage;
  13.          lnkbtnPrevious.Enabled = !pds.IsFirstPage;
  14.  
  15.          DataList1.DataSource = pds;
  16.          DataList1.DataBind();
  17.  
  18.          doPaging();
  19. }

The pager is great but now I need to add my filters. The page wont load if I add the "SelectParameters" code (above) to my aspx page and if I simply add the "WHERE" clause to the "SELECT" clause in the code behind I get an error stating that I need to add a scalar variable.

I have found some examples through googling but nothing specific enough.

I need assistance if anyone would be kind enough to help.
Apr 18 '10 #1
5 4059
CroCrew
564 Expert 512MB
You have posted your question in the asp (Classic ASP) section rather than the asp.net forum section.

I have moved it across for you.

CroCrew~
Apr 19 '10 #2
Frinavale
9,735 Expert Mod 8TB
I'm not sure why you're having problems with the where clause.
What is the error message exactly?
Apr 21 '10 #3
Hi Frinavale,

The error message is "must declare scaler variable @ProdCategoryID"
I have made some progress in getting as far as the following: NB I have shortened the SELECT clause to include only one parameter.

Expand|Select|Wrap|Line Numbers
  1.          string ddl = DropDownList1.SelectedItem.Value;
  2.          SqlConnection conn = new SqlConnection("Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=bootsie23000;Integrated Security=False;");
  3.  
  4.          SqlCommand cmd = new SqlCommand ("SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] WHERE [ProdCategoryID] = @ProdCategoryID");
  5.          SqlDataAdapter da = new SqlDataAdapter ("SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] WHERE ([ProdCategoryID] = @ProdCategoryID", conn );
  6.          cmd.Parameters.AddWithValue("@ProdCategoryID", "ddl");
  7.          DataTable dt = new DataTable();
  8.          da.Fill(dt);
ProdCategoryID is the name of drop down list as seen in the aspx page control parameter code as follows:

Expand|Select|Wrap|Line Numbers
  1.   <SelectParameters>
  2.                  <asp:ControlParameter ControlID="DropDownList1" Name="ProdCategoryID" 
  3.                 PropertyName="SelectedValue" Type="String" />
DropDownList1 is unbound - code from aspx page is below :

Expand|Select|Wrap|Line Numbers
  1.  <asp:DropDownList ID="DropDownList1" runat="server" 
  2.             Height="28px" Width="176px" Font-Bold="True" 
  3.             Font-Size="Medium"  AutoPostBack="True">
  4.             <asp:ListItem>Giant Posters</asp:ListItem>
  5.             <asp:ListItem>Extra Long Posters</asp:ListItem>
  6.             <asp:ListItem>Regular Posters</asp:ListItem>
  7.             <asp:ListItem>Slim Posters</asp:ListItem>
  8.             <asp:ListItem>Small Posters</asp:ListItem>
  9.             <asp:ListItem>Lenticular Posters</asp:ListItem>
  10.             <asp:ListItem>Stickers</asp:ListItem>
  11.             <asp:ListItem>Key Rings</asp:ListItem>
  12.             <asp:ListItem>Calendars</asp:ListItem>
  13.         </asp:DropDownList>

I have probably got a bit to far ahead of myself for a newbie. The filter worked when using VWD 2008 Express wysiwag without the pager. But in following the pager tutorial I stuck with its format which did not include a WHERE clause in the SELECT clause. I was hoping to workout how to add them back in afterwards and that is where I am. The pager tutorial took me away from my VB training ( which I am only a beginner of ) and into C#. I have been googling like mad for advice on scalar variables and thought I had done it with the "string ddl = DropDownList1.SelectedItem.Value;" but I am still getting the same error.

Any help is very much appreciated.
Apr 21 '10 #4
Expand|Select|Wrap|Line Numbers
  1. SqlConnection conn = new SqlConnection("Data Source=tcp:s02.winhost.com;Initial Catalog=DB_5890_maindb1;User ID=DB_5890_maindb1_user;Password=bootsie23000;Integrated Security=False;");
  2.          SqlCommand cmd = new SqlCommand("SELECT [ProductCode], [ImgUrl], [Featuring], [Title] FROM [TableSG1] WHERE [ProdCategoryID] = @ProdCategoryID ORDER BY [Featuring]", conn);
  3.  
  4.          SqlParameter param = new SqlParameter();
  5.          param.ParameterName = "@ProdCategoryID";
  6.  
  7.          param.Value = DropDownList1.SelectedItem.Value;
  8.          cmd.Parameters.Add(param);
  9.  
  10.          SqlDataAdapter da = new SqlDataAdapter(cmd);
  11.          DataTable dt = new DataTable();
  12.          da.Fill(dt);
Apr 25 '10 #5
Frinavale
9,735 Expert Mod 8TB
Hmm interesting.
I've never used the AddWithValue method before.

I was about to recommend that you use the add method instead but it looks like you figured it out before I got the chance.


Thanks for sharing your solution!

-Frinny
Apr 25 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Umut K. | last post by:
Hi all, I've a DataList control and it's DataBound by a SqlDataReader... The Reader returns say 3 records and as expected the datalist shows 3 rows. What i want to do is to add another extra...
4
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a...
2
by: wh1974 | last post by:
I have a DataTable that I'm binding to a DataList control on my web page. I want to be able to restrict the number of rows that are initially displayed from the DataTable. For example I want to be...
10
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
4
by: Miguel Dias Moura | last post by:
Hello, I created a datalist in an ASP.Net / VB page. I display the image and price of a few products. When a user clicks an image I want to load the page "detail.aspx?number=id" and send the...
1
by: Mike | last post by:
Hi! Been stuck on this one for a bit. Would really appreciate any help on this one. To start. I have a sql database table with the following data and design (ex) ...
1
by: Patrick.O.Ige | last post by:
I'm trying to page a Datalist below using VS.NET But i get the error:- System.NullReferenceException: Object reference not set to an instance of an object. At line:- btnPrev.Visible = (Not...
2
by: David | last post by:
Hello. How can I enable and manage paging capability on DataList control like DataGrid? Thank you.
0
by: kapamaroy | last post by:
Hi, I am working on a website and I have to reperesent the contents of a List<> in several pages. I use a PagedDataSource item and a DataList control for that. What I want to do is to create a links...
2
by: sureshl | last post by:
The below workss very fine , in the type for previous, next concept for paging, i need to do with page number Like this, 1 2 3 4......23 (http://bytes.com/topic/visual-basic-net/) Like this...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.