简体   繁体   English

Linq错误“输入字符串的格式不正确。”

[英]Linq error “Input string was not in a correct format.”

My query looks like this: 我的查询如下所示:

var rport = from exc in db.Exception_Datas
            join emp in db.Emp_infos on exc.Emp_id equals emp.ID
            where (exc.Action_date >= frm && exc.Action_date <= to) && 
                   emp.Branch == cmbBranch.SelectedValue && 
                   emp.Dept == cmbDept.SelectedValue && 
                   emp.ID == Convert.ToInt32(cmbEmp.SelectedValue)
            select new
            {
                emp.Emp_name,
                emp.ID,
                emp.Designation,
                emp.Dept,
                emp.Branch,
                exc.Action_date
            };

I am contracting cmbEmp.Items like this: 我正在签约像这样的cmbEmp.Items:

var allEmp = from emp in db.Emp_infos select emp;
myItem.Text = "--Select--";
myItem.Value = "0";
cmbEmp.Items.Add(myItem);
foreach (var semp in allEmp)
{
    myItem = new RadComboBoxItem();
    myItem.Text = semp.Emp_name.ToString();
    myItem.Value = semp.ID.ToString();
    cmbEmp.Items.Add(myItem);
} 

I've followed some other question and post in SO and out of SO. 我已经关注了其他一些问题并在SO中发布了SO。 But any of them didn't helped me solve the problem. 但他们中的任何一个都没有帮助我解决问题。 I am getting this error: 我收到此错误:

{"Input string was not in a correct format."} System.SystemException {System.FormatException} {“输入字符串的格式不正确。”} System.SystemException {System.FormatException}

Issue seems to be on this line. 问题似乎就在这条线上。 Convert.ToInt32(cmbEmp.SelectedValue) . Convert.ToInt32(cmbEmp.SelectedValue) Make sure it parsed correctly and parsed it before using in linq query. 在使用linq查询之前,请确保正确解析并解析它。 Try like this 试试这样吧

int empId;

if (Int32.TryParse(cmbEmp.SelectedValue, out empId))
{
    var rport = from exc in db.Exception_Datas
        join emp in db.Emp_infos on exc.Emp_id equals emp.ID
        where (exc.Action_date >= frm && exc.Action_date <= to) && 
               emp.Branch == cmbBranch.SelectedValue && 
               emp.Dept == cmbDept.SelectedValue && 
               emp.ID == empId
        select new
        {
            emp.Emp_name,
            emp.ID,
            emp.Designation,
            emp.Dept,
            emp.Branch,
            exc.Action_date
        };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM