thormayer
thormayer

Reputation: 1070

Why insert statement generates 2 rows?

I dont know why, but when I do an insert statement in my project, its generate 2 indentical rows instead of makeing just one.

why is that ?

this is my code :

if (ListBox.Items.Count != 0)
        {
            string username = Session["Session"].ToString();

            con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
            con.Open();

            string knowWhichOne = "SELECT ID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            SqlCommand comm = new SqlCommand(knowWhichOne, con);
            int userID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT ClassID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int classID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT SchoolID FROM Users WHERE Username='"+UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int schoolID = (Int32)comm.ExecuteScalar();

            if (RadioWords.Checked == true)
            {
                 game = 1;
            }
            else
            {
                 game = 2;
            }
            string arr = "";
            for (int i = 0; i < ListBox.Items.Count; i++)
            {
                arr += ListBox.Items[i] +",";
            }

            string sqlqueryString = "INSERT INTO HistoryOfGames (GameID, UserID, LengthOfArray, NumberOfErrors, ClassID, SchoolID,Arrayarray) VALUES (@GameID, @UserID, @LengthOfArray, @NumberOfErrors, @ClassID, @SchoolID, @Arrayarray);" + "SELECT SCOPE_IDENTITY()";

            SqlCommand commandquery = new SqlCommand(sqlqueryString, con);

            commandquery.Parameters.AddWithValue("GameID", game);
            commandquery.Parameters.AddWithValue("UserID", userID);
            commandquery.Parameters.AddWithValue("LengthOfArray", HowMany.Text);
            commandquery.Parameters.AddWithValue("NumberOfErrors", 0);
            commandquery.Parameters.AddWithValue("ClassID", classID);
            commandquery.Parameters.AddWithValue("SchoolID", schoolID);
            commandquery.Parameters.AddWithValue("Arrayarray", arr);

            commandquery.ExecuteNonQuery();





            int IdOfRecentHistoryGame = (int)(decimal)commandquery.ExecuteScalar();
            con.Close();

            Response.Redirect("NowPlay.aspx?ID="+ IdOfRecentHistoryGame);

        }

Upvotes: 1

Views: 283

Answers (4)

Ali Mousavi
Ali Mousavi

Reputation: 7

I had the same problem,I handled it this way.not professional but it works:

Dim x As Boolean = True

If x = True Then

here goes your code to insert to database.

End If

x = False

Upvotes: -1

Fredou
Fredou

Reputation: 20100

you do

   commandquery.ExecuteNonQuery();

then right after

   int IdOfRecentHistoryGame = (int)(decimal)commandquery.ExecuteScalar();

you do execute it twice

and don't forget to check for sql injection in your code...

Upvotes: 2

Stu
Stu

Reputation: 15769

You're running it twice, ExecuteNonQuery() and ExecuteScalar(). Get rid of the ExecuteNonQuery().

Upvotes: 5

Cristian Lupascu
Cristian Lupascu

Reputation: 40516

I'd check two things:

  • see how many times this statement is executed (try setting a breakpoint to verify that the code is only run once)
  • see if there are any triggers in the database that might cause an extra record to be inserted

Upvotes: 1

Related Questions