Click here to Skip to main content
12,239,805 members (53,007 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET drop-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 22:36pm
Edited 5-Oct-12 3:25am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:55am
   
not working same prob occured
tanweer akhtar 5-Oct-12 4:57am
   
put your code that bind the dropdownlist here, it will guide to resolve the issue
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

check with if condition if the selected item is SELECT or selected index is zero than dont execute your code.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Check first following condition..
if (drpdwnlist1.SelectedIndex > 0)
              {
                  string selval = drpdwnlist1.SelectedValue;
  cmd=new sqlcommand();
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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">
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  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2016
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