2014-03-31 38 views
0

在vb.net中执行数据读取器命令时,我正面临错误。它抛出处理。这个字段就像你在文本框中输入员工ID一样,然后它将在数据库中捕获其他字段名称,部门。在vb.net中为数据读取器获取错误

这里是我的代码

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 

Dim conn As New MySql.Data.MySqlClient.MySqlConnection 

Dim strConnectionString As String =ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString 

Dim sqlQuery As String = "SELECT * hr_record WHERE Emplid='" & txt1.Text & "'" 

Using sqlConn As New MySqlConnection(strConnectionString) 

     Using sqlComm As New MySqlCommand() 

     With sqlComm 

     .CommandText = sqlQuery 

      End With 

       Try 
        sqlConn.Open() 

        Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader() 

        While sqlReader.Read() 

         txt1.Text = sqlReader("Emplid").ToString() 

         TextBox1.Text = sqlReader("Nama").ToString() 

         TextBox2.Text = sqlReader("DeptDesc").ToString() 

        End While 

       Catch ex As MySqlException 

        MessageBox.Show(ex.Message) 
       End Try 

      End Using 

     End Using 

    End Sub 
+0

你会得到什么错误? – Arion

+0

@Arion我收到错误“连接必须有效并打开。” 'Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()' – kolapopo

+1

你的查询不应该是'SELECT * FROM hr_record ...'吗? –

回答

0

试图改变自己的选择查询像

Dim sqlQuery As String = "SELECT * from hr_record WHERE Emplid='" & txt1.Text & "'" 

注: - 无法使用这样的代码Page_Load。尝试做一个函数并调用该函数从Page_Load并始终使用Parameterized query

更新答案:

的Page_Load

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
DataBind() 
End Sub 

外方法:

Public Sub DataBind() 
Dim sConnection As String = "server=(local);uid=sa;pwd=PassWord;database=DatabaseName" 
    Using Con As New MySqlConnection(sConnection) 
     Con.Open() 
     Using Com As New MySqlCommand("SELECT * from hr_record WHERE Emplid='"txt1.Text 
     "'", Con) 
      Using RDR = Com.ExecuteReader() 
       If RDR.HasRows Then 
        Do While RDR.Read 
        txt1.Text = RDR.Item("Emplid").ToString() 
        TextBox1.Text = RDR.Item("Nama").ToString() 
        TextBox2.Text = RDR.Item("DeptDesc").ToString() 
        Loop 
       End If 
      End Using 
     End Using 
     Con.Close() 
    End Using 
End Sub 

注:试图实现这样你的逻辑和还修改按您的要求。

希望它有效。

+0

我只是覆盖代码,但仍然有相同的错误。顺便说一句,我应该做什么? icant在页面加载写入?所以我必须写出正确的位置在哪里。 – kolapopo

+0

您需要在一个方法/函数中绑定该“Page_Load”代码,并从“Page_Load”调用该方法/函数并将您的连接和查询传递给“MySqlCommand”... – Rahul

+0

我已经完成了您告诉我的但现在看起来好像没有错误,但是当我输入ID时,它没有任何功能,另一个字段不填充数据库中的数据。 – kolapopo

0

您尝试在页面加载得到EMPLID,但它仍然是什么,如果EMPLID存在

0

感谢帮助me..finally此代码它的工作原理感谢所有的一切都归功于你应该使用按钮来检查。 这个代码将起作用

Dim conn As New MySql.Data.MySqlClient.MySqlConnection 

Dim strConnectionString As String = ConfigurationManager.ConnectionStrings("testConnectionString").ConnectionString 


    Using sqlConn As New MySqlConnection(strConnectionString) 
     sqlConn.Open() 
     Using sqlComm As New MySqlCommand() 

      sqlComm.Connection = sqlConn 
      With sqlComm 


       .CommandText = "SELECT * from hr_record WHERE Emplid='" & txt1.Text & "'" 


      End With 

      Try 


       Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader() 

       While sqlReader.Read() 

        txt1.Text = sqlReader("Emplid").ToString() 

        TextBox1.Text = sqlReader("Nama").ToString() 

        txtdep.Text = sqlReader("DeptDesc").ToString() 

       End While 

      Catch ex As MySqlException 

       MessageBox.Show(ex.Message) 
      End Try 
      sqlConn.Close() 
     End Using 

    End Using 

End Sub