Returning an integer value from SQL Server
.net, c#, sql, sql-server
Solution
I notice a couple potential issues:
You need to call reader.Read(), before trying to read data from it. This is usually done in a loop when people expect multiple rows.
while (reader.Read()) {
flID = reader.GetInt16(0);
}
also in your SQL if "about" is meant to be a literal and not another column name you probably need single quotes around it:
"SELECT id FROM Pages WHERE pageName='about'"
Problem
I have an SQL query as follows: ``` SqlConnection conn = new SqlConnection(connectionString); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT id FROM Pages WHERE pageName=about",conn); //cmd.Parameters.Add("@pageName","hakkinda"); SqlDataReader reader = cmd.ExecuteReader(); flID = reader.GetInt16(0); reader.Close(); conn.Close(); ``` I get an error message: `Invalid attempt to read when no data is present.` What's wrong?