2017-09-12 33 views
-1

我的論文有問題。它不斷顯示消息「位置0沒有行」。即使MySql數據庫中有數據。請幫忙,我是新手。謝謝!編輯表單加載。錯誤:「位置0處沒有行。」 [VB.net]

Private Sub frmAccountEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     txtUserID.Text = frmAccountList.AccountCaller 
     da = New MySqlDataAdapter("SELECT * FROM tbl_account WHERE userid = '" & txtUserID.Text & "'", con) 
     ds.Reset() 
     da.Fill(ds) 

     Try 
       cmbAccType.SelectedIndex = ds.Tables(0).Rows(0)(1).ToString 
       txtUsername.Text = ds.Tables(0).Rows(0)(2).ToString 
       txtPassword.Text = ds.Tables(0).Rows(0)(3).ToString 
       txtFirst.Text = ds.Tables(0).Rows(0)(4).ToString 
       txtLast.Text = ds.Tables(0).Rows(0)(5).ToString 
     Catch ex As Exception 
      MessageBox.Show(ex.Message, "ERROR!", MessageBoxButtons.OK, MessageBoxIcon.Error) 
     End Try 
End Sub 
+0

嘗試微調txtUserID.Text,可能有多餘的空格這可能最終上比較返回false。 – pvkcse

+0

@pvkcse你能告訴我如何修剪?謝謝 – Sadnerd

+0

txtUserID.Text = Trim(txtUserId.Text)也檢查userid是否與你的db中的一致。 – pvkcse

回答

0

運行mysql工作臺,並嘗試在那裏查詢以使其工作。您將從查詢中獲得更多信息。如果沒有第0行,那麼你的查詢不會返回任何內容。在嘗試訪問位置0之前尋找行數。

我包括一個我一直使用的類。你也不想用這樣的文本框來硬編碼你的查詢。您可能會受到SQL注入攻擊。用一些這樣的事

sqlquery = "SELECT * FROM tbl_account WHERE [email protected]" 

然後用addparam功能從我的類,加入你的文本框

dim xDB as new mysql 
xdb.addparam("@coluid", txtuserid.text) 
xdb.execquery(sqlquery) 
if xdb.rows.count>0 then 'you have a record 

Imports MySql.Data.MySqlClient 

Public Class mysql 

    'Connection string for mysql 
    Public SQLSource As String = "Server=1.1.1.1;userid=xuser;password=xpassword;" 

    'database connection classes 

    Private DBcon As New MySqlConnection 
    Private SQLcmd As MySqlCommand 
    Public DBDA As New MySqlDataAdapter 
    Public DBDT As New DataTable 
    ' parameters 
    Public Params As New List(Of MySqlParameter) 

    ' some stats 
    Public RecordCount As Integer 
    Public Exception As String 
    Sub ExecQuery(SQLQuery As String) 

     DBcon.ConnectionString = SQLSource 
     Try 
      DBcon.Open() 
      SQLcmd = New MySqlCommand(SQLQuery, DBcon) 
      'loads params into the query 
      Params.ForEach(Sub(p) SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value)) 

      'or like this is also good 
      'For Each p As MySqlParameter In Params 
      ' SQLcmd.Parameters.AddWithValue(p.ParameterName, p.Value) 
      ' Next 
      ' clears params 

      Params.Clear() 
      DBDA.SelectCommand = SQLcmd 
      DBDA.Update(DBDT) 
      DBDA.Fill(DBDT) 
      DBcon.Close() 
     Catch ex As MySqlException 
      Exception = ex.Message 
     Finally 
      DBcon.Dispose() 
     End Try 
    End Sub 
    ' add parameters to the list 
    Public Sub AddParam(Name As String, Value As Object) 
     Dim NewParam As New MySqlParameter(Name, Value) 
     Params.Add(NewParam) 
    End Sub 
End Class