I'm trying to query data from an SQL table using C# using System.Data.SqlClient;
. I'm able to SELECT * FROM table
without problem. I suspect my problem may be due to data types, and perhaps unintentionally casting some variables incorrectly. The error I get is: Invalid attempt to read data when no data is present.
Further details:
In my SQL Table, Column1 and Column3 are of type int. Column2 and Column4 are of type char(1). Column5 is of type decimal(5,4).
Could you please help identify the problem in following code?
public void Score(List<Tuple<int, char>> myList)
{
int myColumn1 = myList[0].Item1;
char myColumn2 = myList[0].Item2;
int myColumn3 = myList[1].Item1;
char myColumn4 = myList[1].Item2;
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM table" +
" WHERE Column1 = " + myColumn1 +
" AND Column2 = '" + myColumn2 +
"' AND Column3 = " + myColumn3 +
" AND Column4 = '" + myColumn4 + "'", myConnection);
SqlDataReader myReader = null;
myReader = myCommand.ExecuteReader();
Console.Out.WriteLine(myReader[5].ToString());
}
Thank you!