Home > Net >  SqlDataAdapter.Fill running SQL command twice
SqlDataAdapter.Fill running SQL command twice

Time:10-14

I'm trying to insert a new row in my SQL database from ASP.NET but it's inserting the row twice instead of once.

I haven't been able to find which line of the code below is causing this.

Here's my code:

 public static void Register(User user)
        {
            string query = "insert into TblTutors (username,email,pass,sub,category,city,fullname,img,bio,tutor,way)

           values (@username,@email,@pass,@mat,@cat,@country,@fullname,Cast(@img As nvarchar(MAX)),@bio,@tutor,@way )";
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.AddWithValue("@username", user.username);
                cmd.Parameters.AddWithValue("@email", user.email);
                cmd.Parameters.AddWithValue("@tutor", user.tutor);
                cmd.Parameters.AddWithValue("@way", user.way);
                cmd.Parameters.AddWithValue("@mat", user.mat);
                cmd.Parameters.AddWithValue("@cat", user.cat);
                cmd.Parameters.AddWithValue("@country", user.country);
                cmd.Parameters.AddWithValue("@pass", "halima");
                cmd.Parameters.AddWithValue("@fullname", user.fullname);
                cmd.Parameters.AddWithValue("@img", user.img);
                cmd.Parameters.AddWithValue("@bio", user.bio);
                con.Open();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sda.Fill(dt);

                int i = cmd.ExecuteNonQuery();
                con.Close();

            }

        }

CodePudding user response:

DataAdapter.Fill is used to populate a DataSet with the results of the SelectCommand of the DataAdapter.

Since you're not looking to do any SELECT queries, remove the code regarding filling the DataTable as while it won't return any data, it will execute your INSERT, UPDATE and DELETE SQL commands inside cmd passed to new SqlDataAdapter(cmd);.

You're essentially writing data twice, once when you fill dt & again when you execute the query:

sda.Fill(dt);
...
int i = cmd.ExecuteNonQuery();

This should work as expected, removing the need for a DataSet as well.

public static void Register(User user)
{
    string query = "insert into TblTutors (username,email,pass,sub,category,city,fullname,img,bio,tutor,way)

   values (@username,@email,@pass,@mat,@cat,@country,@fullname,Cast(@img As nvarchar(MAX)),@bio,@tutor,@way )";
    using (SqlCommand cmd = new SqlCommand(query, con))
    {
        cmd.Parameters.AddWithValue("@username", user.username);
        cmd.Parameters.AddWithValue("@email", user.email);
        cmd.Parameters.AddWithValue("@tutor", user.tutor);
        cmd.Parameters.AddWithValue("@way", user.way);
        cmd.Parameters.AddWithValue("@mat", user.mat);
        cmd.Parameters.AddWithValue("@cat", user.cat);
        cmd.Parameters.AddWithValue("@country", user.country);
        cmd.Parameters.AddWithValue("@pass", "halima");
        cmd.Parameters.AddWithValue("@fullname", user.fullname);
        cmd.Parameters.AddWithValue("@img", user.img);
        cmd.Parameters.AddWithValue("@bio", user.bio);

        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();
    }

}
  • Related