Click here to Skip to main content
15,313,985 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.
   
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>
}
   
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;
}
Tom Marvolo Riddle 25-Apr-14 2:46am
   
what exception you're getting?
My Doubt 25-Apr-14 2:52am
   
Object reference not set to an instance of an object.
Tom Marvolo Riddle 25-Apr-14 2:54am
   
solution updated.Try it.In Either way 'Null' value will be stored in Sql column
Tom Marvolo Riddle 25-Apr-14 2:48am
   
or try like this:<br>
 <br>
if (ddltrees.SelectedItem.Text == "Select")<br>
{<br>
insert query without selected value---------don't pass dropdownlist selected value here<br>
}<br>
else<br>
{<br>
 <br>
insert query with selected value ------pass dropdownlist selected value<br>
}
My Doubt 25-Apr-14 2:49am
   
can u please send the exact code that I have given
Tom Marvolo Riddle 25-Apr-14 2:52am
   
another option updated(quite simple too).Try it
My Doubt 25-Apr-14 2:57am
   
ok

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


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