views:

46

answers:

2

I keep getting this error:

Invalid object name "CAccounts".

and the code I have is:

System.Threading.Thread thread = new System.Threading.Thread(() =>
{
    // Set ConnectionString.
    String sConSg =
    "CONNSTRING HERE";

    using (SqlConnection connection = new SqlConnection(sConSg))
    {
        try
        {
            connection.Open();
        }
        catch (Exception exception)
        {
            MessageBox.Show(stat.Text = exception.Message);
        }

        try
        {
            SqlDataReader slrr = null;
            SqlCommand command = new SqlCommand("SELECT ActivationCode FROM CAccounts WHERE ActivationCode = " +
                "'" + _activationcode.Text + "'", connection);
            slrr = command.ExecuteReader();

            while (slrr.Read())
            {
                if (slrr["ActivationCode"].ToString() == _activationcode.Text)
                {
                    MessageBox.Show(slrr["ActivationCode"].ToString(), "AutoOptimise");

                }
                else
                {
                }
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(stat.Text = exception.Message);
        }
    }

});
thread.Start();

Can somebody please shed some light?

+2  A: 

The table CAccounts you're referencing in your select clause probably doesn't exist in the database. Check that.

See a list of possibilities here:

http://sqlserver2000.databases.aspfaq.com/why-do-i-get-object-could-not-be-found-or-invalid-object-name.html

Leniel Macaferi
Yep. It does. It should, I quad-triple checked. Unless it magically deletes itself right after I create it.
lucifer
I read the info on that page, then double-checked. Again. I'm not entering in any wrong info, or anything else they suggested.
lucifer
@j-t-s: ... or you are connecting to the wrong database :-p
Timwi
Nope. Double-checked that, too. :P
lucifer
+2  A: 

I'd suggest these things:

  • check which schema the object CAccounts is under. Is it dbo or other? Does the user have permissions on that schema?

  • login to SQL Server via Management Studio. Use the credentials in the connection string that the code is using. Paste & run the SQL statement above.

  • use SQL Profiler to capture/verify ensure the SQL statement as it crosses to your SQL Server. Run THAT as an adhoc query against that SQL Server.

  • are there any funny DNS issues? Hosts files? Does this happen during debugging or on the app server?

  • is the database server name correct? i.e. localhost versus a named server. Try addressing by the IP address that you expect it to be run at, just for fun, both in your connection string, and via SSMS.

p.campbell
I've done the first three of your suggestions, however, SQL Server Management studio doesn't seem to like connecting to the databases I have. BUT other applications do. I'm not sure if there are any funny DNS issues, I hadn't thought of that. I'll check that out now. I get these errors while debugging.
lucifer
Thanks, @p, I will try out addressing by the IP, too.
lucifer