C# .NET - Insert and retrieve values from dropdown list to database

Asked By Suguna on 04-Oct-10 11:21 AM

I have a dropdown list with four list items as follows

<asp:ListItem Enabled="true" Text="" Value="" Selected="False"></asp:ListItem>

<asp:ListItem Text="Live" Value="1" ></asp:ListItem>

<asp:ListItem Text="Pending" Value="2" ></asp:ListItem>

<asp:ListItem Text="Closed" Value="3" ></asp:ListItem>

while inserting into database i want to store values (1, 2, or 3) but for display the data after reading it has to show the text values(Live, Pending, Closed respectively)

can any body help me in this and ur help would be much appreciated

Santhosh N replied to Suguna on 04-Oct-10 11:28 AM
You can have your DB table with two columns and both can be inserted while inserting and while retrieving get the names and assign them as the displaymember of the listbox in the datasource so that they will be available as options
Vasanthakumar D replied to Suguna on 04-Oct-10 11:43 AM

you can use the DropDownlist's Text property for this.

1. This will return selected item's Value. So, while inserting use DrowDownId.Text.
2. to set the value to dropdown, you can use
  DropDownId.Text = //get value from database

Suguna replied to Santhosh N on 04-Oct-10 11:55 AM
is there any solution to store just values and not the names??
Santhosh N replied to Suguna on 04-Oct-10 12:02 PM
You could store but you wont have any relation between them while retrieving, and what exactly issue in storing both...
As far as I know, there should not be any issue as while displaying you can always get required column values and need not get all cols of the table..
Suguna replied to Santhosh N on 04-Oct-10 12:11 PM
thanks, for ur reply, my problem is i have five+ dropdown lists with various values and I was looking for a way of just storing minimal values in the database..
Santhosh N replied to Suguna on 04-Oct-10 12:14 PM
It should not be any problem inserting values for five different tables or even single table and it makes the DB with proper relations while you want to retrieve the data which otherwise difficult to get the values either when your values grows in the tables