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";

            }}

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,...)

Edited 4 Years Ago by Michael27

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?

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

Edited 4 Years Ago by Momerath

This article has been dead for over six months. Start a new discussion instead.