因为vbcrlf而尝试插入Mysql数据库ex.tostring时出错

时间:2015-06-30 17:24:22

标签: mysql vb.net

我写了这段代码:

Try
    Uff_IdW = Me.DataGridView1.Item(DgvCol, IdWInsDB).Value.ToString.Trim().Split("_")
    If Uff_IdW.Length = 2 Then SplitOk = True Else SplitOk = False
Catch ex As Exception
    mErr = "split error: " & ex.ToString
    SQLStr = "INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', '" & Errore & "')"
    InsertDBase(cnString, SQLStr)
    SplitOk = False
End Try

但插入查询不起作用,因为ex.tostring中有一个“vbcrlf”。

代码产生的查询字符串如下所示:

"INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', 'split error: System.NullReferenceException: Object reference not set to an instance of an object." & vbCrLf & "   in Test_GP.Form1.FirstOption_DGV_Click(Int32 DgvRow, Int32 DgvCol)')"

如何修复此问题并将没有错误的ex.tostring插入我的数据库?

1 个答案:

答案 0 :(得分:0)

使用字符串上的Replace函数去除回车符:

SQLStr = "INSERT INTO errlog (Sect, ErrDescr) VALUES ('GP', '" & Errore & "')"
SQLStr = SQLStr.Replace(Environment.NewLine, " ")
InsertDBase(cnString, SQLStr)