Results 1 to 4 of 4

Thread: [RESOLVED] Reader giving error on third read from database.

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [RESOLVED] Reader giving error on third read from database.

    For the first two reads from the query, it seems like everything is fine. But for some reason the third read gives me an error no matter what I ask it to read from.

    Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?) C:\Inetpub\wwwroot\thisApp\userpage.aspx.cs 65 35 C:\...\thisApp\

    This is the format of my code:
    if (Label2.Text == usr)
    {
    if (Label3.Text == pss)
    {
    Label2.Text = reader["UserName"] + "<br />";
    Label3.Text = reader["Password"] + "<br />";
    Label4.Text = reader["Relation"];
    //break; //exit out of the loop since user found
    }

    When I test it with this code it shows it up just fine:
    //take the read data from the reader and display it
    readDB_Lbl.Text = (String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}",
    reader[0], reader[1], reader[2], reader[3], reader[4], reader[5], reader[6], reader[7], reader[8], reader[9], reader[10], reader[11], reader[12], reader[13], reader[14], reader[15], reader[16], reader[17], reader[18], reader[19], reader[20], reader[21], reader[22]));

    I hope someone can give me a heads up, thank you in advance.

    Also, I thought that in C# I could just return the lines to keep them short and that the semicolon would end a line and it wouldn't be a problem for lines to return but with this last bit of code even if I concatenate with a + it still errors out that it's needing to be closed. Why won't something like this work?
    readDB_Lbl.Text = (String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, + {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, + {20}, {21}, {22}",

  2. #2

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: Reader giving error on third read from database.

    For the first part I found the answer. Since the database could contain a null in one of the columns, I need to cast .ToString()

    Still not sure why the concatenation doesn't work for that last line of examples.
    readDB_Lbl.Text = (String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, +
    {20}, {21}, {22}",

    How would you concatenate that line if it's not possible this way?

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Reader giving error on third read from database.

    + is string concatenation or variable addition.

    Code:
    readDB_Lbl.Text = (String.Format(@"{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, 
    {20}, {21}, {22}",
    Using the verbatim (@) should work for you.

  4. #4

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Re: Reader giving error on third read from database.

    Thank you for this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width