2013-02-03 119 views
0

無法對DataRow列執行驗證。後面 結束的列允許null,但仍然拋出異常。當我試圖用null的值保存cellNumber.Text時,它不應該拋出異常,我試圖用if語句進行驗證,但也沒有工作。請幫忙。嘗試保存到SQL數據庫時參數異常

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

有沒有這樣的事情作爲一個** SQL數據庫** - SQL只是**結構化查詢語言** - 許多數據庫系統使用的語言,但不是數據庫產品...我們真的需要知道你正在使用什麼數據庫系統(和哪個版本).... –

+0

美元甜甜圈他使用SQL服務器。他被標記爲c#和.net程序員經常使用術語sql數據庫而不是ms sql或sql server。 –

回答

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的內容,而不是博士[「CellNumber」。

相關問題