Codigo ASP.Net - Asignar una columna de un DataReader a una varible en WebMatrix

Hace tiempo que quiero usar las funciones que se generan con el Wizard Code de Web Matrix, pero creando un DataReader en lugar de DataSet.
Por fin entendi, que no se puede tener acceso directo a los Items del DataReader. Lo cual no me agrada mucho, pero ya solucione el problema. Sobretodo en los casos de leer un registro de la tabla.
Lo que yo habia estado intentando durante todo este tiempo.
Aqui la funcion:

Function obtenLotexID(ByVal iDLote As Integer) As System.Data.IDataReader
Dim connectionString As String = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "SELECT [Lotes].* FROM [Lotes] WHERE ([Lotes].[IDLote] = @IDLote)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_iDLote As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_iDLote.ParameterName = "@IDLote"
dbParam_iDLote.Value = iDLote
dbParam_iDLote.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_iDLote)

dbConnection.Open
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

Y aquí el llamado a la funcion

Sub Page_Load
Dim drLote as System.Data.OleDB.OleDBDataReader
intIDLote = Request.QueryString("lote")
textID.Text = obtenLotexID(intIDLote)
End Sub



Por supuesto, lo anterior manda un error de que no encuentra la columna correspondiente. Es asi que hoy vi un ejemplo y la forma de capturar los datos y ponerlas en una variable o como texto de un objeto de html es:


Sub Page_Load
Dim drLote as System.Data.OleDB.OleDBDataReader
intIDLote = Request.QueryString("lote")
intIDEmpresa = Request.QueryString("empresa")
drLote = obtenLotexID(intIDLote)
While drLote.Read()
txtID.Text = drLote("IDLote")
End While
End Sub



Como se puede observar en realidad se crea un nuevo DataReader el cual se llenara con los datos de la funcion (que por supuesto es un datareader por si misma).
Y ahora si, busco mediante un loop los valores que estoy requiriendo.

Listo!

Comentarios

Entradas más populares de este blog

Yahoo! UI (yui) TabView como menu horizontal.

Truco: usar Firefox en el portal IDSE del IMSS

Visual Web Express Edition con MS Access Membership