SqlDataReader将字节转换为整数vb.net
问题描述:
我想使用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)
,但它不工作。请帮帮我。
答
我做了一些愚蠢的事情,它的工作原理。代码是:
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
它的工作原理非常完美。
谢谢大家。
您是否试过'Convert.ToInt32(dr.GetByte(1))'或可能的'CInt()'? – 2013-03-19 04:56:38
想一想,就我所知,一个有位值的列是二进制的,可以存储0或1(或者在这种情况下你可能有问题)。你应该可以做'CInt(dr(1))',它应该可以工作。 – 2013-03-19 05:00:23
@yu_ominae是的,我试过了,它给了我同样的错误! – 7alhashmi 2013-03-19 05:10:08