foreach (DataRow dr in dtData.Rows.Cast<DataRow>().ToList())

解决方案 »

  1.   


    foreach里面有代码对dtdata进行添加和移除的操作,这是不允许的。
    你可以用中间变量记录一下要增加和移除的对象,循环结束后在进行添加和移除操作
      

  2.   

    经过#1的方法改程序后,执行到byte[] groupid = HexStrToHexBytes(dr["Group_name"].ToString());会提示错误:“此行已从表中移除并且没有任何数据。BeginEdit() 将允许在此行中创建新数据”,这是怎么回事呢?
      

  3.   

    你没贴完吧?是不是还有对dtData修改或者增删的代码?
      

  4.   

    问题比较多,有些信息你没有提供,比如:是刚进入这个 foreach 的时候报错了,还是在 foreach 内部执行到某一句话的时候报错了?代码要贴完整,有些你可能出错的都不贴出来。
    而且你…………没有单步调试吗?先单步调试一下,找到真正出错的语句再说吧!
      

  5.   

    就是在foreach 内部执行到byte[] groupid = HexStrToHexBytes(dr["Group_name"].ToString());这句时会提示错误,单步调试也是到这步出错,这怎么回事呢?好像后面没有对dtData修改或者增删的代码了,下面我把这部分程序完整贴一下
     
      

  6.   

    if (EMMSEND && IfChannelSetup && IfStreamSetup)  // 时间到,通道建立,流建立
                    //if (EMMSEND)
                    {
                        // if (receiveflag == true)
                        // {                    string ConnMdbStr = "Data Source='" + txtsqlip.Text + "' ;Initial Catalog='" + dataname.Text + "';User ID=sa;Password='" + txtpassword.Text + "'";
                        SqlConnection ConnMdb = new SqlConnection(ConnMdbStr);
                        string SqlText = "SELECT * FROM Group_SGK3 ";
                        SqlDataAdapter da = new SqlDataAdapter(SqlText, ConnMdb);
                        try
                        {
                            //da.Fill(dt);
                            dtData.Clear();
                            da.Fill(dtData);                    }
                        catch
                        {
                            break;
                        }
                        //}                                       foreach (DataRow dr in dtData.Rows.Cast<DataRow>().ToList())     //遍历行 in 行的集合
                        {
                            byte[] EDS = new byte[183];
                            EDS[0] = 0x82;  // emm table id
                            EDS[1] = 0x40;   // 
                            EDS[2] = 0x3f;
                            EDS[3] = 0x02;// emm secton length
                            byte[] groupid = HexStrToHexBytes(dr["Group_name"].ToString());
                            EDS[4] = groupid[0];
                            EDS[5] = groupid[1];
                            // emm secton length
                            //发送emm                       
                           
                            string strConn = "Data Source=127.0.0.1;Initial Catalog=zy;User ID=sa;Password=229107my229107m";
                            SqlConnection ConnAcc = new SqlConnection(strConn);
                            string strSQL = "SELECT * FROM Group_SGK3 ";
                            SqlDataAdapter de = new SqlDataAdapter(strSQL, ConnMdb);
                            try
                            {
                                //da.Fill(dt);
                                dtData.Clear();
                                de.Fill(dtData);                        }
                            catch
                            {
                                break;
                            }
                                for (int n = 0; n < 7; n++)
                                {
                                    int GK;
                                    foreach (DataRow rdr in dtData.Rows)
                                    {                                    int SGK = Convert.ToInt32(rdr["SGK"]);
                                        GK = SGK << n;                                    
                                            if (Convert.ToBoolean(rdr["state1"]))
                                            {
                                                                                            byte[] gk = new byte[8];
                                                Array.Copy(HexStrToHexBytes(GK.ToString()), 0, gk, 7, 1);
                                                
                                                SKeyValue = SKeyValue1;
                                                byte[] EncryptSk = EncryptDES(SKeyValue, gk);
                                                                                            EDS[6] = 0x01;
                                                Array.Copy(EncryptSk, 0, EDS, 7, EncryptSk.Length);
                                                //Thread.Sleep(5);
                                            }
                                            else
                                            {
                                                // byte[] EDS = new byte[183];                                            EDS[6] = 0x01;
                                                
                                                EDS[7] = 0xff;
                                                EDS[8] = 0xff;
                                                for (int i = 9; i < 15; i++)
                                                {
                                                    EDS[i] = 0x00;                                            }                                        }
                                            if (Convert.ToBoolean(rdr["state2"]))
                                            {
                                                
                                                byte[] gk = new byte[8];
        Array.Copy(HexStrToHexBytes(GK.ToString()), 0, gk, 7, 1);// pdk buf: 0,0,0,0,0,0,0,0xcd
                                                
                                                SKeyValue = SKeyValue2;
                                                byte[] EncryptSk = EncryptDES(SKeyValue, gk);
                                               
                                                EDS[15] = 0x02;
                                                Array.Copy(EncryptSk, 0, EDS, 16, EncryptSk.Length);                                        }
                                            else
                                            {
                                               
                                                EDS[16] = 0xff;
                                                EDS[17] = 0xff;
                                                for (int i = 18; i < 24; i++)
                                                {
                                                    EDS[i] = 0x00;                                            }
                                            }
                                            if (Convert.ToBoolean(rdr["state3"]))
                                            {
                                                
                                                byte[] gk = new byte[8];
          Array.Copy(HexStrToHexBytes(GK.ToString()), 0, gk, 7, 1);// pdk buf: 0,0,0,0,0,0,0,0xcd
                                               
                                                SKeyValue = SKeyValue3;
                                                byte[] EncryptSk = EncryptDES(SKeyValue, gk);
                                               
                                                EDS[24] = 0x03;
                                                Array.Copy(EncryptSk, 0, EDS, 25, EncryptSk.Length);                                        }
                                           
      

  7.   

     else
                                            {                                            
                                             
                                                EDS[24] = 0x03;
                                                
                                                EDS[25] = 0xff;
                                                EDS[26] = 0xff;
                                                for (int i = 27; i < 33; i++)
                                                {
                                                    EDS[i] = 0x00;                                            }                                        }
                                            if (Convert.ToBoolean(rdr["state4"]))
                                            {
                                                                                        byte[] gk = new byte[8];
                 Array.Copy(HexStrToHexBytes(GK.ToString()), 0, gk, 7, 1);
                                               
                                                SKeyValue = SKeyValue4;
                                                byte[] EncryptSk = EncryptDES(SKeyValue, gk);
                                                                                            EDS[33] = 0x04;
                                                Array.Copy(EncryptSk, 0, EDS, 34, EncryptSk.Length);
                                                Thread.Sleep(5);
                                            }
                                            else
                                            {
                                                
                                               
                                                EDS[34] = 0xff;
                                                EDS[35] = 0xff;
                                                for (int i = 36; i < 42; i++)
                                                {
                                                    EDS[i] = 0x00;                                            }                                        }
                                            for (int i = 42; i < 183; i++)
                                            {
                                                EDS[i] = 0xff;                                        }                MsgSend(TcpSocket, (int)Msg_Type_Value.Data_provision, SendBytes, EDS);
                                            InsertMsgLineWithTime("分组用户授权信息发送");
                                            Thread.Sleep(10);                                    }
                                    
                                }
                        
                            
                          
                        }
                        string SqlTex = "SELECT * FROM User_S2 ";
                        SqlDataAdapter ds = new SqlDataAdapter(SqlTex, ConnMdb);
                        try
                        {
                            //da.Fill(dt);
                            dtData.Clear();
                            ds.Fill(dtData);
                        }
                        catch
                        {
                            break;
                        }
                        foreach (DataRow dr in dtData.Rows)
                        {
                            byte[] EDS = new byte[183];
                            EDS[0] = 0x82;  // emm table id
                            EDS[1] = 0x40;   // 
                            EDS[2] = 0x3f;
                            EDS[3] = 0x01;
                            byte[] pdk = new byte[8];
                            Array.Copy(HexStrToHexBytes(dr["pdk"].ToString()), 0, pdk, 7, 1);// pdk buf: 0,0,0,0,0,0,0,0xcd
                            byte[] sgroupid = HexStrToHexBytes(dr["SGroup_name"].ToString());
                            byte[] SGK = new byte[8];
                            Array.Copy(HexStrToHexBytes(dr["SGK"].ToString()), 0, SGK, 7, 1);
                            byte[] EncryptSk = EncryptDES(SGK, pdk);
                            byte[] sgroup_id = HexStrToHexBytes(dr["SGroup_id"].ToString());
                            EDS[4] = (byte)sgroupid[0];
                            EDS[5] = (byte)sgroupid[1];
                            EDS[6] = (byte)sgroup_id[0];
                            EDS[7] = (byte)sgroup_id[1];
                            Array.Copy(EncryptSk, 0, EDS, 8, EncryptSk.Length);
                            int i;
                            for (i = 8 + EncryptSk.Length; i < EDS.Length; i++)
                            {
                                EDS[i] = 0xff;                        }
                            MsgSend(TcpSocket, (int)Msg_Type_Value.Data_provision, SendBytes, EDS);
                            InsertMsgLineWithTime("超组用户分组数据发送");
                            Thread.Sleep(10);                    }
                        SqlText = "SELECT * FROM stack ";
                         ds = new SqlDataAdapter(SqlTex, ConnMdb);
                        try
                        {
                            //da.Fill(dt);
                            dtData.Clear();
                            ds.Fill(dtData);
                        }
                        catch
                        {
                            break;
                        }
                        foreach (DataRow dr in dtData.Rows)
                        {
                            break;
                        
                        }                    EMMSEND = false;
                    }