1。在我们学程序中90%的错误都是由于null引起的。
注意点:a.如果不能够保证每个字段都有数据,最好要做一个判断是否为Null
b.如果为Null的时候进行tostring()转换就会报错。如果本来就是string类型的就没有必要进行tostring的转换。
c.写个通用的数据转换函数来判断避免这种情况。(从数据库中读取的数据都是object类型)可以进行强转。(string)字段。
d.在数据更新操作过程中的一个误区。数据加载的时候不要构造新对象,而应该将原对象提起出来。对需要更新的子对进行赋值,没有被赋值的字段将保持原来的值不变。
eg: int tid = Convert.ToInt32(Request["tid"]);
lenovo_User user = ServiceLocator.UserService.LoadUser(tid);
//1、Personal information
user.P_Affiliation = Convert.ToInt32(this.ddlAffiliation.SelectedValue.ToString());
user.P_FirstName = this.tbFirstName.Value;
user.P_LastName = this.tbLastName.Value;
user.P_FirstName_E = this.tbFirstName_E.Value;
user.P_LastName_E = this.tbLastName_E.Value;
user.P_Dateofbirth = this.tbDobYear.Value + "-" + this.ddlDobMonth.Value + "-" + this.ddlDobDay.Value;
user.P_Nationality = this.ddlNationality.Value;
user.P_Ethnicity = this.ddlEthnicity.Value;
更新操作的时候应该这样执行,走出以前的误区。
2。关于数据回绑的解决办法:(多文本框,dropdownList,checkboxlist的解决方案)
//回绑E_Mobilephone(多文本框)
string[] E_MobileArray;
E_MobileArray = user.E_Mobilephone.ToString().Split('-');
this.tbMobilePhone1_EC.Value = E_MobileArray[0].ToString();
this.tbMobilePhone2_EC.Value = E_MobileArray[1].ToString();
this.tbMobilePhone3_EC.Value = E_MobileArray[2].ToString();
//回绑Date of birth(dropdownlist)
string[] DateArray;
DateArray = user.P_Dateofbirth.ToString().Split('-');
this.tbDobYear.Value = DateArray[0].ToString();
this.ddlDobMonth.Value = DateArray[1].ToString();
this.ddlDobDay.Value = DateArray[2].ToString();
//回绑Special needs(checkboxlist)
string[] SpecialArray = user.L_Special.ToString().Split('-');
//绑定Other选项
this.tbSpecialNeeds7.Value = SpecialArray[1].ToString();
string[] ItemArray = SpecialArray[0].ToString().Split('|');
for (int i = 0; i < this.cbl_Special.Items.Count; i++)
{
string sItem = this.cbl_Special.Items[i].ToString().Trim();
for (int j = 0; j < ItemArray.Length - 1; j++)
{
string cItem = ItemArray[j].ToString();
if (sItem == cItem)
{
this.cbl_Special.Items[i].Selected = true;
}
}
}
注:原理:将数据库中的数据取出之后赋值给数组,然后在跟checkboxlist中的字段进行循环对比,相等的话则选中。