locked
A field initializer cannot reference the nonstatic field, method, or property '_Default..' - a diff. problem i guess

    Question

  • hi,

    could anyone help me out here.  i am getting an error on Web Dev EE

    Error    1    A field initializer cannot reference the nonstatic field, method, or property '_Default.connString'    D:\Apps\CRMS\Default.aspx.cs    15    50    D:\Apps\CRMS\

    and here's my code (C#)

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    namespace CLibrary
    {
        public class CEmployees
        {
            private SqlConnection conn;

            public CEmployees(string strConn)
            {
                this.conn = new SqlConnection(strConn);
            }

            public bool AuthorizeUser(string sUserId, string sPassword)
            {
                object userCount;
                SqlCommand cmd = new SqlCommand("SELECT * FROM employees WHERE (em_username = @Uid AND em_password = @Pwd)", this.conn);

                cmd.Parameters.Add("@Uid", SqlDbType.VarChar);
                cmd.Parameters["@Uid"].Value = sUserId;

                cmd.Parameters.Add("@Pwd", SqlDbType.VarChar);
                cmd.Parameters["@Pwd"].Value = sPassword;

                this.conn.Open();
                userCount = cmd.ExecuteScalar();
                int finalUserCount = Convert.ToInt32(userCount);
                this.conn.Close();

                if (finalUserCount > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }

    and for my Default.aspx.cs where i am getting the error is this:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using CLibrary;

    public partial class _Default : System.Web.UI.Page
    {
        private string connString = ConfigurationManager.ConnectionStrings["StringConnection"].ConnectionString;
        public CEmployees userLogin = new CEmployees(connString);
       
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMsg.Visible = false;

            reqFldValidUid.Visible = true;
            reqFldValidPwd.Visible = true;

            if (IsPostBack)
            {
                if (userLogin.AuthorizeUser(txtUsername.Text, txtPassword.Text))
                {
                    Response.Redirect("Index.html");
                }
                else
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "Access is denied!";
                    reqFldValidUid.Visible = false;
                    reqFldValidPwd.Visible = false;
                }
            }

            txtUsername.Text = "";
            txtPassword.Text = "";
            txtUsername.Focus();
        }
    }

    the error is Error    1    A field initializer cannot reference the nonstatic field, method, or property '_Default.connString'    D:\Apps\CRMS\Default.aspx.cs    15    50    D:\Apps\CRMS\

    thanks!
    Sunday, May 13, 2007 5:41 AM

Answers

  • The line that is in error is in a place in your code which requires that the things to the right of '=' be static variables.  The 'new CEemployees(connString)' is not.

     

    Try it this way:

     

    Code Snippet
    public partial class _Default : System.Web.UI.Page
    {
        private string connString = ConfigurationManager.ConnectionStrings["StringConnection"].ConnectionString; 
         
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMsg.Visible = false;
            public CEmployees userLogin = new CEmployees(connString);

            reqFldValidUid.Visible = true;
            reqFldValidPwd.Visible = true;

     

    Now the variable is initalized when the page is loaded.


     

    Sunday, May 13, 2007 11:28 AM