2011-07-04 32 views
2
Protected Sub cmdOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdOK.Click 
     Dim medID, medName, comment As String 
     Dim power, period, qtty As Integer 
     'Dim nextDateOfDelivery As Date = getNextDateOfDelivery(Val(txtDays.Text)) 
     Dim today As System.DateTime 
     Dim answer As System.DateTime 
     today = System.DateTime.Now 
     answer = today.AddDays(Val(txtDays.Text)) 
     Dim nextDateOfDelivery As Date = answer 


     medName = Trim(txtMedName.Text) 
     qtty = Val(txtQuantity.Text) 
     comment = txtComment.Text 
     power = txtPower.Text 
     medID = lblMedID.Text 
     period = Val(txtDays.Text) 
     '............setting priorities 
     '.......value of p(0) indicates priority for tablest and so on so forth..... 
     Dim p() As Integer = {0, 0, 0, 0} 
     For i = 0 To LBPriorities.Items.Count - 1 
      Select Case LBPriorities.Items(i).Text 
       Case "Tablet" 
        p(0) = i + 1 
       Case "Capsule" 
        p(1) = i + 1 
       Case "Liquid" 
        p(2) = i + 1 
       Case "Injection" 
        p(3) = i + 1 

      End Select 

     Next 

     '............if not available 
     Dim strictlyThis As String = radNotAvailable.SelectedItem.Value 
     Dim connStr As String = ConfigurationManager.ConnectionStrings("databaseConnectionString").ConnectionString 

     Dim con As New SqlClient.SqlConnection(connStr) 
     'Dim con As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;" & _ 
     '          "AttachDbFilename=|DataDirectory|\database.mdf;" & _ 
     '          "Integrated Security=True;User Instance=True") 

     Dim sql As New SqlClient.SqlCommand("INSERT INTO medicine" & _ 
      "(medID, userID, medName, quantity, dateOfOrder, power, tabPriority, capPriority, liqPriority, " & _ 
      "injPriority, period, notAvailable, prescription, comment, nextDateOfDelivery)" & _ 
      "VALUES('" & medID & "','" & User.Identity.Name & "','" & medName & "'," & qtty & ",'" & today & "'," & power & _ 
      "," & p(0) & "," & p(1) & "," & p(2) & "," & p(3) & ",'" & period & "','" & strictlyThis & "', '' ,'" & comment & "','" & nextDateOfDelivery & "')", con) 

     con.Open() 
     sql.ExecuteNonQuery() 
     con.Close() 
     con.Dispose() 

     lblMessage.Text = "The details of the medicine have been saved. You may upload a prescription" 
     FUPrescription.Enabled = True 
     cmdUpload.Enabled = True 
    End Sub 

這是我現在使用的是什麼...但我得到一個錯誤說「字符串或二進制數據將被截斷。 該語句已終止。」這是什麼意思???是否有我的SQL語句錯誤?字符串或二進制數據將被截斷。該語句已終止

+7

你試圖把一些不適合列的數據(例如,你的列是varchar(50),並且你試圖放置100個字符) –

+0

它是varchar(50)...我把它加到varchar(700) )仍然我得到錯誤...我可以保存一個日期類型在VArchar? –

+0

你可以粘貼你的標籤le創建腳本和您試圖插入的所有值? –

回答

2

我想說這是你存儲在dateOfOrder中的值。

根據你的截圖,這是一個varchar(10),但你存儲在那裏DateTime.Now.ToString(),默認值是格式:

5/1/2008 6:32:06 PM 

其中在19個字符的用武之地。

我建議你在more suitable field中存儲日期和時間,例如DateDateTime字段。

如果你必須將它保存爲文本,那麼你至少應該將其存儲在format that is suitable for sorting,和明確的,如:

DateTime.Now.ToString("o") // Round-trip format 

,這將給你:

2008-06-15T21:15:07.0000000 
相關問題