I have created a program in C# that would allow the user to execute his query on a table that exists in a given database. Further, I want the user to select a table from the combo box on which he wants to execute his query. However, I am unable to fetch the table names from the database into the combo box. This is the code that I am using:
public partial class AddQuery : Form
{
public AddQuery()
{
InitializeComponent();
fill_combo();
}
void fill_combo()
{
string cmdstr = "Use Dev_Server";
SqlConnection con = new SqlConnection(@"Data Source=INPDDBA027\NGEP;Initial Catalog=Dev_Server;Integrated Security=True");
SqlCommand cmd = new SqlCommand(cmdstr,con);
DataSet ds = new DataSet();
try
{
con.Open();
cmd.ExecuteNonQuery();
//comboBox1.Items.Add(dr);
foreach (DataTable dt in ds.Tables)
{
comboBox1.Items.Add(dt.TableName[0]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Please Help.