Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using sql-aliases and the datareader

Status
Not open for further replies.

akekrakbar

Programmer
Jun 18, 2003
1
SE
Hello!
Im trying to do a simple login with databaseinteractivity. But when i try to get data wich doesn´t come from a column in the db but rather a function i get an error message. This is the code:

Code:
Sub klick_login(Src as object, E as EventArgs ) 
    
   Dim cn as new OLEDBConnection(dbPath) 
   Dim dr as OleDbDataReader 
   Dim strSql as String 

   strSql = "SELECT COUNT() as total FROM medlemmar WHERE anvandarnamn='" & txtAnvandarnamn.text & "' AND losenord='" & txtLosenord.text & "'" 

   Dim cm = New OLEDBCommand(strSQL, cn)

   cn.Open() 
    
   dr=Cm.ExecuteReader(CommandBehavior.CloseConnection) 

dr.read()


   cn.Close() 
   lblTest.text = dr("total") 

End Sub

This is the error message:

Code:
Exception Details: System.InvalidOperationException: No data exists for the row/column. 

Source Error: 


Line 26:    lblTest.text = dr("total")

What am I doing wrong?
 
Hi there,

You have to put the line:

lblTest.text = dr("total")

before:

cn.Close()

That's the way datareader works, it fetch data directly from the database. Once you close the datareader, the connection to the database is gone, so no data left, that's why you got the error.

Cheers.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top