0

I have a database with following structure. now I want to populate a listbox with this table in c#. how can I do this?

Column      | Type
------------------------------
ID          | Unique Identifier;
txtname     | nvarchar(50);
txtcitycode | nchar(5);
1
  • 1
    show us what you tried Commented Mar 23, 2014 at 11:41

1 Answer 1

1

You can use this code:

using (SqlConnection con = new SqlConnection(YOUR_CONNECTION_STRING))
        {
            using (SqlCommand cmd = new SqlCommand("Select ID, txtname, txtcitycode from YOUR_TABLE", con))
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DropDownList1.DataTextField = "txtname";
                DropDownList1.DataValueField = "txtcitycode";
                DropDownList1.DataSource = dt;
                DropDownList1.DataBind();
            }
        }

Replace "YOUR_CONNECTION_STRING" with connection string and "YOUR_TABLE" with your table name

if you dont know connection string, you can find some useful information in this website: http://www.connectionstrings.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.