Edit: The line of code that gives the exception is "foreach (Column column in table.Columns) " and that's because the Table table object is null. How would I load a Table object? My code seemed to be the right approach but it doesn't work as expected. :D
I'm pulling a list of all columns in a given SQL table, and then loading them to a listbox. Here is the code:
public Dictionary<string,string> FindColumns(string tableName)
{
using (var mySqlConnection = new SqlConnection(Properties.Settings.Default.ConnectionString))
{
var myConnection = new ServerConnection(mySqlConnection);
var myServer = new Server(myConnection);
var myDatabase = myServer.Databases["Shipments"];
//Using a breakpoint I figured out that table is remaining null. That's
//why I'm getting this error. How can I pull a table then?
Table table = myDatabase.Tables[tableName];
foreach (Column column in table.Columns)
{
Columns.Add(column.Name, column.DataType.ToString());
}
}
return Columns;
}
I'm getting the error on the above foreach loop.
And here is where I invoke the method:
private void lstTableNames_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (var column in db.FindColumns("Shipments"))
{
lstTableColumns.Items.Add(column.Key + "--" + column.Value);
}
}
Thanks for the help!