I have a connection in a DAL to SQL server. Using this connection I am trying to read the query. I have tried using ExecuteReader() and ExecutreNonQuery() with no luck.
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
try{
var cn = new Connection_Handler();
string input = selectinput.Text;
string reader = cn.Execute_SQL(input).ToString();
MessageBox.Show(reader);
}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
}
class Connection_Handler
{
SqlConnection cn;
void ConnectiontoSQL()
{
string str = "server=xxx; database=jobSearch; user=sa; password=xxx";
cn = new SqlConnection(str);
cn.Open();
}
public int Execute_SQL(string select)
{
ConnectiontoSQL();
string Query = "select " + select + " from jobSearch where searchID = 1";
SqlCommand cmd = new SqlCommand(Query, cn);
SqlDataReader reader = cmd.ExecuteReader();
return Convert.ToInt32(reader);
}
}
my error message is "unable to cast object of type 'sqldatareader' to type 'iconvertible' "