how to add data in dropdown using database using

Posted on 2009-12-19
Last Modified: 2012-05-08

i know its a easy question but i would to know .. please write the code..

i want to know how could i select the one row from database and insert it into my dropdown.. using

query str = "select customerName from Customer where id=" '+custId+' "=customerId";

i want to select those customers whoz id is same which i pass via parameter and check the database id.. kindly give the code..

    LVL 5

    Accepted Solution


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;
    using System.Text;
    public partial class _Default : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
        private string GetConnectionString()
            return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        private void BindDropDownList()
            DataTable dt = new DataTable();
            SqlConnection connection = new SqlConnection(GetConnectionString());
                string sqlStatement = "SELECT Top(20)* FROM Customers";
                SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
                SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
                if (dt.Rows.Count > 0)
                    DropDownList1.DataSource =dt;
                    DropDownList1.DataTextField = "ContactName"; // the items to be displayed in the list items
                    DropDownList1.DataValueField = "CustomerID"; // the id of the items displayed
            catch (System.Data.SqlClient.SqlException ex)
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);

    Open in new window

    LVL 5

    Expert Comment

    Change Select query to your querylike this:
    select customerName from Customer where id=' "'+custId+" ' ";

    Open in new window


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Dynamic Database connection in SSIS 2012 11 42
    SQL Server 2008 Error 7 34
    query question 4 20
    Custom auto number 6 27
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now