Click here to Skip to main content
16,022,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using the following coding to fill a drop down list.The values are imported from a table in sql server.There's no error in the code but the data isn't retrieved. What is the problem? Thanks in advance.

C++
SqlDataAdapter da;
SqlConnection con;
SqlCommand com;

string connect = ConfigurationSettings.AppSettings["connstring"];
con = new SqlConnection(connect);
con.Open();


com = new SqlCommand("select PostCode from PostCode", con);
da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
PostCode.DataSource = dt;
PostCode.DataTextField = "PostCode";
PostCode.DataBind();
con.Close();
Posted
Comments
Smrng 23-Aug-12 6:34am    
when i used
if (!IsPostBack)
{
//above syntax
}

it worked fine but not with all fields.some are not displayed

Hi Smrng,

try to use like this!!

VB
ddl.datasource = dt;
PostCode.DataTextField = "PostCode";

PostCode.DataValueField = "PostCode";

 PostCode.DataBind();


otherwise use this


 string qry1 = "select RoutNo from Tbl_Bus_RoutDetails";
        da = new SqlDataAdapter(qry1, con);

        da.Fill(ds, "Tbl_Bus_RoutDetails");
        dt = ds.Tables["Tbl_Bus_RoutDetails"];

        DropDownList1.Items.Clear();
        foreach (DataRow Row in dt.Rows)
        {
            ListItem item1 = new ListItem();
            item1.Value = Row[0].ToString();
            DropDownList1.Items.Add(item1.Value);
        }
        DropDownList1.Items.Insert(0, new ListItem("Select", "Default value"));
        da.Dispose();

regards
sarva
 
Share this answer
 
v3
SqlDataAdapter da;
SqlConnection con;
SqlCommand com;

string connect = ConfigurationSettings.AppSettings["connstring"];
con = new SqlConnection(connect);
con.Open();


com = new SqlCommand("select PostCode from PostCode", con);
da = new SqlDataAdapter(com);
DataTable dt = new DataTable();
da.Fill(dt);
PostCode.DataSource = dt;
PostCode.DataTextField = "PostCode";
postcode.DatavalueField="Provide value"
PostCode.DataBind();
con.Close();
 
Share this answer
 
Try This:

 private void FillDropDownList1()
    {
        con.open();
        DataSet ds = new DataSet();
        SqlDataAdapter myda = new SqlDataAdapter("select PostCode from PostCode", connection Object);
        myda.Fill(ds);
        Drop_Postcode.DataSource = ds;
        Drop_Postcode.DataValueField = "Postcode";
        Drop_Postcode.DataBind();

    }

Call this function In Page Load Event....

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDropDownList1();
        }
        
    }


if any Query,Please Post It....
 
Share this answer
 

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