I want to fill data in drop down list from database table.
plz give me code for this

What DBMS?

Its quite easy but you would agree with me that you would learn faster and better if you try it out yourself. Try something out and if you encounter any problems, just post your codes and whatever error message

Error:

Incorrect syntax near the keyword 'table'.

MY code is:

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

            

            ddl.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            String strQuery = "SELECT name FROM table";

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;

            cmd.Connection = con;

            try
            {

                con.Open();

                ddl.DataSource = cmd.ExecuteReader();

                ddl.DataTextField = "ContactName";

                ddl.DataValueField = "CustomerID";

                ddl.DataBind();

            }

           

            finally
            {

                con.Close();

                con.Dispose();

            }

        }
    }

Finally i got the answer
my code is :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            

            ddl.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            String strQuery = "SELECT name FROM customer";

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;

            cmd.Connection = con;

            try
            {

                con.Open();

                ddl.DataSource = cmd.ExecuteReader();

                ddl.DataTextField = "name";

                ddl.DataValueField = "name";

                ddl.DataBind();

            }

           

            finally
            {

                con.Close();

                con.Dispose();

            }

        }
    }
}

I want to fill data in drop down list from database table.
plz give me code for this

----------------------------------------------------


<asp:DropDownList ID="dropdepartment" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Department"
DataValueField="Department_id">
</asp:DropDownList>

or you can also use the dataset or querystring....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.