2013-03-19 70 views
0

我想使用SqlDataReader從數據庫表中檢索信息。我有兩列一個字符串,另一個是位。該字符串將填充到文本框中。但是,當我想將它填充到單選按鈕列表時,這個問題並沒有發生。它不斷給我這個錯誤Specified cast is not valid.這是我VB的服務器端代碼:SqlDataReader將字節轉換爲整數vb.net

Dim dt As DataTable = New DataTable() 
     Dim command As New SqlCommand(query2, conn) 
     Dim param As New SqlParameter() 
     param.ParameterName = "@cUserName" 
     param.Value = Session("Edit") 
     command.Parameters.Add(param) 

     Dim dr As SqlDataReader = command.ExecuteReader() 
     If dr.HasRows Then 
      dr.Read() 
      tbUsername.Text = dr.GetString(0) 
      rblDept.SelectedIndex = dr.GetByte(1) 
     End If 

我試圖dr.GetByte(1),但它不工作。請幫幫我。

+0

您是否試過'Convert.ToInt32(dr.GetByte(1))'或可能的'CInt()'? – 2013-03-19 04:56:38

+0

想一想,就我所知,一個有位值的列是二進制的,可以存儲0或1(或者在這種情況下你可能有問題)。你應該可以做'CInt(dr(1))',它應該可以工作。 – 2013-03-19 05:00:23

+0

@yu_ominae是的,我試過了,它給了我同樣的錯誤! – 7alhashmi 2013-03-19 05:10:08

回答

0

我做了一些愚蠢的事情,它的工作原理。代碼是:

If dr.HasRows Then 
      dr.Read() 
      tbUsername.Text = dr.GetString(0) 
      Dim deptid As Integer = CInt(dr(1)) 
      If deptid = -1 Then 
       rblDept.SelectedIndex = 1 
      Else 
       rblDept.SelectedIndex = 0 
      End If 
     End If 

它的工作原理非常完美。

謝謝大家。