Click here to Skip to main content
11,632,011 members (77,285 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB7.x VB SQL SQL-Server database , +
how to check if a value exists in a DB table and return a Boolean value to variable in vb.net??
Posted 2-Sep-12 21:02pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can create a function like this and execute the function to get your desired output

create function fnCheckExists()
returns bit
as
begin
declare @val as bit=0;
if exists (select yourColumn from yourTable)
begin
 set @val=1;
end
else
begin
 set @val=0;
end
return @val
end
  Permalink  
Comments
Oshtri Deka at 3-Sep-12 4:02am
   
You are checking if there is any record inside a table.
I believe OP wants to check for particular values.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
dmunisubbu at 3-Sep-12 3:52am
   
fine 4+
kishore doni at 3-Sep-12 3:59am
   
Thank you dmunisubbu

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,393
1 Sergey Alexandrovich Kryukov 8,044
2 Mika Wendelius 6,260
3 F-ES Sitecore 2,321
4 Suvendu Shekhar Giri 1,970


Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100