尝试保存到SQL数据库时出现参数异常

时间:2013-02-03 13:35:39

标签: c# sql

无法在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");
    }
}

1 个答案:

答案 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”]更有意义。