Click here to Skip to main content
15,890,357 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have a drop down which source code is
ASP.NET
<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.

C#
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


SQL
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
Updated 5-Oct-12 2:25am
v3

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

C#
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();
       }
   }
 
Share this answer
 
Comments
Rashid Choudhary 5-Oct-12 3:55am    
not working same prob occured
tanweer 5-Oct-12 4:57am    
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.
 
Share this answer
 
Dear Rashid,

Before going to check in the database you can check for the value.

C#
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
 
Share this answer
 
Check first following condition..
C#
if (drpdwnlist1.SelectedIndex > 0)
              {
                  string selval = drpdwnlist1.SelectedValue;
  cmd=new sqlcommand();
}
 
Share this answer
 
When you pass the selected value of your dropdown list is "--select--"
then it will give you error:
SQL
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.
 
Share this answer
 
v2
Hi,

please find the code solution below,

XML
<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>




C#
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();

}
 
Share this answer
 
As I can see your question, you need to modify a couple of things here:
1. Change in aspx:
ASP.NET
<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:
C#
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900