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'tunderstand why this could be happening. There is data in the db for theRelativeLN column and it has the same data type as the previous twocolumns.
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 FROMRelativeTable WHERE RelativeTable.UserID IN (SELECT UserID FROM UsrTblWHERE 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.
Hello my friend,
There may be data in the database for it but you are not including this column in your query.
Your query starts with: SELECT Relation, RelativeFN FROM RelativeTable
Change it to start with: SELECT Relation, RelativeFN, RelativeLN FROM RelativeTable
Kind regards
Scotty
|||
OMG, thank you so much. I have been finagling around with this code so much I missed my changes.
THANK YOU THANK YOU!
No comments:
Post a Comment