INST_ID
column is type of integer, and you are passing a string as criteria in your query. You have to convert it to
int
. Moreover, your
SqlCommand
text is also ill-formed. Add parameters to your
SqlCommand
then use it in a connection as in the following:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_SqlParameter
{
class Program
{
static void Main(string[] args)
{
string connStr = @"[some connection string]";
string cmdStr = @"Select INST_ID, TOTAL_REVE, DATE, FINYR, "+
"INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, "+
"PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, "+
"TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, "+
"CURRDEBT, LONGTERMDEBT, TOTALNETASSETS from TableFIN2013 where INST_ID=@pInstId";
string paramVal = "1";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
cmd.Parameters.AddWithValue("@pInstId", int.Parse(paramVal));
SqlDataReader reader = cmd.ExecuteReader();
}
}
}
}
}
For SqlCommand.Parameters, see here:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters%28v=vs.110%29.aspx[
^]
Besides error, I advice to separate data management code from UI code. It is a good practice to design data and UI management in different libraries.