无法填充数据库表

时间:2013-03-27 15:18:07

标签: c#

protected void ButtonAddDatabase_Click(object sender, EventArgs e)
{
    using (KnowItCvdbEntities db = new KnowItCvdbEntities())
    {
        SPWeb theSite = SPControl.GetContextWeb(Context);
        SPUser theUser = theSite.CurrentUser;
        string strUserName = theUser.LoginName;

        var theEmplDbSkill = (
                        from p
                        in db.EMPLOYEES
                        where p.username == strUserName
                        select p).FirstOrDefault();

        _emp = theEmplDbSkill;

        if (_emp != null)
        {                        
            foreach (var vItem in ListBoxDatabases.Items)
            {
                if (ValueAlreadyInListDb(vItem))
                    return;
            }                        

      //Get existing skilllevel from db
      var skillLevel = (from sL in db.TECHNICAL_SKILL_LEVEL 
                        where sL.technical_skill_level_id == Convert.ToInt32(DropDownListDB.SelectedValue)
                        select sL).FirstOrDefault();

       //Get existing skillvalue from db
        var skillValue = (from sV in db.TECHNICAL_SKILLS
                         where sV.technical_skill_id == Convert.ToInt32(RadioButtonListDatabase.SelectedValue)
                         select sV).FirstOrDefault();

       //Adding to employees_technical_skills table
       var empSkill = new EMPLOYEES_TECHNICAL_SKILLS
       {
            technical_skill_id = Convert.ToInt32(DropDownListDB.SelectedItem.Value),
            TECHNICAL_SKILLS = skillValue,
            technical_skill_level_id = Convert.ToInt32(RadioButtonListDatabase.SelectedItem.Value)
            TECHNICAL_SKILL_LEVEL = skillLevel,
            employee_id = _emp.employee_id
        }

            _emp.EMPLOYEES_TECHNICAL_SKILLS_Add(empSkill);
            db.SaveChanges();

            _emp.EMPLOYEES_TECHNICAL_SKILLS.Add(empSkill);
            db.SaveChanges();

        }                
    }
}    

现在我正在填写下表: enter image description here

这是我的表格: enter image description here

但我一直收到错误:

  

LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法,并且此方法无法转换为商店表达式。

此致 克里斯蒂安。

1 个答案:

答案 0 :(得分:0)

看起来Entity框架无法将Convert.ToInt32()转换为SQL表达式。我会首先将radiobutton和dropdown值转换为内存中的int,然后将它们传递给查询:

int TechnicalSkillLevelID = int.Parse(DropDownListDB.SelectedValue);
int TechnicalSkillID = int.Parse(RadioButtonListDatabase.SelectedValue);
//Get existing skilllevel from db
      var skillLevel = (from sL in db.TECHNICAL_SKILL_LEVEL 
                        where sL.technical_skill_level_id == TechnicalSkillLevelID)
                        select sL).FirstOrDefault();

       //Get existing skillvalue from db
        var skillValue = (from sV in db.TECHNICAL_SKILLS
                         where sV.technical_skill_id == TechnicalSkillID)
                         select sV).FirstOrDefault();

       //Adding to employees_technical_skills table
       var empSkill = new EMPLOYEES_TECHNICAL_SKILLS
       {
            technical_skill_id = skillValue.technical_skill_id,
            TECHNICAL_SKILLS = skillValue,
            technical_skill_level_id = skillLevel.technical_skill_level_id)
            TECHNICAL_SKILL_LEVEL = skillLevel,
            employee_id = _emp.employee_id
        }

此外,由于您已经撤回了技能水平和技能值,因此无需从下拉/单选按钮列表中获取值。只需使用skillValueskillLevel个对象。