Results 1 to 2 of 2

Thread: [RESOLVED] Error when getting data from the database. VWDE/SQL/Asp.Net/C#

  1. #1

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

    Resolved [RESOLVED] Error when getting data from the database. VWDE/SQL/Asp.Net/C#

    I get the error:
    Index out of range exception was unhandled by user code.
    At the line:
    Label4.Text += reader["RelativeLN"].ToString();

    The two lines above it:
    Label4.Text += reader["Relation"].ToString() + ": ";
    Label4.Text += reader["RelativeFN"].ToString();

    will read just fine if I comment out that third line. I don't understand why this could be happening. There is data in the db for the RelativeLN column and it has the same data type as the previous two columns.

    private void getRelatives()
    {
    //Define database connection
    SqlConnection conn = new SqlConnection(
    "Server=localhost\\SqlExpress;Database=MyDB;" + "Integrated Security=True");
    //Create command
    SqlCommand comm = new SqlCommand("SELECT Relation, RelativeFN FROM RelativeTable WHERE RelativeTable.UserID IN (SELECT UserID FROM UsrTbl WHERE UserName = @usrnmeLbl)", conn);
    comm.Parameters.AddWithValue("@usrnmeLbl", usrNmeLbl.Text);
    conn.Open();
    SqlDataReader reader = comm.ExecuteReader();
    while (reader.Read())
    {
    Label4.Text += reader["Relation"].ToString() + ": ";
    Label4.Text += reader["RelativeFN"].ToString();
    Label4.Text += reader["RelativeLN"].ToString();
    }
    }

    I'm hoping someone can provide me some info on what could possibly be going wrong.
    Thank you so much in advance.

  2. #2

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

    Re: Error when getting data from the database. VWDE/SQL/Asp.Net/C#

    Solved it. I wasn't selecting that column. Duh...

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