none
How to solve this error ....(ExecuteScalar: Connection property has not been initialized.) RRS feed

  • Question

  • I have following code that generate the error how to solve it..

     

    public bool IsExistsforUpdate()

    {

    int temp; string cnstr = @"Data Source=abhishek-pc\sqlexpress;Initial Catalog=eAttendance;Integrated Security=True";

    SqlConnection con = new SqlConnection(cnstr);

    SqlCommand cmd = new SqlCommand("Select count(*) from department where departmentcode = @departmentcode and departmentid != @departmentid , con");

    SqlParameter pDepartmentId = new SqlParameter("@departmentid", SqlDbType.VarChar, 50);

    pDepartmentId.Value = departmentid; cmd.Parameters.Add(pDepartmentId);

    SqlParameter pDepartmentCode = new SqlParameter("@departmentcode", SqlDbType.VarChar, 20);

    pDepartmentCode.Value = departmentCode;

    cmd.Parameters.Add(pDepartmentCode);

    con.Open();

    temp =Convert.ToInt32(cmd.ExecuteScalar()); -- (ExecuteScalar: Connection property has not been initialized. how to solve this error)

    con.Close();

    if (temp > 0)

    return true;

    else

    returnfalse;

    }

    • Moved by Sethu Srinivasan Thursday, August 30, 2012 5:35 PM ADO.NET (From:SQL Server Application and Multi-Server Management)
    Wednesday, August 18, 2010 6:20 PM

All replies