protected void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = null;

            con = new SqlConnection(@"Data Source=(LocalDB)\v11.0 ; AttachDbFilename=C:\Users\Bhawna\Documents\Visual Studio 2012\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Database2.mdf;Integrated Security=True ; User ID=sa ; Password=123");

            try
            {
                con.Open(); 
                String Sqlquery = (" Insert INTO adb (Studentid) values (@S)");
                SqlCommand sqlupdate = new SqlCommand(Sqlquery, con);
                sqlupdate.Parameters.AddWithValue("@S",Convert.ToInt16(textBox1.Text));
                //sqlupdate.Parameters.Add(new SqlParameter("@S", SqlDbType.VarChar, 50)).Value = textBox1.Text;
                int result= sqlupdate.ExecuteNonQuery();
                label1.Text =result.ToString() + " Row is inserted into table";

                textBox1.Text = "Connection Established";
                con.Close();
            }
            catch (Exception ex)
            {
                label1.Text ="outer catch: " + ex.Message;
                textBox1.Text = " Not Connected";

            }}

Dani AI

Generated

The error "Invalid object name adb" means SQL Server cannot find a table called adb in the database that the app is actually connected to. Common reasons (building on earlier replies): the INSERT is targeting a database name instead of a table (), the table really does not exist in the current database (), the table exists under a different schema (), or the LocalDB .mdf being attached at runtime is not the same copy that was inspected in Server Explorer (Visual Studio can copy the file to the output folder).

Quick checks that usually reveal the problem:

SELECT DB_NAME() AS CurrentDatabase;

SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'adb';
// quick runtime check after opening the connection
con.Open();
System.Diagnostics.Debug.WriteLine("Connected database: " + con.Database);

Practical fixes and things to verify:

  • Confirm whether adb is a table name or the database name. An INSERT must target a table; if adb is the database, use the actual table inside it or fully qualify the table as [DatabaseName].[Schema].[TableName]. If the table is named adb, prefix it with the schema (for example, [dbo].[adb]).
  • Check the connection string: LocalDB typically uses Integrated Security (Windows auth). Remove conflicting User ID/Password when Integrated Security=True.
  • If the project includes the .mdf file, check its "Copy to Output Directory" setting — a copied/empty MDF in bin\Debug will cause missing-table symptoms. Prefer Copy if newer or use |DataDirectory|.
  • Inspect the database and tables in SQL Server Object Explorer or SSMS to confirm exact names and schema before running the INSERT.

Summary checklist: verify current database (DB_NAME), list tables (INFORMATION_SCHEMA), confirm schema-qualified table name, fix connection string/copy behavior. This builds on the helpful points from , and while addressing common LocalDB pitfalls noted in similar threads by .

Recommended Answers

All 7 Replies

Any error messages? What doesn't work.

Please don't just give us a code chunk.

error was Invalid object name adb

here adb is the name of the database

In your INSERT statement adb is table not database, (studentid) is column an @s is the value you are inserting

the general expreseion goes like this INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

I tried that but i am still getting the same error

Do you have a table named adb? You're trying to insert a Student ID, are you sure the table isn't named Students or something similar?

Table name is adb

If you are getting invalid object name, either the table isn't 'adb' or there is no 'Studentid' column.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.