无法在DataRow
列上执行验证。后面的列
end允许null
,但仍然会抛出异常。当我尝试在null
中使用cellNumber.Text
值进行保存时,它不应该抛出异常,我尝试使用if语句进行验证但是也没有用。请帮忙。
private void btnSave_Click(object sender, EventArgs e)
{
DataRow dr = dt.NewRow();
dr["FirstName"] = txtFirstName.Text;
dr["LastName"] = txtLastName.Text;
dr["Shirt"] = txtShirt.Text;
dr["Pant"] = txtPant.Text;
if (dr.IsNull("CellNumber"))
{
MessageBox.Show("Please enter Cell number");
}
else
{
dr["CellNumber"] = txtCellNo.Text; //Argument exception is thrown here
}
dr["DueDate"] = txtDueDate.Text;
dr["Date"] = txtDate.Text;
dt.Rows.Add(dr);
try
{
da.Update(ds, "Measurement");
}
catch (DBConcurrencyException ex)
{
MessageBox.Show(ex.Message);
dt.Clear();
da.Fill(ds, "Measurement");
}
finally
{
MessageBox.Show("Success");
}
}
答案 0 :(得分:0)
你可能正在检查错误的东西。你有这个:
if (dr.IsNull("CellNumber"))
{
MessageBox.Show("Please enter Cell number");
}
else
{
dr["CellNumber"] = txtCellNo.Text;
}
dr["CellNumber"] = txtCellNo.Text;//Argument exception is thrown here
检查txtCellNo.Text的内容而不是dr [“CellNumber”]更有意义。