Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a drop down list for trees. On selecting the tree name and press the submit button, the corresponding id will be stored in the sql table. But I set default as --select-- in the drop down list, this means no value is selected. So when I click submit button nothing should be stored in the sql database. But it stores either id 0 or --select--.
Posted
Comments
ArunRajendra 25-Apr-14 1:29am    
Post what ever code / query you have tried.
Tom Marvolo Riddle 25-Apr-14 1:29am    
Use dropdownlist.selecteditem.text to get the selected value.
My Doubt 25-Apr-14 1:51am    
ddltrees.Items.Insert(0, new ListItem("--Select--", string.Empty));
This is my query
Tom Marvolo Riddle 25-Apr-14 1:52am    
solution posted .Try it

Check first the selected value of the dropdown list upon clicking the submit button.
 
Share this answer
 
Comments
My Doubt 25-Apr-14 1:52am    
The first item id will be created as 0 but I need it as blank
Use DropDownList1.SelectedItem.Text to get the selected value.

Try like this:
C#
 if( DropDownList1.SelectedItem.Text =="Select")
  {
  DropDownList1.Items.Clear();
 DropDownList1.Items.Insert(00, "Null");
 DropDownList1.SelectedIndex = 0;
 //insert query here (Null)
 }
else
 {
  // insert query here( SelectedItemValue)
 }

or
C#
if (ddltrees.SelectedItem.Text == "Select")
{
//insert query without selected value---------don't pass dropdownlist selected value here
}
else
{
//insert query with selected value ------pass dropdownlist  selected value;<br>
}
 
Share this answer
 
v3
Comments
My Doubt 25-Apr-14 2:06am    
Hi Jas,
The code you have send is I already tried and found the problem that I mentioned in my question. I need to get a blank value when nothing is selected. Because I am using a label which displays what the thing is selected in the drop down list. So when the user doesn't select anything it should show blank otherwise it will be a bug.
Tom Marvolo Riddle 25-Apr-14 2:08am    
oh i'm sorry .I get it wrong.Please wait i'll update soon
My Doubt 25-Apr-14 2:11am    
ok I am waiting
Tom Marvolo Riddle 25-Apr-14 2:15am    
solution updated.If you're not sending anything(selected item) the default Null value will be stored in Sql.

Try it & let me know
My Doubt 25-Apr-14 2:42am    
I tried this code but it will be an exception.
if (ddltrees.SelectedItem.Text == "Select")
{
ddltrees.Items.Clear();
ddltrees.Items.Insert(0, "Null");
ddltrees.SelectedIndex = 0;
}
else
{

ddltrees.DataValueField = "TreeID";
ddltrees.DataTextField = "TreeName";
ddltrees.DataBind();
ddltrees.Items.Insert(00, "Select");
ddltrees.SelectedIndex = 0;
}

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