2015-01-16 64 views
0

我總是得到一個「字符串或二進制數據將被截斷」我搜索谷歌的答案,但我找不到相同的值爲我的文本框。在我的程序中,我有一個文本框(multiLine),它具有數值,逗號,小數點(2位)& vbCrlf,它看起來像這樣。可以存儲數字,逗號,小數點(2位)和vbcrlf的VB.net/SQL數據類型?

1,234,567.89 
     9,876,543.21 

上圖是我的文本框所具有的值的一個示例。 (注:請注意小心地將上面的數字有2個逗號,因爲它是百萬元),我不知道什麼是背後的原因,但如果它只有1COMMA,我的代碼工作完全正常一樣

123,456.78 
     987,654.32 

我已經設置我的vb.net數據類型爲字符串,小數,雙精度和對象,但我仍然得到相同的錯誤。關於我的SQL數據類型,我已經將它設置爲varchar,nvarchar,char,但仍然得到相同的錯誤。這裏是我的代碼:

*This is my CLASS code title SQLControl 

Public Sub AddToJournalEntry(DrCrAmount As Object) 
    Try 
     Dim strInsert As String = "INSERT INTO JournalEntry (DrCrAmount) " & _ 
            "VALUES (" & _ 
            "'" & DrCrAmount & "') " 
     SQLCon.Open() 
     SQLCmd = New SqlCommand(strInsert, SQLCon) 
     SQLCmd.ExecuteNonQuery() 
     SQLCon.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 
End Sub 

*This my code in my frmMain 

Dim sql As New SQLControl 

Private Sub btnGenerate_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerate.Click 

    Dim format1 As String = "{0,-10} {1,12}" 

    Dim Line1Amount, Line2Amount As String 

    If lbl1.Text = "Dr" Then 
     Line1Amount = String.Format(format1, txtAmount1.Text, vbNullString) 
     Line1Amount = Line1Amount.TrimEnd(" ", vbNullString) 
    ElseIf lbl1.Text = "Cr" Then 
     Line1Amount = String.Format(format1, vbNullString, txtAmount1.Text) 
     Line1Amount = Line1Amount.TrimEnd(" ", vbNullString) 
    Else 
     Line1Amount = String.Format(format1, vbNullString, vbNullString) 
     Line1Amount = Line1Amount.TrimEnd(" ", vbNullString) 
     lbl1.Text = vbNullString 
    End If 

    If lbl2.Text = "Dr" Then 
     Line2Amount = String.Format(format1, txtAmount2.Text, vbNullString) 
     Line2Amount = Line2Amount.TrimEnd(" ", vbNullString) 
    ElseIf lbl2.Text = "Cr" Then 
     Line2Amount = String.Format(format1, vbNullString, txtAmount2.Text) 
     Line2Amount = Line2Amount.TrimEnd(" ", vbNullString) 
    Else 
     Line2Amount = String.Format(format1, vbNullString, vbNullString) 
     Line2Amount = Line2Amount.TrimEnd(" ", vbNullString) 
     lbl2.Text = vbNullString 
    End If 

    Dim myString2 As String = Line1Amount & vbCrLf & Line2Amount 

    ****This code is to delete unnecessary spaces and new lines 
    myString2 = myString2.TrimEnd(" ", vbNullString, Convert.ToChar(Keys.Return), Convert.ToChar(Keys.LineFeed)) 

    txtDrCrAmount.Text = myString2 

End Sub 

Private Sub btnRecord_Save_Click(sender As System.Object, e As System.EventArgs) Handles btnRecord_Save.Click 

    sql.AddToJournalEntry(txtDrCrAmount.Text) 

End Sub 

注意:我的代碼已經被編輯,爲了您的方便,我已經刪除了不必要的代碼。

謝謝=)順便說一句,我是一個新手程序員。

+0

如果使用數據類型varchar創建數據庫表,則不應該有任何問題。 –

+0

嗨Akhil先生,是的,它正在工作,但問題在於上面提到的2comcom。它工作正常,在1 comma.tnx – Nadz

+0

但同樣適用於我與兩個甚至三個逗號和decimal.please檢查錯誤發生的地方。 –

回答

0

每當您看到消息....

string or binary data would be truncated 

覺得自己...磁場沒有大到足以容納我的數據。

檢查yourtable的表結構。我想你會發現字段的長度不足以容納你試圖插入的數據。例如,如果journalentry字段是varchar(50)字段,並且您嘗試將51個字符輸入到該字段中,則會出現此錯誤。

+0

謝謝,我忘了檢查與其他表相關/鏈接的其他表。有一個長度問題,現在解決了。非常感謝 – Nadz

相關問題