View Single Post
  #5 (permalink)  
Old July 31st, 2010, 04:29 AM
venubv venubv is offline
Registered User
 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populate Values from DB to Drop Downlist

//Declare SqlCommand
SqlCommand cmd;
//Declare SqlAdapter
SqlDataAdapter da;
//Decalre Dataset
DataSet ds;

//Declare String
string strQuery;

//Query
strQuery = "Select Column_Id,Column_Desc from Test_Table";

//decalre Connection to DB
SqlConnection con = default(SqlConnection);

//Connection String to SQL 2005
con = new SqlConnection("Data Source=MYServer-PC\\SQLEXPRESS;Initial Catalog=Sample_New;User Id=sa;Password=test@123;");

try
{
cmd = new SqlCommand(strQuery, con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
if (con.State != ConnectionState.Closed)
{
con.Close();
}
wddltest.DataSource = ds;


wddltest.DataTextField = "Column_Desc";
wddltest.DataValueField = "Column_Id";
wddltest.DataBind();
//wddltest.Items.Insert(0, New ListItem("All", "0"));

}
catch (Exception ex)
{
con.Close();
throw new Exception(ex.Message, ex);
}

More info visit :http://www.book4dotnet.com/