1

i have made a form in c# to add data into my datatable, it works fine, all i want is to return a message when the data is inserted, and i am having an issue with that, the code is as follows:

conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = ("INSERT INTO datatable (Number_Plate,Registered_Keeper,Make,Model,Year_Of_Make,Colour,Engine_Size,Transmission,Fuel_Type) Values (@Number_Plate,@Registered_Keeper,@Make,@Model,@Year_Of_Make,@Colour,@Engine_Size,@Transmission,@Fuel_Type)");
            cmd.Parameters.AddWithValue("@Number_Plate", Plate.Text); 
            cmd.Parameters.AddWithValue("@Registered_Keeper", Keeper.Text);
            cmd.Parameters.AddWithValue("@Make", Make.Text);
            cmd.Parameters.AddWithValue("@Model", Model.Text);
            cmd.Parameters.AddWithValue("@Year_Of_Make", Year.Text);
            cmd.Parameters.AddWithValue("@Colour", Colour.Text);
            cmd.Parameters.AddWithValue("@Engine_Size", Engine.Text);
            cmd.Parameters.AddWithValue("@Transmission", Transmission.Text);
            cmd.Parameters.AddWithValue("@Fuel_Type", Fuel.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (cmd.ExecuteNonQuery()==1)
            {
                button1.Visible = false;
                label10.Visible = true;
            }
            else
            {
                button1.Visible = false;
                label10.Text = "Data Not Added Please try Again!";
                label10.Visible = true;
            }
        }

when i run the code, i get an issue with the If statement ane the error is:

There is already an open DataReader associated with this Command which must be closed first.

any help appreciated.

2
  • Why do you have SqlDataReader reader = cmd.ExecuteReader(); in there? That doesn't make any sense... and the error is telling you exactly that. Commented Mar 12, 2013 at 17:41
  • 1
    Please read up on the using statement. SqlConnection, SqlCommand, and SqlDataReader (though you don't need it here) all implement IDisposable. Commented Mar 12, 2013 at 17:42

2 Answers 2

3

just remove this line (you don't need it)

SqlDataReader reader = cmd.ExecuteReader();

makes no sense because you are inserting a record and not fetching on the database.

snippet,

        // other codes
        cmd.Parameters.AddWithValue("@Engine_Size", Engine.Text);
        cmd.Parameters.AddWithValue("@Transmission", Transmission.Text);
        cmd.Parameters.AddWithValue("@Fuel_Type", Fuel.Text);
        if (cmd.ExecuteNonQuery()==1)
        {
            button1.Visible = false;
        // other codes
Sign up to request clarification or add additional context in comments.

1 Comment

i have tried that code and it is displaying the label but not inserting the data into the data table
0

You have open Connection to Database previously. So you need to close that first to execute.

conn.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.