Winforms文本框自动完成崩溃应用程序

时间:2018-06-21 15:04:05

标签: sql vb.net winforms

有时,当我在文本框中编写内容时,程序崩溃。我找不到解决方案。这是非常奇怪的情况。为什么程序有时会崩溃。此处代码:

 Private Sub CreateProductNameAutoCompleteSource(ByVal tamamlamaturu As Byte)
    Dim sql_ProductName As String
    Dim gorkemDAL As New Aktuel.Data.SqlClient(cns_ScalaConnectionString)
    Dim dtbl_ProductName As DataTable
    Dim X As New AutoCompleteStringCollection

    Dim parcakodu
    If tamamlamaturu = tamamlamatipi.talepedilenparca Then parcakodu = txt_talepedilenparca.Text
    If tamamlamaturu = tamamlamatipi.gonderilenparca Then parcakodu = txt_gonderilenparca.Text

    sql_ProductName = "select distinct urunkodu from urunler where urunkodu like '" & parcakodu & "%' order by urunkodu asc"

    gorkemDAL.OpenDbConnection()
    dtbl_ProductName = gorkemDAL.ExecuteTable(sql_ProductName)
    gorkemDAL.CloseDbConnection()

    For Each row As DataRow In dtbl_ProductName.Rows
        X.Add(row(0).ToString)
    Next
    If tamamlamaturu = tamamlamatipi.talepedilenparca Then
        txt_talepedilenparca.AutoCompleteSource = AutoCompleteSource.CustomSource
        txt_talepedilenparca.AutoCompleteCustomSource = X
        txt_talepedilenparca.AutoCompleteMode = AutoCompleteMode.Suggest
    End If

    If tamamlamaturu = tamamlamatipi.gonderilenparca Then
        txt_gonderilenparca.AutoCompleteSource = AutoCompleteSource.CustomSource
        txt_gonderilenparca.AutoCompleteCustomSource = X
        txt_gonderilenparca.AutoCompleteMode = AutoCompleteMode.Suggest

    End If

End Sub

和Textchange事件:

Private Sub txt_talepedilenparca_TextChanged(sender As Object, e As EventArgs) Handles txt_talepedilenparca.TextChanged
    CreateProductNameAutoCompleteSource(tamamlamatipi.talepedilenparca)
End Sub

我在事件查看器中发现错误消息: Error Message

Error Message

3 个答案:

答案 0 :(得分:0)

代码看起来很干净,我看不到任何明显的东西,但是我要确保parcakodu不包含任何以sql结尾的语音标记:

sql_ProductName = "select distinct urunkodu from urunler where urunkodu like '" & parcakodu & "%' order by urunkodu asc"

成为:

sql_ProductName = "select distinct urunkodu from urunler where urunkodu like '" & parcakodu.Replace("'", "''") & "%' order by urunkodu asc"  

(您可能想复制并粘贴,以查看我在这里所做的事情)

我还会将您的ForEach语句包装为null检查-它是否可能是您在尝试访问dtbl_ProductName为null时?访问该属性。如果对象为null,则行将引发异常,而本身为Nothing。

尝试类似的东西:

If (dtbl_ProductName IsNot Nothing)
    For Each row As DataRow In dtbl_ProductName.Rows
        X.Add(row(0).ToString)
    Next
End If

失败-使用过去导致其崩溃的已知搜索字符串,检查是否每次都能使其崩溃。字符串sql中是否存在不喜欢的字符,或者sql查询的结果集为空?

答案 1 :(得分:0)

尝试使用此技巧:

    Private Sub txt_talepedilenparca_TextChanged(sender As Object, e As EventArgs) Handles txt_talepedilenparca.TextChanged

Dim sql_ProductName As String
    Dim gorkemDAL As New Aktuel.Data.SqlClient(cns_ScalaConnectionString)
    Dim dtbl_ProductName As DataTable
    Dim X As New AutoCompleteStringCollection

sql_ProductName = "select distinct urunkodu from urunler where urunkodu order by urunkodu asc"

    gorkemDAL.OpenDbConnection()
    dtbl_ProductName = gorkemDAL.ExecuteTable(sql_ProductName)
    gorkemDAL.CloseDbConnection()

try
        If txt_talepedilenparca.lenght >= 3 and txt_talepedilenparca.lenght mod 3 = 0 then 
                    For Each row As DataRow In dtbl_ProductName.select("urunkodu like '*" & parcakodu & "*'")
            X.Add(row(0).ToString)
        Next
if x.count > 0
txt_talepedilenparca.AutoCompleteSource = AutoCompleteSource.CustomSource
        txt_talepedilenparca.AutoCompleteCustomSource = X
        txt_talepedilenparca.AutoCompleteMode = AutoCompleteMode.Suggest
end if
    End If
catch ex as exception
msgbox(ex.tostring)
end try
    End Sub

答案 2 :(得分:0)

在回答关于您收到的错误的问题时,您说程序正在关闭。 因此,第一步是将代码包装在Try ... Catch块中,以便在不关闭程序的情况下获得有意义的错误消息。

Private Sub CreateProductNameAutoCompleteSource(ByVal tamamlamaturu As Byte)
    Try
        Dim sql_ProductName As String
        Dim gorkemDAL As New Aktuel.Data.SqlClient(cns_ScalaConnectionString)
        Dim dtbl_ProductName As DataTable
        Dim X As New AutoCompleteStringCollection

        Dim parcakodu
        If tamamlamaturu = tamamlamatipi.talepedilenparca Then
            parcakodu = txt_talepedilenparca.Text
        ElseIf tamamlamaturu = tamamlamatipi.gonderilenparca Then
            parcakodu = txt_gonderilenparca.Text
        Else
            Exit Sub
        End If

        sql_ProductName = "select distinct urunkodu from urunler where urunkodu like '" & parcakodu.Replace("'", "''") & "%' order by urunkodu asc"

        gorkemDAL.OpenDbConnection()
        dtbl_ProductName = gorkemDAL.ExecuteTable(sql_ProductName)
        gorkemDAL.CloseDbConnection()

        If (dtbl_ProductName IsNot Nothing) Then
            For Each row As DataRow In dtbl_ProductName.Rows
                X.Add(row(0).ToString)
            Next
        End If

        If tamamlamaturu = tamamlamatipi.talepedilenparca Then
            txt_talepedilenparca.AutoCompleteSource = AutoCompleteSource.CustomSource
            txt_talepedilenparca.AutoCompleteCustomSource = X
            txt_talepedilenparca.AutoCompleteMode = AutoCompleteMode.Suggest
        End If

        If tamamlamaturu = tamamlamatipi.gonderilenparca Then
            txt_gonderilenparca.AutoCompleteSource = AutoCompleteSource.CustomSource
            txt_gonderilenparca.AutoCompleteCustomSource = X
            txt_gonderilenparca.AutoCompleteMode = AutoCompleteMode.Suggest

        End If
    Catch ex As Exception
        MessageBox.Show(String.Format("The following error has occured:{0}{1}", vbCrLf, ex.Message), "An error has occured")
    End Try
End Sub

Private Sub txt_talepedilenparca_TextChanged(sender As Object, e As EventArgs) Handles txt_talepedilenparca.TextChanged
    If txt_talepedilenparca.Text.Length >= 3 Then
        CreateProductNameAutoCompleteSource(tamamlamatipi.talepedilenparca)
    End If
End Sub

在解决主要问题之前,这将使您对所发生的事情有个清晰的认识。

我也对传递给查询的字符串值进行了转义,因此如果用户在文本框中输入单引号,则不会创建SQL注入。 我还限制了该功能在文本框至少包含3个字母之前不会触发。

如果程序仍然关闭而没有错误消息,则说明问题发生在其他地方,但是如果您收到错误消息,则可以从那里获取。