Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno",comboBox1.Text);
            command.CommandText = "INSERT into details"+ "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

            ClearFields();
        }


When i enter one set of details and click the button, it updates the database. but I am unable to enter a second set of details the second time around. It says @StudentID has already been declared.
Posted
Comments
pramod.hegde 23-Aug-12 6:39am    
Are you trying to insert the record for the same StudentID? If StudentID is a primary key, then the insert statement would fail, as primary key should be unique.
codingisok101 23-Aug-12 6:45am    
no the problem was that everytime i entered one set of records, i could not enter another set of records even after clicking the button. the previous set of records still existed in the form

1 solution

Add
C#
command.Parameters.Clear();


C#
private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno",comboBox1.Text);
            command.CommandText = "INSERT into details"+ "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
                command.Parameters.Clear()
            }

            ClearFields();
        }
 
Share this answer
 
v2
Comments
codingisok101 23-Aug-12 6:39am    
Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900