Hi!
my code gives me this error.
"An object reference is required for the non-static field, method, or property 'HD.RegisterMember.CN'"


Plz can anyone help me.
This is my code(Class);

private SqlConnection CN;
        public static String getLastMemID()
        {
             
      
            String ID = null;
            try
            {

                CN = new SqlConnection("Data Source=BJ-PC\\SQLEXPRESS;Initial Catalog=DB;Integrated Security=SSPI");
           
                CN.Open();
                string sqlQuery = @"SELECT MAX(Mem_ID) FROM MemberMaster";
                using (SqlCommand com = new SqlCommand(sqlQuery, CN))
                {
                    using (SqlDataReader dr = com.ExecuteReader())
                    {
                        if (dr.Read())
                             ID=dr[0].ToString();
                    }
                }
            }
             
            catch (SqlException ex)
            {
                MessageBox.Show("A SqlException. You must check the Querry parameteres", "Error Message");
                MessageBox.Show(ex.ToString());

            }
            finally
            {
                CN.Close();


            }
           // if (ID > 0)
              // MessageBox.Show(ID);
           // else
           //     MessageBox.Show("There is no id in this table (its an empty table)");

            return ID;
        }

i had the same error before but i forgot what i did... i guess its about the cn.open(); put it in after string sqlquery=select idnum. hehe

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.