Hi Try like this
private void Form4_Load(object sender, EventArgs e)
{
int Company_Id = 3;
Boolean id = Convert.ToBoolean(existCompanyId(Company_Id));
}
public int existCompanyId(int Company_Id)
{
string SQLConnectionStr = "Password=password;Connection Timeout=0;Persist Security
Info=False;User ID=userid;Initial Catalog=databasename;Data Source=servername;";
SqlConnection sqlConnection = new SqlConnection(SQLConnectionStr);
int CId;
string TableExists = "IF EXISTS (select * from Company WHERE CompanyId ='" +
Company_Id + "') select 1 ELSE select 0";
try
{
using (SqlConnection spContentConn = new SqlConnection(SQLConnectionStr))
{
spContentConn.Open();
using (SqlCommand TableExistsQry = new SqlCommand(TableExists, spContentConn))
{
TableExistsQry.CommandTimeout = 0;
TableExistsQry.CommandType = CommandType.Text;
CId = System.Convert.ToInt32(TableExistsQry.ExecuteScalar());
}
spContentConn.Close();
}
return CId;
}
catch (Exception ex)
{
throw ex;
}
}
If the Company Id 3 exists in the table we will get id as true other wise id as false.