简体   繁体   中英

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:

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. 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}

Issue seems to be on this line. Convert.ToInt32(cmbEmp.SelectedValue) . Make sure it parsed correctly and parsed it before using in linq query. 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
        };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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