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

            }}

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?

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.