0

I am trying to populate a dropdownlist with the name of the tables in my SQL database. This is my code attempt but nothing is happening when I call it and the dropdownlist stays empty.

public  void FillDropDownList()
    {
        String connString = "myConnectionString";

        //**STRING UPDATED**
        String Query = "SELECT * FROM information_schema.tables";

        using (var cn = new SqlConnection(connString))
        {
            cn.Open();
            DataTable dt = new DataTable();
            try
            {
                SqlCommand cmd = new SqlCommand(Query, cn);
                SqlDataReader myReader = cmd.ExecuteReader();
                dt.Load(myReader);
            }
            catch (SqlException e)
            {
                //to be completed
            }

            radDropDownList1.DataSource = dt;
            radDropDownList1.ValueMember = "TABLES_NAME";
            radDropDownList1.DisplayMember = "TABLES_NAME";
        }
    }

How can I populate the dropdownlist with the above query?

5
  • Have you checked your dataTable. is there any records exists? Commented Oct 20, 2014 at 11:01
  • Make sure your Postback., Commented Oct 20, 2014 at 11:02
  • @vallabha yes, I have data in there. I think the problem is with the displayMember and ValueMember. I have used a different query that I have updated in my question Commented Oct 20, 2014 at 11:06
  • @SonerGönül if I run the update query straight in the DB would return what I need. I checked. What I get in the dropdown is System.Data.RowView... and not the tables name! Commented Oct 20, 2014 at 11:10
  • If you are using the same code as posted the below answer that i had given should work Commented Oct 20, 2014 at 11:12

2 Answers 2

2

You query is not correct, it needs to be

SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_TYPE = 'BASE TABLE'
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is with the string that you are using as ValueMember and DisplayMember , please replace the below

 radDropDownList1.ValueMember = "TABLES_NAME";
 radDropDownList1.DisplayMember = "TABLES_NAME";

with

 radDropDownList1.ValueMember = "TABLE_NAME";
 radDropDownList1.DisplayMember = "TABLE_NAME";

1 Comment

@user3855329 please mark it as answer, so that others can get benefited

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.