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

            }}
5
Contributors
7
Replies
8
Views
4 Years
Discussion Span
Last Post by Momerath
0

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 by Michael27

0

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?

0

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

Edited by Momerath

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.