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.