C#值不能为空。参数名称:user

时间:2017-12-01 08:36:01

标签: c# connection-string

在我的C#应用​​程序中,我使用了2个连接字符串(application_cs,users_cs)。要更改这些连接字符串,我使用:

public int CreateUser(int stid, int cid, int usrId, string email, string tel, string mob, string username, 
            bool create, bool prime)
        {
            int result = 0; 
            Guid userid = new Guid();
            DALUsers.UserDBDataContext dc = new DALUsers.UserDBDataContext();
            DAL.AppDataContext d = new DAL.AppDataContext();

            BLL.Security.SetProviderUsers();

            if (create) //create the user first
            {

                string question = "1";
                string answer = "1";
                bool isAproved = true;

                string password = System.Web.Security.Membership.GeneratePassword(8, 2);


                MembershipCreateStatus cs = new MembershipCreateStatus();

                MembershipUser newUser = Membership.CreateUser(username, password, email, question, answer, isAproved, out cs);
                Membership.UpdateUser(newUser);
                Roles.AddUserToRole(username, "User_x");
                if (cs == MembershipCreateStatus.Success)
                {
                    result = 1;
                }
                else
                    X.MessageBox.Info("Error", "Cannot create user due to :" + cs.ToString(), UI.Danger).Show();
            }

            //at this point we have the user created either way. 

            // return userid;
            var id = (from i in dc.aspnet_Users where i.UserName.CompareTo(username) == 0 select i.UserId);
            if (id.Count() == 1)
            {
                userid = id.First();
                bool contin = true;
                var fulname = (from i in dc.Clients where i.id == usrId select i).First();

                if (String.IsNullOrEmpty(fulname.Mobile)) fulname.Mobile = mob; 

                fulname.Email = email;
                fulname.ModifiedBy = HttpContext.Current.User.Identity.Name;
                fulname.ModifiedDate = DateTime.Now;

                dc.SubmitChanges();


                DateTime dt = DateTime.Now;

                DALUsers.CIUser usr = new DALUsers.CIUser();

                var existing = (from i in dc.CIUsers where i.UserName.CompareTo(username) == 0 && i.cid == cid select i);
                if (existing.Count() > 0)
                {
                    X.MessageBox.Info("Warning", "UserName already exists . Please try another!", UI.Warning).Show();
                    contin = false;
                }
                else
                {


                    dc.CIUsers.InsertOnSubmit(usr);
                    dc.SubmitChanges();
                }

                if (contin)
                {
                    DALUsers.CIUser usrNew = new DALUsers.CIUser();

                    var approved = (from k in dc.aspnet_Memberships //if user is not approved 
                                    where k.UserId == userid
                                    select k).FirstOrDefault();

                    if (approved.IsApproved == false)
                    {
                        approved.IsApproved = true;
                    }

                    ProfileBase profile = ProfileBase.Create(username);
                    profile.SetPropertyValue("Mobile", mob);
                    profile.SetPropertyValue("Email", email);
                    profile.Save();

                    usrNew.UserId = usrId;
                    usrNew.cid = cid;
                    usrNew.FullName = fulname.LastName + " " + fulname.FirstName;
                    usrNew.Role = "User_x";
                    usrNew.SignRights = prime;
                    usrNew.IsPrime = prime;
                    usrNew.stid = stid;
                    usrNew.UserName = username;
                    usrNew.UserId = userid;
                    usrNew.CreatedDate = DateTime.Now;
                    usrNew.CreatedBy = HttpContext.Current.User.Identity.Name;
                    dc.CIUsers.InsertOnSubmit(usrNew);
                    dc.SubmitChanges();

                    result = 1;

                    X.MessageBox.Info("Success", "The user has been successfully added", UI.Success).Show();
                }
            }
            else
                X.MessageBox.Info("Error", "Could not find the user", UI.Danger).Show();
            BLL.Security.SetProviderApp();
            return result;
        }

因此,在我想要添加用户的代码中,我这样做:

DALUsers.aspnet_User user = new DALUsers.aspnet_User();

修改 我刚才在代码中看到了这一行:

user

但是变量用户不会在代码中的任何其他位置使用。可能它已经被放在那里......而且它是我代码中唯一名为git status file_name.py的变量。这是导致这个问题吗?但那么为什么只能在生产服务器上呢? 的修改

奇怪的是,当我从visual studio本地运行我的应用程序时,它就像一个魅力。但是当我在生产服务器上运行的应用程序中添加用户时,当我尝试添加第二个用户时,它会失败并且我收到此错误:

  

值不能为空。参数名称:user

如果我尝试登录我的应用程序之后就失败了。我必须从iis重新启动我的网站才能再次登录。 有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好吧,我无法在您的代码中发现错误,但如果您说这个错误只发生在服务器中并且您确定您的文件在服务器和本地计算机之间同步,那么可能错误在于您的web.config 。看看