Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETdrop-down
i have a drop down which source code is
<asp:DropDownList ID="DropDownList1" runat="server"
                   onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                   AutoPostBack="True">
                   <asp:ListItem> --Select--</asp:ListItem>
                   <asp:ListItem> ClientName</asp:ListItem>
                   <asp:ListItem> DeliveryDate</asp:ListItem>
                   <asp:ListItem> ExtendDate</asp:ListItem>
 
we select any item it run successfully.but when we select --select-- then it gives error of Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
          cmd = new SqlCommand(&quot;selectDataByDDLValue&quot; + DropDownList1.SelectedItem.Value.ToString(), con);
          rd = cmd.ExecuteReader();//Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
          GridView2.DataSource = rd;
          GridView2.DataBind();
          rd.Close();
 

procedure is
 

ALTER PROCEDURE [dbo].[selectDataByDDLValue](@ddlValue varchar(50))
AS
IF @ddlValue = &#39;--select--&#39;
BEGIN
select distinct Fname,Lname from Client_registration_tbl
END
ELSE
BEGIN
IF @ddlValue = &#39;Client Name&#39;
BEGIN
select distinct Fname,Lname from Client_registration_tbl
END
ELSE
BEGIN
  IF @ddlValue = &#39;Delivery Date&#39;
  BEGIN
  select a.Fname,a.Lname, b.Product_name,b.Product_quan,b.Delivery_date  from Client_registration_tbl a , Product_order  b where a.Uname= b.Uname
  END
  ELSE
  BEGIN
  select a.Fname,a.Lname, b.Product_name,b.Product_quan,b.Delivery_date,b.Extend_date  from Client_registration_tbl a , Product_order  b where a.Uname= b.Uname
  END
  END
END
Posted 4 Oct '12 - 21:36
Edited 5 Oct '12 - 2:25


7 solutions

As I can see your question, you need to modify a couple of things here:
1. Change in aspx:
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
   onselectedindexchanged="DropDownList1_SelectedIndexChanged"
   AutoPostBack="True">
   <asp:listitem value=""> --Select--</asp:listitem>
   <asp:listitem value="ClientName"> ClientName</asp:listitem>
   <asp:listitem value="DeliveryDate"> DeliveryDate</asp:listitem>
   <asp:listitem value="ExtendDate"> ExtendDate</asp:listitem>
</asp:dropdownlist>
Always try to add a value with the ListItems, so that while validating you won't get any problem.
2. Change in CS:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
      if(DropDownList1.SeletedValue != ""){
          cmd = new SqlCommand("selectDataByDDLValue", con);
          cmd.CommandType = CommandType.StoredProcedure; // Must declare the command type
          // Add the parameter here.
          cmd.Parameters.AddWithValue("@ddlValue", DropDownList1.SelectedValue);
          SqlDataAdapter sda = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          sda.Fill(ds);
          GridView2.DataSource = ds.Tables[0];
          GridView2.DataBind();
          sda.Dispose(); // Releasing the resource of SqlDataAdapter
          cmd.Dispose(); // Releasing the resource of SqlCommand
          con.Close(); //Closing the connection
      }
}
 

All the best.
--Amit
  Permalink  
hi, Offcourse --select-- is not a proper filter value and it should give error you have to put a check before calling the DB operations
 
 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(DropDownList1.SelectedItem.Value.ToString()))
        {
            cmd = new SqlCommand("selectDataByDDLValue" + DropDownList1.SelectedItem.Value.ToString(), con);
            rd = cmd.ExecuteReader();//Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
            GridView2.DataSource = rd;
            GridView2.DataBind();
            rd.Close();
        }
    }
  Permalink  
Comments
Rashid Choudhary - 5 Oct '12 - 3:55
not working same prob occured
tanweer akhtar - 5 Oct '12 - 4:57
put your code that bind the dropdownlist here, it will guide to resolve the issue
check with if condition if the selected item is SELECT or selected index is zero than dont execute your code.
  Permalink  
Dear Rashid,
 
Before going to check in the database you can check for the value.
 
if(DropDownList1.selectedIndex!=0)
{
    cmd = new SqlCommand(&quot;selectDataByDDLValue&quot; + DropDownList1.SelectedItem.Value.ToString(), con);
          rd = cmd.ExecuteReader();//Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
          GridView2.DataSource = rd;
          GridView2.DataBind();
          rd.Close();
}
 
Please check it and make it resolved if it helps.
 
Thanks and Regards
Suman Zalodiya
  Permalink  
Check first following condition..
if (drpdwnlist1.SelectedIndex > 0)
              {
                  string selval = drpdwnlist1.SelectedValue;
  cmd=new sqlcommand();
}
 

  Permalink  
When you pass the selected value of your dropdown list is "--select--"
then it will give you error:
Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
Because in value "--selecte--" there is two dash(--) before "select" word and in SQl server two dash(--) works like a comment. So your parameter becomes comment and no value is pass to the procedure.
 
For your solution remove the dash(-) from "--select--" word in drop down list.
  Permalink  
Hi,
 
please find the code solution below,
 
<asp:DropDownList ID="DropDownList1" runat="server"
                   onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                   AutoPostBack="True">
                   <asp:ListItem Value="-1"> --Select--</asp:ListItem>
                   <asp:ListItem Value="ClientName"> ClientName</asp:ListItem>
                   <asp:ListItem Value="DeliveryDate"> DeliveryDate</asp:ListItem>
                   <asp:ListItem Value="ExtendDate"> ExtendDate</asp:ListItem>
 

 
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
      {
if(DropDownList1.SelectedItem.Value.ToString() != "-1")
            {
          cmd = new SqlCommand(&quot;selectDataByDDLValue&quot; + DropDownList1.SelectedItem.Value.ToString(), con);
          rd = cmd.ExecuteReader();//Procedure or function 'selectDataByDDLValue' expects parameter '@ddlValue', which was not supplied.
          GridView2.DataSource = rd;
          GridView2.DataBind();
          rd.Close();
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 425
1 OriginalGriff 315
2 Slacker007 240
3 Aarti Meswania 210
4 Maciej Los 200
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,036
4 Maciej Los 2,488


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid