I have the following function which checks to see if a user(strU
) exist in the table for a column, if so return 1, otherwise return 0:
public int AddDataScalar(string strU)
{
string strQueryExistence = "SELECT 1 FROM [OB].[h].[OP_PEONS] WHERE Executive= '" + strU + "'";
int inNum;
using (SqlConnection con = new SqlConnection(strConn))
{
con.Open();
SqlCommand cmd = new SqlCommand(strQueryExistence, con);
object value = cmd.ExecuteScalar().ToString();
if (value != null)
{
inNum = 1;
}
else
{
inNum = 0;
}
con.Close();
}
return inNum;
}
It is failing in this line: object value = cmd.ExecuteScalar().ToString();
With the following error: Object reference not set to an instance of an object.
How do I resolve it?