检查数据是否存在:错误消息集合被修改;枚举操作不能执行

iyzzxitl  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(251)

我正在检查数据库表中是否存在一个密封编号,但是当我使用存储过程或其背后的存储过程时,它不起作用。存储过程生成错误消息,但如果它不存在,它会在值存在时告诉我没有提供值。从它背后的代码中可以看出这个错误:集合被修改了;枚举操作不能执行。
代码隐藏

protected void showData()
    {
        #region Seal
        SqlCommand R = new SqlCommand("PP_SealRecord", objConnection);
        R.CommandType = CommandType.StoredProcedure;
        R.Parameters.AddWithValue("@loadSheetNum", dispatchSheetNo);
        objConnection.Open();
        SqlDataReader Reader = R.ExecuteReader();

        int maximumTextBoxCount = 7;
        if (Reader.Read())
        {
            ControlCache = new List<Control>();
            phSealNum.Controls.Clear();

            for (int i = 0; i < maximumTextBoxCount; i++)
            {
                TextBox txt = new TextBox();

                string index = string.Format("seal{0}", i + 1);

                if (Reader[index] != DBNull.Value)
                {
                    txt.Text = (string)Reader[index];
                }
                else
                {

                    continue;
                }

                phSealNum.Controls.Add(txt);
                phSealNum.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                ControlCache.Add(txt);
                txt.Width = 100;
                //txt.Enabled = false;
            }
        }

        Reader.Close();
        objConnection.Close();
        #endregion

}

protected void Update_Click(object sender, EventArgs e)
        {
                #region Seal Data
                string str = null;
                int countseal = 0;

                foreach (TextBox textBox in phSealNum.Controls.OfType<TextBox>())
                {
                    string constr = ConfigurationManager.ConnectionStrings["TWCL_OPERATIONSConnectionString"].ConnectionString;

                    using (SqlCommand comm = new SqlCommand("PP_CountSeal"))
                    {
                        comm.Connection = con;
                        con.Open();
                        comm.CommandType = CommandType.StoredProcedure;

                        str = textBox.Text.TrimEnd();

                        comm.CommandType = CommandType.StoredProcedure;

                        string seal1 = string.Format("@seal{0}", countseal);
                        comm.Parameters.AddWithValue(seal1, str);

                        int count = (int)cmd.ExecuteScalar();
                        string name = HttpContext.Current.User.Identity.Name;

                        if (count > 0)
                        {
                            SqlCommand command = new SqlCommand("PP_SealRecord", con);
                            command.CommandType = CommandType.StoredProcedure;
                            command.Parameters.AddWithValue("@loadSheetNum", dispatchSheetNo);

                            SqlDataReader data = command.ExecuteReader();
                            if (data.Read())
                            {
                                string previousseal1 = Convert.ToString(data["seal1"]);

                                string previousseal2 = Convert.ToString(data["seal2"]);

                                string previousseal3 = Convert.ToString(data["seal3"]);

                                string previousseal4 = Convert.ToString(data["seal4"]);

                                string previousseal5 = Convert.ToString(data["seal5"]);

                                string previousseal6 = Convert.ToString(data["seal6"]);

                                string previousseal7 = Convert.ToString(data["seal7"]);
                                EditSeal(dispatchSheetNo, previousseal1, previousseal2, previousseal3, previousseal4, previousseal5, previousseal6, previousseal7, name);

                            }
                        }
                        else
                        {
                            // INSERT STATEMENT
                            CreateSeal(loadsheet, CreatedBy);
                        }

                        #endregion
}
//Creates history when seal is updated
    protected void InsertSealHistory()

        {
            SqlConnection con = new SqlConnection(connection); //SQL Connection
            con.Open();

            #region Get Seal Record
            SqlCommand cmd = new SqlCommand("PP_SealDataRecord", con);
            cmd.CommandType = CommandType.StoredProcedure; //SQL Command Type is Stored Procedure

            cmd.Parameters.AddWithValue("@loadsheetnum", dispatchSheetNo);
            SqlDataReader dr = cmd.ExecuteReader();
            string sealNumber = "";
            string CreatedBy = "";
           DateTime? DateCreated = null;

             while (dr.Read()) { 
             sealNumber = dr["sealNumber"].ToString();
             CreatedBy = dr["CreatedBy"].ToString();
             DateCreated = Convert.ToDateTime(dr["DateCreated"].ToString());

                }

            dr.Close();
            con.Close();
            #endregion

            #region Insert Seal History
            con.Open();
            SqlCommand I = new SqlCommand("PP_CreateSealDataHistory", con);
            I.CommandType = CommandType.StoredProcedure;

            I.Parameters.AddWithValue("@LoadSheetNum", dispatchSheetNo);
            I.Parameters.AddWithValue("@SealNumber", sealNumber);
            I.Parameters.AddWithValue("@CreatedBy", CreatedBy);
            I.Parameters.AddWithValue("@DateCreated",DateCreated);

            I.ExecuteNonQuery();

            con.Close();
            #endregion
        }

        protected void EditSeal(string num, string a, string b, string c, string d, string e, string f, string g, string user)
        {
            SqlConnection con = new SqlConnection(connection); //SQL Connection
            con.Open();
            int maxPossibleTextBoxCount = 7;
            int selectedTextBoxCount = phSealNum.Controls.OfType<TextBox>().Count();
            int emptyTextBoxCount = maxPossibleTextBoxCount - selectedTextBoxCount;

            SqlCommand U = new SqlCommand("PP_updateSeal", con);
            U.CommandType = CommandType.StoredProcedure;

            U.Parameters.AddWithValue("@loadsheetNum", num);

            foreach (TextBox textBox in phSealNum.Controls.OfType<TextBox>())
            {
                if (!Regex.IsMatch(textBox.Text.Replace(" ", ""), @"(^([0-9]*|\d*\d{1}?\d*)$)"))
                {
                    lblError.Text = "Please enter only numeric values for seal number";
                    return;
                }
                else if (textBox.Text == "")
                {
                    lblError.Text = "Please enter seal number";
                    return;
                }
                else
                {
                    countSeal += 1;

                    Session["CountSeal"] = countSeal;
                    if (countSeal == 1)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, a);
                    }
                    else if (countSeal == 2)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, b);
                    }
                    else if (countSeal == 3)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, c);
                    }
                    else if (countSeal == 4)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, d);
                    }
                    else if (countSeal == 5)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, e);
                    }
                    else if (countSeal == 6)
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, f);
                    }

                    else
                    {
                        string seal = string.Format("@previous_seal{0}", countSeal);
                        U.Parameters.AddWithValue(seal, g);
                    }
                    string seal1 = string.Format("@seal{0}", countSeal);
                    U.Parameters.AddWithValue(seal1, textBox.Text);

                }
            }

            // Here we add the parameters for the non-selected textboxes.
            if (emptyTextBoxCount > 0)
            {

                for (int i = 0; i < emptyTextBoxCount; i++)
                {
                    countSeal += 1;
                    Session["CountSeal"] = countSeal;
                    string seal = string.Format("@previous_seal{0}", countSeal);
                    string seal1 = string.Format("@seal{0}", countSeal);
                    U.Parameters.AddWithValue(seal, System.DBNull.Value);
                    U.Parameters.AddWithValue(seal1, System.DBNull.Value);
                }
            }

            U.Parameters.AddWithValue("@lastUser", user);
            //U.Parameters.Add("@outmessage", SqlDbType.Char, 500);

            //U.Parameters["@outmessage"].Direction = ParameterDirection.Output;
            U.ExecuteNonQuery();

            //lblError.Text = (string)U.Parameters["@outmessage"].Value;

            con.Close();

            showData();
        }

        protected void CreateSeal(string num, string user)
        {
            SqlConnection con = new SqlConnection(connection); //SQL Connection
            con.Open();

            SqlCommand U = new SqlCommand("PP_CreateSealNumber", con);
            U.CommandType = CommandType.StoredProcedure;
            int counter = 1;
            int maxPossibleTextBoxCount = 7;
            int selectedTextBoxCount = phSealNum.Controls.OfType<TextBox>().Count();
            int emptyTextBoxCount = maxPossibleTextBoxCount - selectedTextBoxCount;
            foreach (TextBox textBox in phSealNum.Controls.OfType<TextBox>())
            {
                if (!Regex.IsMatch(textBox.Text, @"(^([0-9]*|\d*\d{1}?\d*)$)"))
                {
                    lblError.Text = "Please enter only numeric values for seal number";
                    return;
                }
                else
                {
                    string seal = string.Format("@seal{0}", counter++);
                    //command.Parameters.AddWithValue(seal, textBox.Text);
                    U.Parameters.AddWithValue(seal, textBox.Text);
                }
            }

            // Here we add the parameters for the non-selected textboxes.
            if (emptyTextBoxCount > 0)
            {
                for (int i = 0; i < emptyTextBoxCount; i++)
                {
                    string seal = string.Format("@seal{0}", counter++);
                    //command.Parameters.AddWithValue(seal, textBox.Text);
                    U.Parameters.AddWithValue(seal, DBNull.Value);
                    //command.Parameters.AddWithValue($"@seal{counter++}", DBNull.Value);

                }
            }
            U.Parameters.AddWithValue("@loadsheetNum", num);

            U.Parameters.AddWithValue("@lastUser", user);
            U.ExecuteNonQuery();

            con.Close();

            showData();
        }

protected void TotalSeal_SelectedIndexChanged(object sender, EventArgs e)
    {
        populate();
    }

    //Populates the amount of textbox based on the value selected from the drop down
    public void populate()
    {
        //ControlCache = new List<Control>();
        //phSealNum.Controls.Clear();

        int targetCount = Convert.ToInt32(TotalSeal.SelectedItem.Value);
        int currentItems = phSealNum.Controls.OfType<TextBox>().Count();
        int totalitems = targetCount - currentItems;
        if (totalitems <= 7)
        {
            for (int i = 0; i < totalitems; i++)
            {

                TextBox tx = new TextBox();
                tx.MaxLength = 10;
                tx.Width = 100;
                phSealNum.Controls.Add(tx);
                phSealNum.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

                ControlCache.Add(tx);
            }
        }
        else
        {
            lblError.Text = targetCount + " exceeds number of seals";
        }
    }
ia2d9nvy

ia2d9nvy1#

在循环中迭代时,集合phsealnum.controls.oftype()是否被修改?请检查一下,让我知道。

相关问题