• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Error populating a text using a datareader

I am trying to populate a label and two text boxes. When I debug I can see the label gets populated. However the error message with the text box is:

Method 'object.ToString()' referenced without parentheses


Here is my code:

while (dr.Read())
{
lblPortalUserId.Text = dr["PortalUserId"].ToString();
dr.Read();
txtUserName.ToString = dr["UserName"].ToString();
dr.Read();
txtPassword.ToString = dr["Password"].ToString();
dr.Read();
dr.Close();
}

Any suggestions?
0
Sanjeet
Asked:
Sanjeet
  • 2
1 Solution
 
ennixoCommented:
while (dr.Read())
{
lblPortalUserId.Text = dr["PortalUserId"].ToString();
dr.Read();
txtUserName.Text= dr["UserName"].ToString();
dr.Read();
txtPassword.Text= dr["Password"].ToString();
dr.Read();
dr.Close();
}

you wrote ToString instead of Text for txtUserName and txtPassword properties
0
 
ennixoCommented:
i don't know what you're trying to do exactly but i think what you want is this :

while (dr.Read())
{
    lblPortalUserId.Text = dr["PortalUserId"].ToString();
    txtUserName.Text= dr["UserName"].ToString();
    txtPassword.Text= dr["Password"].ToString();
}
dr.Close();

dr.Read() reads the next record and stores it in your DataReader, then you get values from the columns of the current record to populate label and textbox
if you .Read() between each column value this won't be the same record AND you will get an Exception at the end of the DataReader

dr.Close() closes the datareader so any data can be read then. if you close it inside your while loop, i think when it will test the next dr.Read() you'll get an Exception too.
0
 
dunglaCommented:
Remove dr.Read() and dr.Close() at the last line of while loop
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now