日期:2014-05-17  浏览次数:20457 次

循环判断后添加数据出现丢失情况,盼帮忙
C# code
private void AddUserWork(int id)
        {
            if (string.IsNullOrEmpty(txtcompany1.Text))
            {
                action = 1;
            }
            try
            {
                for (int i = 1; i < 11; i++)
                {
                    string companyid = "txtcompany" + i.ToString();
                    string caddressid = "txtcaddress" + i.ToString();
                    string positionid = "txtposition" + i.ToString();
                    string salarid = "txtsalar" + i.ToString();
                    string prizeid = "txtprize" + i.ToString();
                    string contactid = "txtcontact" + i.ToString();
                    string ctelid = "txtctel" + i.ToString();
                    string causeid = "txtcause" + i.ToString();
                    string enteryearid = "ddlenteryear" + i.ToString();
                    string entermonthid = "ddlentermonth" + i.ToString();
                    string leaveyearid = "ddlleaveyear" + i.ToString();
                    string leavemonthid = "dllleavemonth" + i.ToString();

                    TextBox txtcompany = (TextBox)Panel3.FindControl(companyid);
                    TextBox txtcaddress = (TextBox)Panel3.FindControl(caddressid);
                    TextBox txtposition = (TextBox)Panel3.FindControl(positionid);
                    TextBox txtsalar = (TextBox)Panel3.FindControl(salarid);
                    TextBox txtprize = (TextBox)Panel3.FindControl(prizeid);
                    TextBox txtcontact = (TextBox)Panel3.FindControl(contactid);
                    TextBox txtctel = (TextBox)Panel3.FindControl(ctelid);
                    TextBox txtcause = (TextBox)Panel3.FindControl(causeid);
                    DropDownList ddlenteryear = (DropDownList)Panel3.FindControl(enteryearid);
                    DropDownList ddlentermonth = (DropDownList)Panel3.FindControl(entermonthid);
                    DropDownList ddlleaveyear = (DropDownList)Panel3.FindControl(leaveyearid);
                    DropDownList dllleavemonth = (DropDownList)Panel3.FindControl(leavemonthid);

                    if (!string.IsNullOrEmpty(txtcompany.Text))
                    {
                        string edudate = "";
                        string graduate = "";
                        edudate = ddlenteryear.SelectedValue + "-" + ddlentermonth.SelectedValue + "-01";
                        graduate = ddlleaveyear.SelectedValue + "-" + dllleavemonth.SelectedValue + "-01";

                        if (!UserInfoDB.AddUserWork(id, txtcompany.Text, txtcaddress.Text, txtposition.Text, Convert.ToDateTime(edudate), Convert.ToDateTime(graduate), txtsalar.Text, txtcause.Text, txtcontact.Text, txtctel.Text, txtprize.Text))
                        {
                            action = 1;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
        }


这里调用DB层的UserInfoDB.AddUserWork方法添加用户数据到数据表,为什么有时候丢失数据,有时候添加用户数据成功?(就是添加数据的时候会丢失一些用户的数据)
急盼大家帮帮忙看下!谢谢

------解决方案--------------------
丢数据是怎样的呢?是全部添加成功了,但是有些信息加进去的不完整?还是有的成功了,而有的根本就没有进库?

如果是前者,可能的原因就是数据库某些字段的长度设置不够,被自动截取了

如果是后者,你要确保是否每次循环都能保证进入If判断,也就是txtcompany.Text不为空,另外就是是否存在异常.