2014-03-31 71 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