开发者

SQL Insert query is executed twice

I'm using adapter.InsertCommand to insert some data into a table.

The only problem is that it's executed twice, thus giving me double entries in the DB. I've tried following the example in the documentation of adapter.InsertCommand and my own code, but get the same result.

This is my code:

public class nokernokDAL
{
    SqlConnection connection = new SqlConnection();
    SqlDataAdapter adapter = new SqlDataAdapter();

    public nokernokDAL()
    {
        connection.ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
        connection.Open();
    }

    public void addNewComment(int userID, int pageID, string title, string comment)
    {
        string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
                       "VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";

        adapter.InsertCommand = new SqlCommand(query, connection);
        adapter.InsertCommand.ExecuteNonQuery();

    }
开发者_开发知识库}

Anny suggestions in how I can avoid this?

UPDATE

Right, after some debugging, I discovered that my newWallComplaint_Click function was fired twice. This was becuae I had the following code:

    protected void Page_Load(object sender, EventArgs e)
    {
            btnNewWallComplaint.Click += new EventHandler(this.newWallComplaint_Click);
    }

Not checking for PostBack, this executes my function after submit also. So to avoid having my function run twice, I added a check for PostBack.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnNewWallComplaint.Click += new EventHandler(this.newWallComplaint_Click);
        }

    }

Now my query is not run twice.


I can't see anything in your code that would execute it twice. I'd assume that it is being called twice. Put a break point at addNewComment and if it is being called twice look at the stack traces to see where it is being called from on both occasions.

Maybe you have an event being called twice for example. This can happen in ASP.NET if you both have auto wiring of events enabled and have wired the event up explicitly.

By the way you should definitely use parametrized queries not string concatenation. I'm assuming that comment is user supplied input? In which case you are setting yourself up for a SQL injection attack with the code you have shown.


I found that I had both a datatable and a dataset but only needed the datatable...

because I had them both running beside eachother in the same command, a duplicate was created...

make sure you use everything in the command and you understand what each one does even if your up awake for 2 days just like I was...

            DataSet dataset = new DataSet();

            data.Fill(dataset, "data");

            // Populate a new data table and bind it to the BindingSource.
            DataTable datatable = new DataTable();
            data.Fill(datatable);

as you can see i had two sources filling one MySqlDataAdapter...

            //removing the following two lines fixed my duplicates issue...
            //DataSet dataset = new DataSet();
            //data.Fill(dataset, "data");

            // Populate a new data table and bind it to the BindingSource.
            DataTable datatable = new DataTable();
            data.Fill(datatable);

hope it helps someone...


Do you really need a DataAdapter for this? Maybe you can try this.

 public class nokernokDAL
 {
     string connectionString;

     public nokernokDAL()
     {
         ConnectionString = EPiServer.Global.EPConfig["EPsConnection"].ToString();
     }

     public void addNewComment(int userID, int pageID, string title, string comment)
     {
         string query = "INSERT INTO dbo.nokernok_kommentarer (userID, pageID, commentTitle, comment) " +
                        "VALUES ("+ userID +", "+ pageID +", '"+ title +"', '"+ comment +"')";

         using (SqlConnection conn = new SqlConnection(_connString))
         {
             SqlCommand cmd = conn.CreateCommand();
             cmd.CommandText = Query;
             conn.Open();
             cmd.ExecuteNonQuery();
         }
     }
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜