Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The dropdownlist in asp.net is filled dynamically & I want to store all it's value to SQL Server. First I want to get all the values to of dropdownlist in code behind? How to do it?
Posted
Comments
Dholakiya Ankit 19-Jul-13 6:13am    
i think solution2 is right you can do that by loop and getting values from it
[no name] 19-Jul-13 9:56am    
Your question and comments make no sense. You already have these values that you are putting into your DDL so what is the actual problem? And why are you unable to connect to your database and insert data into a table?
sunpop 19-Jul-13 11:33am    
The dropdownlist is filled dynamically & I know to access it in Javascript. But in code behind, i know to take the selected value. But I don't know to access ALL the items of the dropdownlist. So, I need help. Once I know to access all the items then, I can put it in the table.

C#
<asp:DropDownList ID="ddlCountry" runat="server" />



c# code  


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindContrydropdown();
}
}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
using (SqlConnection con = new SqlConnection("Data Source=maneavinash;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserId,UserName FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "UserName";
ddlCountry.DataValueField = "UserId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
}
 
Share this answer
 
Comments
sunpop 19-Jul-13 5:44am    
Sorry I want the other way. I don't want dropdownlist to be filled. I want the all the values of dropdownlist to be stored in DB
you can insert by for loop ...by getting a value every time and then insert it into DB
 
Share this answer
 
Comments
sunpop 19-Jul-13 7:05am    
Could you tell me the code to take the value (not selected value) from dropdownlist?

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