none
新手关于linq问题5 RRS feed

  • 问题

  • 以下代码中,如果在数据库 db.charactars表中当遇到行里列为permission1~9有null的空值时就会出错(如果行一个行里没有null的话是正常的),停在db.SubmitChanges();提示弹出“找不到行,或行已更改”这提示,求教高手是什么原因,有什么解决办法?谢谢!
    public string replace(string a)
            {
                string b="";
                if (a != null)
                {
                    int bg = a.IndexOf(DropDownList3.SelectedValue + ":");
                    int be = a.IndexOf(";", bg + 1);
                
                if (bg != -1)
                {
                    b = a.Replace(a.Substring(bg, be - bg + 1), DropDownList3.SelectedValue + ":" + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList1") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList2") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList3") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList4") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList5") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList6") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList7") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList8") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList9") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList10") as DropDownList).SelectedValue + ";");
                   
                }
                else { b = a + DropDownList3.SelectedValue + ":" + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList1") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList2") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList3") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList4") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList5") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList6") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList7") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList8") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList9") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList10") as DropDownList).SelectedValue + ";"; }
                } 
                else { b =  DropDownList3.SelectedValue + ":" + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList1") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList2") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList3") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList4") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList5") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList6") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList7") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList8") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList9") as DropDownList).SelectedValue + (this.charactarpermissionlistWebUserControl11.FindControl("DropDownList10") as DropDownList).SelectedValue + ";"; }
                    return b;
            }
            protected void Button4_Click(object sender, EventArgs e)
            {
                DataClasses1DataContext db = new DataClasses1DataContext();
                try
                    {
                                       
                         var query = from m in db.charactars
                                     where m.charactar == this.TextBox7.Text
                                     select m;
                         if (query.Count() > 0)
                         {
                            
                             var charactarc = db.charactars.Single(c => c.charactar == this.TextBox7.Text);
                             switch (this.DropDownList2.SelectedValue)
                             {
                                 case "permission1":
                                   
                                     charactarc.permission1 = replace(charactarc.permission1);
                                    break;
                                 case "permission2":
                                    charactarc.permission2 = replace(charactarc.permission2);
                                     break;
                                 default:
                                     break;
                             }
                             
                         }
                         else
                         {
                             var charactar1 = new charactars();
                             charactar1.charactar = this.TextBox7.Text;
                             switch (this.DropDownList2.SelectedValue)
                        {
                                     case "permission1":
                                       charactar1.permission1 = replace(charactar1.permission1); 
                                     break;
                                     case "permission2":
                                     charactar1.permission2 = replace(charactar1.permission2);
                                     break;
                   default:
                                  break;
                          }
                             db.charactars.InsertOnSubmit(charactar1);
                            
                         }
                    
            
                         db.SubmitChanges();
               
                this.GridView2.DataBind();
                this.DetailsView1.DataBind();
                this.Button4.Enabled = false;
                this.Label4.Text = DateTime.Now.ToString() + "创建/修改角色成功!";
                this.UpdatePanel1.Update();
               
                         
                    }
                catch (System.InvalidOperationException) { MessageBox.Show(this, "操作出错,可能角色重复了,或SQL错误"); this.GridView2.DataBind(); this.DetailsView1.DataBind(); return; }
            }

    • 已编辑 hzpemu 2012年1月18日 5:16
    2012年1月18日 5:15

答案

全部回复