Saturday, April 3, 2010

Check record exist or not using asp.net and c#

This code show how to check record exist in database.Method get custid as a parameter and pass to stored procedure that will check record exist or not.

public bool IsCustomerExist(string strCust)
{
//SqlConnection objSqlConnection = null;
SqlCommand objSqlCommand = null;
try
{
//objSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["StrConnect"].ToString());
objSqlCommand = new SqlCommand("usp_CheckCustomerExists", con);
objSqlCommand.CommandType = CommandType.StoredProcedure;
objSqlCommand.Parameters.Add("CustomerID", SqlDbType.Int, 4).Value = Convert.ToInt32(strCust);
con.Open();
SqlDataReader objSqlDataReader = objSqlCommand.ExecuteReader();
if (objSqlDataReader.HasRows)
return true;
else
return false;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
}

Thanks & Regards
Santosh

0 comments:

Post a Comment