Sometimes we need to check whether a value exists in a particular column of database table or not. Below is the C# code for the same:
public static int checkExistingCustomerName(string custName)
{
string strCmd = "SELECT COUNT(CustomerName) FROM CustomerDetails WHERE CustomerName='" + custName + "'";
string custID;
using (OleDbConnection conn = new OleDbConnection(Constants.connectionString))
{
try
{
OleDbCommand cmd = new OleDbCommand(strCmd, conn);
conn.Open();
object obj = cmd.ExecuteScalar();
if (obj == DBNull.Value || Convert.ToInt32(obj)==0)
return 0;
else
return 1;
}
catch (Exception ex)
{
return 1000;
}
}
}