Check whether a specific value exists or not in a table using ASP.NET


The following function will check if a specific value exists or not in a database. checkValueExists function will take three arguments — FileName, Table Name and Field Value.

public bool checkValueExists(string strField,string strTable,string strFieldValue)

{

try

{

StringBuilder sbQuery = new StringBuilder();

sbQuery.Append("select Count("+ strField +") from "+ strTable +" where "+ strField +" = '");

sbQuery.Append(strFieldValue.Replace("'","''"));

sbQuery.Append("'");

 

// returns Boolean value i.e True if value already exist,False if value doesn't exist.

 

if(ExecScaler((sbQuery.ToString()))>0)

return true;

}

catch(Exception ex)

{

throw new Exception(ex.ToString());

}

return false;
}



Added on July 20, 2007 Comment
}