The INSERT statement conflicted with the FOREIGN KEY constraint “FK_tKosikZbozi_tKosik”

耗尽温柔 提交于 2019-12-25 15:55:53

问题


i have eshop with tZbozi (table of products), tKosik (table of shopping cart), and middle table, so that is N:M. In my PC everything works fine, but when I put it on the server, i got

"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tKosikZbozi_tKosik". The conflict occurred in database "sumenergocz1", table "dbo.tKosik", column 'idKosik'. The statement has been terminated."

Here is my ADD method:

            // Check if exist row of this product in this cart table
            bool exist = false;
            int pocetKosik = 0;
            string sqlCommand = "SELECT zboziPocet FROM tKosikZbozi WHERE zboziID = @idZbozi AND kosikID = @id";
            SqlCommand select = new SqlCommand(sqlCommand, connection);
            select.Parameters.Add("@idZbozi", SqlDbType.Int).Value = ProductID;
            select.Parameters.Add("@ID", SqlDbType.Int).Value = this.cookie.Values["id"];
            connection.Open();
            if (select.ExecuteScalar() != null)
            {
                pocetKosik = Convert.ToInt32(select.ExecuteScalar());
                exist = true; // if exist row, i save that it exist
            }
            connection.Close();







// Update date in cart table
            sqlCommand = "UPDATE tKosik SET casPristupuKosik = @newDate WHERE idKosik = @id";
            SqlCommand updateDate = new SqlCommand(sqlCommand, connection);
            updateDate.Parameters.Add("@newDate", SqlDbType.VarChar, 14).Value = this.cookie.Values["date"];
            updateDate.Parameters.Add("@id", SqlDbType.Int).Value = this.cookie.Values["id"];
            connection.Open();
            updateDate.ExecuteNonQuery();
            connection.Close();



// HERE ADD SOMETHING INTO MIDDLE TABLE
            if (!exist) // if dont exist row for this table, i insert new row
                sqlCommand = "INSERT INTO tKosikZbozi (zboziID, zboziPocet, kosikID) VALUES (@idZbozi , @pocetZbozi, @id)";
            else // if exist, i update exist row
                sqlCommand = "UPDATE tKosikZbozi SET zboziPocet += @pocetZbozi WHERE zboziID = @idZbozi AND kosikID = @id";
            SqlCommand insertOrUpdate = new SqlCommand(sqlCommand, connection);
            insertOrUpdate.Parameters.Add("@idZbozi", SqlDbType.Int).Value = ProductID;
            insertOrUpdate.Parameters.Add("@pocetZbozi", SqlDbType.Int).Value = ProductNumber;
            insertOrUpdate.Parameters.Add("@id", SqlDbType.Int).Value = this.cookie.Values["id"];
            connection.Open();
            insertOrUpdate.ExecuteNonQuery(); // And here is problem and i dont know how :-(
            connection.Close();

here are tables:

in public Cart() initialization is:

// create cart in tKosik
                string sqlCommand = "INSERT INTO tKosik (casPristupuKosik, randomIDKosik) values(@date, @id)";
                SqlCommand insert = new SqlCommand(sqlCommand, connection);
                insert.Parameters.Add("@date", SqlDbType.VarChar,14).Value = this.cookie.Values["date"];
                insert.Parameters.Add("@id", SqlDbType.Int).Value = this.cookie.Values["randomIDKosik"];
                connection.Open();
                insert.ExecuteNonQuery();
                connection.Close();

                // SELECT idKosik and add to cookies
                sqlCommand = "SELECT idKosik FROM tKosik WHERE randomIDKosik = @ID AND casPristupuKosik = @date";
                SqlCommand select = new SqlCommand(sqlCommand, connection);
                select.Parameters.Add("@ID", SqlDbType.Int).Value = this.cookie.Values["randomIDKosik"];
                select.Parameters.Add("@date", SqlDbType.VarChar, 14).Value = this.cookie.Values["date"];
                connection.Open();
                this.cookie.Values.Add("id", select.ExecuteScalar().ToString());
                connection.Close();

Maybe I can add to n-table (in 1:n relation) somehow through the parent table? But how?

来源:https://stackoverflow.com/questions/5560564/the-insert-statement-conflicted-with-the-foreign-key-constraint-fk-tkosikzbozi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!