2013-11-21 76 views
0

嘿大家,我再次卡住,当窗体加载,我得到的错误:“连接错误:没有位置0行”的帮助!?Vb和MySQL连接和代码错误

Private Sub frmadduser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim sqlDT As New DataTable 
    txtlname.Text = "" 
    txtfname.Text = "" 
    txtaddress.Text = "" 
    txtcontact.Text = "" 
    Try 
     If Me.Text = "Edit Account" Then 
      sqlSTR = "SELECT * FROM users WHERE idUsers =" & FrmSYSUSER.lstusers.FocusedItem.Text 
      ExecuteSQLQuery(sqlSTR) 
      MsgBox(sqlDT.Rows(0)("Username")) 
      With sqlDT 
       If .Rows.Count > 0 Then 
        txtlname.Text = .Rows(0)("lname") 
        txtfname.Text = .Rows(0)("fname") 
        txtaddress.Text = .Rows(0)("address") 
        txtcontact.Text = .Rows(0)("contact") 
        txtusername.Text = .Rows(0)("username") 
        txtpassword.Text = .Rows(0)("password") 
        txtconfirm.Text = .Rows(0)("password") 
        cmbaccnttype.Text = .Rows(0)("accesstype") 
       End If 
      End With 
     End If 
    Catch ex As Exception 
     MsgBox("Connection Error :" & ex.Message, MsgBoxStyle.Information, xTitlename) 
    End Try 
End Sub 

回答

0

如果ExecuteSQLQuery(sqlSTR)返回DataTable你也许可以这样做:

sqlDT = ExecuteSQLQuery(sqlSTR) 

,并检查是否有它没做什么,像以前那样行:

if (sqlDT.Rows.Count>0) Then 
    With sqlDT 
     txtlname.Text = .Rows(0)("lname") 
     txtfname.Text = .Rows(0)("fname") 
     txtaddress.Text = .Rows(0)("address") 
     txtcontact.Text = .Rows(0)("contact") 
     txtusername.Text = .Rows(0)("username") 
     txtpassword.Text = .Rows(0)("password") 
     txtconfirm.Text = .Rows(0)("password") 
     cmbaccnttype.Text = .Rows(0)("accesstype") 
    End With 
End If