0

My SQL query isn't dropping anything into the combobox. The connection seems to be made but the while loop doesn't seem to work. Can anybody tell me what it wrong?

string sqltable = ("dbo.SLTDS_C"+id+"_table");  

SqlConnection con = new SqlConnection("Data Source=" + server   + ";Initial Catalog=" + database + ";Integrated Security=" + security);  
con.Open();  

string sqldatapull = ("select name from syscolumns where id = object_id('" + sqltable + "') order by name asc");
SqlCommand cmd = new SqlCommand(sqldatapull, con);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())  
{  
    sqldatapull = dr[0].ToString();  
    comboBox1.Items.Add(sqldatapull);  
}  

dr.Close();  
con.Close();  

Correction code:
string sqldatapull = ("select name from syscolumns where id = object_id('" + sqltable + "') order by name asc");

3
  • 5
    Are you sure that you're getting results back with that query? Commented Dec 7, 2009 at 20:47
  • 1
    no i'm not string sqldatapull = ("select name from syscolumns where id = object_id('" + sqltable + "') order by name asc"); fixed it all. Commented Dec 7, 2009 at 21:04
  • 1
    is there any reason you aren't using parameters/markers instead of just string concat? just trying to avoid sql injection down the line... Commented Dec 7, 2009 at 21:54

1 Answer 1

2

It's because you're including dbo. as part of the table name. If you run

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

You will see that the table names have no schema in the TABLE_NAME column.

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.