views:

67

answers:

2

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.

+2  A: 

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.

Martin Smith
Thanks. Debugging is not working, but I'm good at outputting strings. It turned outthat I did not use if(!isPostBack) on page load, thus executing my script twice. See updated code.
Steven
A: 

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();
        }

    }
}
TooFat