i have an asp.net web application, i have a problem in it that if a page (not all pages) has a button call a c# function that insert a record on a table in sql server 2012 database, sometimes the query runs twice although i execute it one time only.
this problem happens only when i deploy the application on a server and doesn't happen every time as i deployed this app on 4 servers 2 of them has this problem and not always happen and 2 of them works fine, although one of the servers that works fine has 60 GB database (the largest)
the code that use for insert
public static bool InsertToDB(SqlConnection Conn, string InsertStr)
{
SqlConnection LocalConnection = new SqlConnection();
try
{
LocalConnection = new SqlConnection("user id=xxx;" +
"password=" + pass + ";server=" + Server + " ;Trusted_Connection=no;" +
"database=" + DataBaseName + "; " +
"connection timeout=1000; MultipleActiveResultSets=True;max pool size=500");
LocalConnection.Open();
SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
SqlCommand MySqlCommand = new SqlCommand();
SqlCommand MySqlCommandWrite = new SqlCommand();
MySqlCommandWrite.CommandText = InsertStr;
MySqlCommandWrite.CommandTimeout = 999999999;
MySqlCommandWrite.Connection = LocalConnection;
MySqlCommandWrite.ExecuteNonQuery();
LocalConnection.Close();
LocalConnection.Dispose();
SqlConnection.ClearPool(LocalConnection);
return true;
}
catch (Exception ex)
{
LocalConnection.Close();
LocalConnection.Dispose();
SqlConnection.ClearPool(LocalConnection);
}
}
the code behind the button
protected void BTN_Adddeposit_Click(object sender, EventArgs e)
{
double temp;
int temp2;
long temp3;
try
{
int DivisionCode = 0;
int SectorCode = Convert.ToInt32(HttpContext.Current.Session["SectorCode"]);
int GenDivisionCode = Convert.ToInt32(HttpContext.Current.Session["GenDivisionCode"]);
CLSDeposit OBJDeposit = new CLSDeposit(DivisionCode,SectorCode,GenDivisionCode);
OBJDeposit.ClientID = ClientID;
OBJDeposit.active = 0;
OBJDeposit.DepositType = 1;
OBJDeposit.DivisionCode = DivisionCode;
OBJDeposit.DepositDate=Convert.ToDateTime(TXT_DepositDate.Text);
OBJDeposit.DepositID=Convert.ToInt32(TXT_DepositID.Text);
OBJDeposit.DepositValue=Convert.ToDouble(TXT_DepositValue.Text);
OBJDeposit.InstallmentValue = OBJDeposit.DepositValue;
OBJDeposit.InvoiceNo = Convert.ToInt64(TXT_DepositFatoora.Text).ToString();
OBJDeposit.NumOfParts = 1;
OBJDeposit.RearrangeDate = new DateTime(1900, 1, 1);
OBJDeposit.IsPaid=0;
OBJDeposit.Reason = TXT_Notes.Text;
OBJDeposit.DebitBeforeChange = OBJDeposit.DepositValue;
OBJDeposit.UserID = Convert.ToInt32(HttpContext.Current.Session["LoginUserID"]);
bool OperationResult = OBJDeposit.InsertDeposit();
}
catch (Exception err)
{
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "swal('خطأ في إتمام العملية','" + err.Message + "', 'error')", true);
}
}
the code that insert
public bool InsertDeposit()
{
string InsertDepositCommand = "INSERT INTO [dbo].[Deposits]([ser],[ClientID],[DDate],[DepitRemain],[Depit],[paid],[Paiddate],[UserID],[DivisionCode],[GenDivisionCode],[SectorCode],[DebitBeforeChange],Reason,installment,installmentVal,FormNo,DepType,RearrangeDate,active,RegisterDate) VALUES";
InsertDepositCommand+="("+this.DepositID+","+this.ClientID+",convert(DATETIME,'" + DepositDateString + "',103)"+","+this.DepositValue+","+this.DepositRemain+","+this.IsPaid;
InsertDepositCommand += ",convert(DATETIME,'" + PaidDateString + "',103)," + UserID + "," + this.DivisionCode + "," + this.GenDivisionCode + "," + this.SectorCode + "," + DebitBeforeChange + ",'" + Reason + "'," + NumOfParts + "," + InstallmentValue + "," + InvoiceNo + "," + DepositType + ",convert(DATETIME,'" + DepositDateString + "',103)," + active + ",convert(DATETIME,'" + RegiterDateString + "',103))";
bool OperationResult = SQLDataBase.InsertToDB(SQLDataBase.LocalConnection, InsertDepositCommand);
return OperationResult;
}
What I have tried:
i tried to search to find any solution for this or at least any reason but i can't as it is not a persistent problem and doesn't have a persistent condition to happen.