Thursday, April 11, 2013

Data Connection with Check condition using Data set and get the Return value


using System;
using System.Data;
using System.Data.SqlClient;
Public bool Yourfunctionname ()
{
    Try
     {
bool bTemp = true;

DataSet ds = new DataSet();

SqlConnection cn1 = new SqlConnection (“Server=YourServerAddress;Database=YourDataBase;User Id=YourUsername;Password=YourPassword;”);

cn1.Open();

            SqlCommand  cmd1 = new SqlCommand("select YourIdName from  YourTableName where parentcode = '" + strParentCode + "' or code = '" + strCode + "'", cn1);

SqlDataAdapter da = new SqlDataAdapter(cmd1);

da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (strCode == ds.Tables[0].Rows[i][0].ToString())
                    {
                        bTemp = false;
                    }
                }
            }         
 
dr1.Close();

cn1.Close();

return bTemp;
  }

  Catch (Exception ex)
   {

      Response.Write(ex.Message);

      return false;

    }
}

No comments:

Post a Comment