I have a C# application and within the code I am checking whether a table exist:
// checking whether the table selected from the dataset exists in the database or not
string exists = null;
try
{
SqlCommand cmd = new SqlCommand("SELECT * FROM sysobjects where name = '" + tbTableName.Text + "'", myConnection);
exists = cmd.ExecuteScalar().ToString();
}
catch (Exception exce)
{
MessageBox.Show(exce.Message, "Program Message2", MessageBoxButtons.OK, MessageBoxIcon.Error);
exists = null;
}
Once that is done, if the table doesn't exist I create a new table, if the table exist I alter the columns if it is missing.
When I run the application and I click a button to check and the above code is run, I get the following error:
Object reference not set to an instance of an object
The table creation or modifying the table code works fine afterward but I am not sure why I am receiving the error.
Any idea how to fix it?