Applying datareader results to label or text???

  • Thread starter Thread starter KathyB
  • Start date Start date
K

KathyB

Please tell me where I'm going wrong on the marked lines***. The code
is fine to that point. I've only used this code to populate
dropdownlists using databind to the control. Here, I just need to get
the one field associated with a variable or a label control.

THANKS!

kathy

Dim Conn3 As New OleDbConnection()
Dim Rdr3 As OleDbDataReader
Dim strSQL3 As String = "SELECT Password FROM tblUsers WHERE
([UserName] = @UserName)"
Dim Cmd3 As New OleDbCommand(strSQL3, Conn3)
Conn3 = New OleDbConnection(strConn)
Dim prmUserName As OleDbParameter = New OleDbParameter("@UserName",
OleDbType.VarChar, 50)
prmUserName.Value = cboUser.SelectedItem.Value
Cmd3.Parameters.Add(prmUserName)
Cmd3.Connection = Conn3
Conn3.Open()
Rdr3 = Cmd3.ExecuteReader()
dbPass.Text = Rdr3("Password") '***what is wrong with this
line?
Dim strPass As String = Rdr3("Password") '***or what is wrong with
this line?
Rdr3.Close()
Conn3.Close()
 
You have to call the Read method of the datareader to advance it to the
first record, before you can grab any values. If you are not at the first
row yet - you cannot access a column value - since there is no row!

Read return true or false based on whether or not it was able to advance the
cursor.
 
What kind of problem you having? Besides needing to read
the reader (see posting by Marina) you cannot use
the .Text property on a text box that is type 'password'.
You can use the .Attribute.item("Value")= ... to set it
though...
 
Thanks, Marina,

Could you please tell me HOW to get to the first row. The only time I've
used datareader was to populate listboxes, can't figure out how to get
just the one value.

Thanks.

KathyBurke
 
I did tell you how to do this. I said to call the Read method, and I
explained its return values and what it means.

For more information, please look up the OleDbDataReader class in the
documentation.
 
Yep. Marina did give you the right info...

So to use this in the code you provided earlier, it would look like:

Dim Conn3 As New OleDbConnection()
Dim Rdr3 As OleDbDataReader
Dim strSQL3 As String = "SELECT Password FROM tblUsers WHERE _
([UserName] = @UserName)"
Dim Cmd3 As New OleDbCommand(strSQL3, Conn3)
Conn3 = New OleDbConnection(strConn)
Dim prmUserName As OleDbParameter = New OleDbParameter("@UserName",
OleDbType.VarChar, 50)
prmUserName.Value = cboUser.SelectedItem.Value
Cmd3.Parameters.Add(prmUserName)
Cmd3.Connection = Conn3
Conn3.Open()
Rdr3 = Cmd3.ExecuteReader()

If Rdr3.Read() Then ' <--- positions to the first record
dbPass.Text = Rdr3("Password") '***what is wrong with this
line?
Dim strPass As String = Rdr3("Password") '***or what is wrong with this
line?
End If
Rdr3.Close()
Conn3.Close()

This should give you what you want.
--
Jim Blizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.


--------------------
 
Thank you all for the help, I am now getting the reader results (yeah!),
but still doing something wrong comparing it to the password textbox.

David said "you cannot use
the .Text property on a text box that is type 'password'.
You can use the .Attribute.item("Value")= ... to set it
though..."

I must be doing this wrong (haven't dealt with attributes yet...) so
please tell me where this line is wrong...?

thanks again, kathy

dbpass is the textbox set to password format:

dbPass.Attributes.Item("Value") = Rdr3("Password") OR...
dbPass.Attributes.Item("Password") = Rdr3("Password")

sorry if i'm being totally stupid. I also can't find a good reference on
what the available attributes are...any suggestions?


KathyBurke
 
YA KNOW KATHY,

if you post here for an answer, do us the courtesy of at
least READING OUR RESPONSES.

You were given a complete answer, and yet, you ask the
SAME QUESTIONS AGAIN - How do I read, How do I use the
text field in a password control.

It's really aggravating.
 
YA KNOW DAVID,

That's a bit MEAN! I did read the responses, but I am very new to
asp.net and sometimes it doesn't sink in without an example. Sometimes
the experienced people (e.g., you and Marina) respond with a direction
of "do this" and sometimes that doesn't translate if the person hasn't
done that before. But let me assure you, I have read several books but
they tend to blur after a while. Real world examples work best for some
people (including me, unfortunately). When I went back and read Marina's
first response, I realized what I had missed.

And I did try your response control.attributes.item("value") =
reader_result, but I kept getting an unknown page error...therefore, I
asked again. I worked around it by assigning the reader_result to a
textbox and doing a compare validator...I'm sure that's not the best way
to go.

All that being said, I think it's great that you and other very
experienced users help those less fortunate via this group. I certainly
didn't mean to piss you off.

Kathy



KathyBurke
 
Back
Top