2013-05-02 38 views
0

使用Visual Studio .NET 2003的Im時,添加按鈕在所有文本框,組合框字段都填充數據時工作正常,但是在測試時未填充數據字段時將數據留空NULL它返回錯誤說「字符串未被識別爲有效的DateTime」允許將空值保存在數據庫中

我有一個名爲txtPurchasedDate

我的存儲過程

CREATE PROCEDURE AddOfficeEquipmentProfile 
    (
    @OE_ID  varchar(11)  = NULL, 
    @OE_Category  char(3)   = NULL, 
    @OE_SubCategory char(3)   = NULL, 
    @OE_Name  varchar(35)  = NULL, 
    @OE_User  varchar(35)  = NULL, 
    @OE_Brand  varchar(15)  = NULL, 
    @OE_Model  varchar(35)  = NULL, 
    @OE_Specs  varchar(1000)  = NULL, 
    @OE_SerialNo  varchar(35)  = NULL, 
    @OE_PropertyNo varchar(35)  = NULL, 
    @OE_MacAddress varchar(100)  = NULL, 
    @OE_Static_IP  varchar(15)  = NULL, 
    @OE_Vendor  varchar(35)  = NULL, 
    @OE_PurchaseDate smalldatetime  = NULL, 
    @OE_WarrantyInclusiveYear int  = NULL, 
    @OE_WarrantyStatus char(2)   = NULL, 
    @OE_Status  varchar(15)  = NULL, 
    @OE_Dept_Code char(3)   = NULL, 
    @OE_Location_Code char(8)   = NULL, 
    @OE_Remarks  varchar(1000)  = NULL 
    ) 
    AS 

    INSERT INTO tblOfficeEquipmentProfile (OE_ID, OE_Category, OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo, 
    OE_PropertyNo, OE_MacAddress, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code, 
    OE_Location_Code, OE_Remarks) 
    VALUES (@OE_ID, @OE_Category, @OE_SubCategory, @OE_Name, @OE_User, @OE_Brand, @OE_Model, 
    @OE_Specs, @OE_SerialNo, @OE_PropertyNo, @OE_MacAddress, @OE_Static_IP, @OE_Vendor, @OE_PurchaseDate, @OE_WarrantyInclusiveYear, @OE_WarrantyStatus, 
    @OE_Status, @OE_Dept_Code, @OE_Location_Code, @OE_Remarks) 

    IF @@ERROR<>0 
     BEGIN 
      ROLLBACK TRANSACTION 
      RETURN 0 
     END 
    ELSE 
     BEGIN 
      COMMIT TRANSACTION 
      RETURN 1 
     END 
    GO 

我Vb.net添加按鈕代碼

文本框
Dim cmd As SqlCommand = sqlconn.CreateCommand 
     sqlconn.Open() 
     cmd.CommandType = CommandType.StoredProcedure 
     cmd.CommandText = "AddOfficeEquipmentProfile" 

    cmd.Parameters.Add("@OE_ID", SqlDbType.VarChar, 11, "oeq-su-999") 
    cmd.Parameters.Add("@OE_Category", SqlDbType.Char, 3, "COM") 
    cmd.Parameters.Add("@OE_SubCategory", SqlDbType.Char, 3, "SU") 
    cmd.Parameters.Add("@OE_Name", SqlDbType.VarChar, 35, "adminpmis01") 
    cmd.Parameters.Add("@OE_User", SqlDbType.VarChar, 35, "Ivan") 
    cmd.Parameters.Add("@OE_Brand", SqlDbType.VarChar, 15, "DELL") 
    cmd.Parameters.Add("@OE_Model", SqlDbType.VarChar, 35, "optiplex") 
    cmd.Parameters.Add("@OE_Specs", SqlDbType.VarChar, 1000, "dualcore") 
    cmd.Parameters.Add("@OE_SerialNo", SqlDbType.VarChar, 35, "sgh5960") 
    cmd.Parameters.Add("@OE_PropertyNo", SqlDbType.VarChar, 35, "j7h7h6g6f2") 
    cmd.Parameters.Add("@OE_MacAddress", SqlDbType.VarChar, 100, "j7h7:h6g6f2") 
    cmd.Parameters.Add("@OE_Static_IP", SqlDbType.VarChar, 15, "192.168.1.5") 
    cmd.Parameters.Add("@OE_Vendor", SqlDbType.VarChar, 35, "ADWAYS") 

    cmd.Parameters.Add("@OE_PurchaseDate", SqlDbType.SmallDateTime) 
    cmd.Parameters.Add("@OE_WarrantyInclusiveYear", SqlDbType.Int) 
    cmd.Parameters.Add("@OE_WarrantyStatus", SqlDbType.Char, 2, "IN") 
    cmd.Parameters.Add("@OE_Status", SqlDbType.VarChar, 15, "Good") 
    cmd.Parameters.Add("@OE_Dept_Code", SqlDbType.Char, 3, "ADM") 
    cmd.Parameters.Add("@OE_Location_Code", SqlDbType.Char, 8, "ADM_OFC") 
    cmd.Parameters.Add("@OE_Remarks", SqlDbType.VarChar, 1000, "ACTIVE") 
    cmd.Parameters("@OE_ID").Value = txtOEID.Text 
    cmd.Parameters("@OE_Category").Value = cmbCategory.Text 
    cmd.Parameters("@OE_SubCategory").Value = cmbSubCategory.Text 
    cmd.Parameters("@OE_Name").Value = txtName.Text 
    cmd.Parameters("@OE_User").Value = txtUser.Text 
    cmd.Parameters("@OE_Brand").Value = cmbBrand.Text 
    cmd.Parameters("@OE_Model").Value = cmbModel.Text 
    cmd.Parameters("@OE_Specs").Value = txtSpecs.Text 
    cmd.Parameters("@OE_SerialNo").Value = txtSerialNo.Text 
    cmd.Parameters("@OE_PropertyNo").Value = txtPropertyNo.Text 
    cmd.Parameters("@OE_MacAddress").Value = txtMacAddress.Text 
    cmd.Parameters("@OE_Static_IP").Value = txtStaticIp.Text 
    cmd.Parameters("@OE_Vendor").Value = txtVendor.Text 
    cmd.Parameters("@OE_PurchaseDate").Value = txtPurchaseDate.Text 
    cmd.Parameters("@OE_WarrantyInclusiveYear").Value = txtWarrantyInclusiveYear.Text 
    cmd.Parameters("@OE_WarrantyStatus").Value = txtWarrantyStatus.Text 
    cmd.Parameters("@OE_Status").Value = txtStatus.Text 
    cmd.Parameters("@OE_Dept_Code").Value = cmbDeptCode.Text 
    cmd.Parameters("@OE_Location_Code").Value = cmbLocationCode.Text 
    cmd.Parameters("@OE_Remarks").Value = txtRemarks.Text 
    cmd.ExecuteNonQuery() 
    MsgBox("Successfully Added Equipment Profile") 
    sqlconn.Close() 
+0

什麼格式是你的PAS而purchaseDate登錄? – Jason 2013-05-02 01:03:43

+0

dd/mm/yyyy @Jason – ivandinglasan 2013-05-02 01:05:38

+0

如果您傳入空日期,您需要確保您的數據庫設置爲允許空值。每列可以設置爲允許空值。 – Jason 2013-05-02 01:08:33

回答

2

如果要存儲空值,則必須在vb代碼中使用條件邏輯,以便不將這些參數發送到存儲過程,或發送dbnull值。即使你的字符字段存儲空字符串,這可能不是你的意圖。

+0

語法和邏輯怎麼會先生? – ivandinglasan 2013-05-02 01:05:57

+0

好神人,至少把你自己的一些努力加入其中。 – JohnFx 2013-05-02 02:35:01

+0

@JohnFx我不想問問,如果我沒有用完想法,已經嘗試dbNULL。我先嚐試不同的事情,然後在發佈 – ivandinglasan 2013-05-02 03:41:06

0

使用

If (Not String.IsNullOrEmpty(txtPurchaseDate.Text)) Then 
    cmd.Parameters("@OE_PurchaseDate").Value = txtPurchaseDate.Text 
End If 
+0

會嘗試先生brb反饋謝謝 – ivandinglasan 2013-05-02 01:17:01

+0

IsNull(txtPurchaseDate.Text)檢查可能不是絕對必要的 - 我認爲txtPurchaseDate .Text將爲空字符串,「」如果未設置。您可以通過在運行時檢查該值來檢查這一點。雖然它可能比不包括支票更安全。 – Ryan 2013-05-02 01:18:26

+0

IsNull未聲明先生 – ivandinglasan 2013-05-02 01:19:13

0

爲什麼不使用的DateTimePicker爲您而purchaseDate代替txtPurchaseDate.Text你一個文本框能說出你的控制dtpPurchaseDate那麼你必須:

cmd.Parameters("@OE_PurchaseDate").Value = dtpPurchaseDate.Value 

默認值(或最小值)爲DateTime Picker爲1/1/1980,因此您可以將dtpPurchaseDate的值初始化爲1/1/1980,如:

dtpPurchaseDate = System.DateTime.Parse("1/1/1980") 

所以,這意味着順便說一句,當存儲在你的數據庫中它的值爲1/1/1980而不是NULL。

如果你想用文本框錢包,那麼你可以檢查是否爲空,則指定最小日期值實際上是1/1/0001這樣的:

If (If (String.IsNullOrEmpty(txtPurchaseDate.Text)) Then 
    cmd.Parameters("@OE_PurchaseDate").Value = DBNull.Value 
Else 
    Dim tempPurchaseDate as DateTime 

    System.DateTime.TryParse(txtPurchaseDate.Text, tempPurchaseDate) 
    cmd.Parameters("@OE_PurchaseDate").Value = tempPurchaseDate   
End If 

但同樣它在儲值你的數據庫1/1/0001

你可以顯示在你的文本框狀空: 鑑於你有一個DataRow名爲博士

If (dr["OE_PurchaseDate"] = System.DateTime.Parse("1/1/0001")) Then 
    txtPurchaseDate.Text = "" 
Else 
    txtPurchaseDate.Text = dr["OE_PurchaseDate"] 
End If 
+0

如果我不保留它而不改變任何默認日期,該怎麼辦? – ivandinglasan 2013-05-02 01:48:12