Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

                //DateTime startDate = DateTime.Now;
                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)
            {
                //general exception
                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.
Posted
Updated 26-Apr-17 22:07pm
v2
Comments

1 solution

Without your code, or any access to your data, there isn't really anything we can do to help - you say "I execute it one time only" but we can't even see that one time!

Start by logging activity: every time you insert or update a record, log all the details to a file, and review the file after the problem has occurred. You may get information which helps you work out why it inserted twice.
If that doesn't help, add a trigger to SQL which also logs INSERT operations, and compare the two logs. If the SQL INSERT log doesn't agree with the app log, then there is a "hidden insert" somewhere in your app - either because you didn't log it, or because you have an update-to-DB going on behind the scenes perhaps from a bound table or similar.

Get what information you can, and take a good hard look at it - we can't do anything constructive from this distance!
 
Share this answer
 
Comments
amir tarek 27-Apr-17 4:10am    
i updated the question and added the code that insert the record
OriginalGriff 27-Apr-17 4:17am    
That doesn't really help - apart to show that this isn't the only problem you have, you are wide open to SQL Injection - because we can't tell if that is the only place you do it, or if that code is only executed once.
You need to get logging what is happening, and having a close look at it. This is a "run time" problem, and it needs run time data to track it down - individual code fragments don't help because they aren't connected the same way as they are in the running application!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900