you can change this way.. when you read database first you want to open the connection using
[SqlConnectionObject].Open();
, In your code you want to add
cn.Open();
. when you finish data accessing you need to close your connection by
[SqlConnectionObject].Close();
.
and try to use
try-catch-finally[
^] for
sql
operation so you can handle your exception too..
public int foundmer(string mername)
{
try
{
cn.Open();
SqlCommand comm1 = new SqlCommand();
comm1.CommandType = CommandType.Text;
comm1.CommandText = "select cse from tbl_user_CSE where cse='" + mername + "'";
comm1.Connection = cn;
SqlDataReader dtr1;
dtr1 = comm1.ExecuteReader();
if (dtr1.HasRows == true)
{
dtr1.Close();
return -1;
}
else
{
dtr1.Close();
return 1;
}
}
catch (Exception ex)
{
}
finally
{ cn.Close(); }
}