1

I have been working on a software which uses database which is shared with multiple PCs. The project is all about to store missing baggage information. We have given a facility to copy the newly inserted record into the master DB. Now what here happens is when multiple users are trying to update the db at the same time single item get stored for multiple time in the global DB.

So I have tried to use TableLock using serializable but I got nothing here.

Edit

 query = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;BEGIN TRANSACTION;";
 insertdata(query); //using cmd.ExecuteNonQuery();
 query = "select * from Goods WITH (TABLOCKX)" ;
 DataSet dsGoods = getdata(query, "config");//Function to get the data
 updateitem();
 query = "COMMIT TRANSACTION";
 insertdata(query);//using cmd.ExecuteNonQuery();

And the updateitem() is as follow

public static void updateitem()
{
            string query = "select * from config where param='lastsync'";
            DataSet ds = dataaccess.getdata(query, "config");
            query = "select isonlive,associateid,itemid,founddate,regdate,status,foundbyname,categoryid,subcatid,item,model,color,foundwhere,returnedtoname,showonline,officeid,isdeleted,(select username from [user] where userid=registeredby) as reguser,(select username from [user] where userid=returnby) as returnedby,notes,returneddate from  item ";
            String updatedDate =ds.Tables[0].Rows[0]["value"].ToString();
            if (updatedDate != "")
            {

                query = "select isonlive,associateid,itemid,founddate,regdate,foundbyname,status,categoryid,subcatid,item,model,color,foundwhere,returnedtoname,officeid,showonline,isdeleted,(select username from [user] where userid=registeredby) as reguser,(select username from [user] where userid=returnby) as returnedby,notes,returneddate from  item  where updateat >= @updateat";
            }
            System.Data.SqlClient.SqlCommand cmd = new SqlCommand(query);
            if (updatedDate != "")
            {
                cmd.Parameters.AddWithValue("@updateat",DateTime.ParseExact(updatedDate,"dd-MM-yyyy HH:mm:ss",null,System.Globalization.DateTimeStyles.None));
            }
            DataRow dr;
            ds = dataaccess.getdata(cmd, "item");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                try
                {
                    int status = 0;
                    dr = ds.Tables[0].Rows[i];
                    if (dr["status"].ToString() == "Transferred")
                    {
                        status = 2;
                    }
                    else if (dr["status"].ToString() != "Received")
                    {
                        status = 1;
                    }



                    DateTime regdate = Convert.ToDateTime(dr["regdate"]);
                    DateTime founddate = Convert.ToDateTime(dr["founddate"]);
                    //returndatetime = String.Format("MMM dd yyyy H:mm:ss", returndate);

                    if (dr["showonline"].ToString() == "False")
                    {
                        status = 1;
                    }

                    if (dr["isdeleted"].ToString() == "true")
                    {
                        insertdata("delete from goods where AssociateID='" + dr["associateid"] + "' and ID='" + dr["itemid"] + "'");
                        continue;
                    }

                    if (dr["isonlive"].ToString() == "true")
                    {

                        query = "update goods set Status='" + status + "',officeid='" + dr["officeID"] + "', notes='" + dr["notes"].ToString().Replace("'", "''") + "',ReturnedTo='" + dr["returnedtoname"].ToString().Replace("'", "''") + "',founddate=@founddate,ReturnedDate=@returndate,ReturnedBy='" + dr["returnedby"].ToString().Replace("'", "''") +
                            "',Model='" + dr["model"].ToString().Replace("'", "''") + "',ColorID='" + dr["color"].ToString().Replace("'", "''") + "',FoundWhere='" + dr["foundwhere"].ToString().Replace("'", "''") + "',MainCat='" + dr["categoryid"] + "',SubCat='" + dr["subcatid"] + "',ItemID='" + dr["item"] + "'  where AssociateID='" + dr["associateid"] + "' and ID='" + dr["itemid"] + "'";
                    }
                    else
                    {
                        query = "select * from goods where AssociateID='" + dr["associateid"] + "' and ID='" + dr["itemid"] + "' and MainCat='" + dr["categoryid"] + "' and SubCat='" + dr["subcatid"] + "' and ItemID='" + dr["item"] + "'";
                        DataSet dsItems = getdata(query, "config");

                        if(dsItems.Tables[0].Rows.Count==0)
                        {
                            query = "insert into goods (AssociateID,ID,DateReg,Status,MainCat,SubCat,ItemID,Model,ColorID,FoundWhere,RegBy,FoundBy,ReturnedTo,ReturnedDate,ReturnedBy,Notes,IP,founddate,officeid) values('" + dr["associateid"] + "','" + dr["itemid"] + "',@regdate,'" + status + "'," +
                            "'" + dr["categoryid"] + "','" + dr["subcatid"] + "','" + dr["item"] + "','" + dr["model"].ToString().Replace("'", "''") + "','" + dr["color"].ToString().Replace("'", "''") + "'," +
                            "'" + dr["foundwhere"].ToString().Replace("'", "''") + "','" + dr["reguser"].ToString().Replace("'", "''") + "','" + dr["reguser"].ToString().Replace("'", "''") + "','" + dr["returnedtoname"].ToString().Replace("'", "''") + "',@returndate," +
                            "'" + dr["returnedby"].ToString().Replace("'", "''") + "','" + dr["notes"].ToString().Replace("'", "''") + "','',@founddate,'" + dr["officeID"].ToString() + "')";
                        }

                    }

                    SqlCommand sce = new SqlCommand(query);

                    if (dr["returneddate"].ToString() != "")
                    {

                        sce.Parameters.AddWithValue("@returndate", Convert.ToDateTime(dr["returneddate"]));
                    }
                    else
                    {
                        sce.Parameters.Add("@returndate", SqlDbType.DateTime).Value = DBNull.Value;
                    }

                    sce.Parameters.AddWithValue("@regdate", regdate);
                    sce.Parameters.AddWithValue("@founddate", founddate);
                    insertdata(sce);
                    query = "update item set isonlive = 'true',updateat=@updateDate where itemid = '" + dr["itemid"] + "'";
                    sce = new SqlCommand(query);
                    sce.Parameters.AddWithValue("@updateDate", DateTime.Now);
                    dataaccess.insertdata(sce);
                }
                catch (Exception ex)
                {
                    App.writelog(ex.Message + "\n" + ex.StackTrace);
                }

            }
}

P.S.: I want it to be done though ASP.Net.

Community
  • 1
  • 1
The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
  • 4
    "but i got nothing here" ??? What is the problem that you are having with this? Note, that tablock is a very radical solution. – usr Apr 13 '15 at 10:45
  • @usr I have tried to lock the table and tried it again to create the same situation but still i am getting same issue i.e. multiple rows are getting inserted. And if i am trying to use "commit trans" here it gives me error *The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.* Do you have any possible solution for this? – The Hungry Dictator Apr 13 '15 at 10:47
  • Seems to me like you maybe using update instead of insert. – Zohar Peled Apr 13 '15 at 10:47
  • @ZoharPeled - I am using it for both update and insert. As if item is already available then it gets updated (that too only if any updation has been made else it does nothing.) – The Hungry Dictator Apr 13 '15 at 10:50
  • 2
    We need to see some code in order to find out what's wrong. if you can provide a minimal effective code that demonstrate the problem you are facing it would help us to help you. – Zohar Peled Apr 13 '15 at 10:53
  • @ZoharPeled i have updated the question as inserted as minimal code as i could. – The Hungry Dictator Apr 13 '15 at 11:00
  • if you commit a transaction without explicitly starting one manually you will get the "no corresponsing begin transaction" error. Since we can't see the flow of your code, we can't assume how many transactions you're starting/commiting in the underlying code. Your query is missing a "BEGIN TRANSACTION" statement somewhere when you try to insert – DerpyNerd Dec 01 '16 at 11:11

0 Answers0