Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have written the following code in asp.net c# web form:

ASP.NET
<asp:DropDownList ID="ddltype" runat="server" CssClass="dropspace" Style="width: 95%; float: left">
                                            <asp:ListItem >SELECT YOUR GADGET</asp:ListItem>
                                            <asp:ListItem >Laptop</asp:ListItem>
                                            <asp:ListItem >Desktop</asp:ListItem>
                                            <asp:ListItem >Printer</asp:ListItem>
                                        </asp:DropDownList>


the problem is that when i click on submit button without selecting any item from dropdown list it insert "SELECT YOUR GADGET" into the sql database. please help me out from this problem. please write the c# code so that it can not insert first list item value to the data base.

What I have tried:

C#
if(ddltype.selectedValue !="")
{ cmd = new SqlCommand("sp_submitquery", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@gadget", SqlDbType.VarChar).Value = ddltype.SelectedValue;
            

            cmd.ExecuteNonQuery();
          

              ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Your Query Submitted Sucessfully');", true);
}



 if (ddltype.SelectedValue == "")
        {
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Please select your gadget');", true);
        }
Posted
Updated 6-May-16 0:49am
v2
Comments
Sergey Alexandrovich Kryukov 30-Apr-16 20:56pm    
One way to normalize the situation is to enforce selection of an item in all list boxes. If a user did not select anything, it should be your "select your gadget" element. Isn't that simple?
In server side, handle this selection and don't insert if this is that first element.
—SA

I think this would be your solution..

C#
if(ddltype.selectedindex==0)
{
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Please select your gadget');", true);
return;
}
if(ddltype.selectedValue !="")
{ cmd = new SqlCommand("sp_submitquery", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@gadget", SqlDbType.VarChar).Value = ddltype.SelectedValue;


cmd.ExecuteNonQuery();


ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Your Query Submitted Sucessfully');", true);
}
 
Share this answer
 
v2
in your
ASP.NET
<asp:ListItem >SELECT YOUR GADGET</asp:ListItem>
put
Value="0"
u can also validate with a requiredfiled validator using InitialValue="0"
 
Share this answer
 
If you set index as a 0. So you will get the default index value before saving the data and then you check this index value as a 0 or not and fire the popup whatever you want.
 
Share this answer
 

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