C# 尝试保存到SQL数据库时参数异常

C# 尝试保存到SQL数据库时参数异常,c#,sql,C#,Sql,无法对DataRow列执行验证。后面的柱子 end允许null,但仍引发异常。当我试图用cellNumber.Text中的null值保存时,它不应该引发异常,我试图用if语句验证,但也不起作用。请帮忙 private void btnSave_Click(object sender, EventArgs e) { DataRow dr = dt.NewRow(); dr["FirstName"] = txtFirstName.Text; dr["LastName"] =

无法对
DataRow
列执行验证。后面的柱子 end允许
null
,但仍引发异常。当我试图用
cellNumber.Text中的
null
值保存时,它不应该引发异常,我试图用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");
    }
}

你可能检查错了东西。你有这个:

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”]的内容更有意义。

没有SQL数据库——SQL只是结构化查询语言——许多数据库系统都使用这种语言,但不是数据库产品。。。我们真的需要知道您使用的是什么数据库系统(以及哪个版本)……他使用的是sql server。他将其标记为c#,而.net程序员通常使用术语sql数据库,而不是ms sql或sql server。