Databinding DataReaders

Testsubject

New member
Joined
Dec 29, 2005
Messages
1
Programming Experience
Beginner
Hi folks,

I have a number of questions/little issuses with datareaders that I need some help with.

I know this is going to sound silly but.....How do you data bind a datareader which returns a single row to a label's text prop??

VB.NET:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
 strConsCode = Request.QueryString("ConsCode")
 
 cnBarwickPas.Open()
 
 Dim cmdPatsByCons As New Odbc.OdbcCommand("SELECT ASSETCOM.MASTER5.MSMROL, ASSETCOM.MASTER5.MSFNME1, ASSETCOM.MASTER5.MSSNME, CASE WHEN LENGTH(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8))) = 8 THEN SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 1 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 3 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 5 FOR 4) WHEN LENGTH(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8))) = 7 THEN '0' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 1 FOR 1) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 2 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 4 FOR 4) ELSE 'NO DATE' END AS MSDOB, ASSETDDH.SPEC5.SPCODE, ASSETDDH.SPEC5.SPNAME, ASSETDDH.CONSLT5.CONSCODE, ASSETDDH.CONSLT5.CONSNAME, ASSETDDH.WARDS5.WARDCODE, ASSETDDH.WARDS5.WARDNAME, CASE WHEN LENGTH(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8))) = 8 THEN SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 1 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 3 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 5 FOR 4) WHEN LENGTH(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8))) = 7 THEN '0' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 1 FOR 1) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 2 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 4 FOR 4) ELSE 'NO DATE' END AS IADATE, ASSETDDH.INPAT5.IUNIT FROM ASSETCOM.MASTER5, ASSETDDH.INPAT5, ASSETDDH.CONSLT5, ASSETDDH.SPEC5, ASSETDDH.WARDS5 WHERE ASSETCOM.MASTER5.MSMROL = ASSETDDH.INPAT5.IMROLL AND ASSETDDH.INPAT5.ICCONS = ASSETDDH.CONSLT5.CONSCODE AND ASSETDDH.INPAT5.ICSPEC = ASSETDDH.SPEC5.SPCODE AND ASSETDDH.INPAT5.IWARD = ASSETDDH.WARDS5.WARDCODE AND ASSETDDH.CONSLT5.CONSCODE =" & "'" & strConsCode & "'", cnBarwickPas)
 
 Dim drConsName As Odbc.OdbcDataReaderdrConsName = cmdPatsByCons.ExecuteReader(CommandBehavior.SingleRow)
 
 lblConsultantSts.Text = drConsName("CONSNAME")
 
 lblConsultantSts.DataBind()
 
 drConsName.Close()
 
 cnBarwickPas.Close()
 
 End Sub
Now the above code does not work and returns a nice message saying haha up yours I'm not going to work.:(

VB.NET:
 [B][I]No data exists for the row/column.[/I] [/B]

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif][B]Description:  [/B]An unhandled exception occurred during the execution of the current web  request. Please review the stack trace for more information about the error and  where it originated in the code. 

[B]Exception Details:  [/B]System.InvalidOperationException: No data exists for the  row/column.

[B]Source Error:[/B] 

   An unhandled exception was generated during the execution of the  current web request. Information regarding the origin and location of the  exception can be identified using the exception stack trace below.  
[B]Stack Trace:[/B] 

   [InvalidOperationException: No data exists for the row/column.]
   System.Data.Odbc.OdbcDataReader.GetValue(Int32 i) +102
   System.Data.Odbc.OdbcDataReader.get_Item(String value) +20
   DDHPAS.consultant.Page_Load(Object sender, EventArgs e) +245
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +731
[/FONT]
Now this is part has gone completely over my head. :eek: If I add a ListBox to the web form data bind the ListBox to the drConsName Data Reader and then data bind the Label to the drConsName Data Reader it works fine. Code exmaple below.

VB.NET:
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
  strConsCode = Request.QueryString("ConsCode")
  
 cnBarwickPas.Open()
 
  Dim cmdPatsByCons As New Odbc.OdbcCommand("SELECT ASSETCOM.MASTER5.MSMROL, ASSETCOM.MASTER5.MSFNME1, ASSETCOM.MASTER5.MSSNME, CASE WHEN LENGTH(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8))) = 8 THEN SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 1 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 3 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 5 FOR 4) WHEN LENGTH(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8))) = 7 THEN '0' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 1 FOR 1) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 2 FOR 2) || '-' || SUBSTRING(CAST(ASSETCOM.MASTER5.MSDOB AS VARCHAR(8)) FROM 4 FOR 4) ELSE 'NO DATE' END AS MSDOB, ASSETDDH.SPEC5.SPCODE, ASSETDDH.SPEC5.SPNAME, ASSETDDH.CONSLT5.CONSCODE, ASSETDDH.CONSLT5.CONSNAME, ASSETDDH.WARDS5.WARDCODE, ASSETDDH.WARDS5.WARDNAME, CASE WHEN LENGTH(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8))) = 8 THEN SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 1 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 3 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 5 FOR 4) WHEN LENGTH(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8))) = 7 THEN '0' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 1 FOR 1) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 2 FOR 2) || '-' || SUBSTRING(CAST(ASSETDDH.INPAT5.IADATE AS VARCHAR(8)) FROM 4 FOR 4) ELSE 'NO DATE' END AS IADATE, ASSETDDH.INPAT5.IUNIT FROM ASSETCOM.MASTER5, ASSETDDH.INPAT5, ASSETDDH.CONSLT5, ASSETDDH.SPEC5, ASSETDDH.WARDS5 WHERE ASSETCOM.MASTER5.MSMROL = ASSETDDH.INPAT5.IMROLL AND ASSETDDH.INPAT5.ICCONS = ASSETDDH.CONSLT5.CONSCODE AND ASSETDDH.INPAT5.ICSPEC = ASSETDDH.SPEC5.SPCODE AND ASSETDDH.INPAT5.IWARD = ASSETDDH.WARDS5.WARDCODE AND ASSETDDH.CONSLT5.CONSCODE =" & "'" & strConsCode & "'", cnBarwickPas)
  
  Dim drConsName As Odbc.OdbcDataReaderdrConsName = cmdPatsByCons.ExecuteReader(CommandBehavior.SingleRow)
 
 lbxConsultantSts.DataSource = drConsName
 lbxConsultantSts.DataTextField = ("CONSNAME")
 lbxConsultantSts.DataBind()
 lblConsultantSts.Text = drConsName("CONSNAME")
 lblConsultantSts.DataBind()
 drConsName.Close()
 
 cnBarwickPas.Close()
 
 End Sub
Why does this work and allow me to data bind a label's text prop to the same data reader when there is a ListBox using the same data reader but not allow me to data bind a single Label to the data reader. (Hope this makes sense).

Just as some background to this I am using VS.NEt 2003 with VB.

If anyone can sort this out or help me i would be very grateful.

Thanks!!!
 
Back
Top