ASP.Net MVC: System.Data.SqlClient.SqlException: Cannot open database Gamme.MDF data requested by the login. The connection failed.

anouar2002
 
on Apr 19, 2013 06:16 AM
4767 Views

Hello everyone,

I am trying to develop an application ASP MVC 3 using the C # language and the Visual Studio 2010 and SQL Server 2005 data.
I want to make records in my database, but I can not because the connection is failed.
I do not know why? I have checked my connection settings (server name, database name, iduser, password ...)
I also do a test connection manually (Tools / Add Connection) and it worked.
Here is the error message that appears:

Can not open database "Gamme.MDF data" requested by the login. The connection failed.
Failed to logon the user AdminUser '.

Description: An unhandled exception occurred falling on the execution of the current web request. Please review the stack trace for more information about the error and Where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Can not open database "Gamme.MDF data" requested by the login. The connection failed.
Failed to logon the user AdminUser '.

Source Error:


Line 97: SqlConnection cn = new SqlConnection (@ "Data Source = SWEET-DE396641E \ SQLEXPRESS; User Id = AdminUser; Password = AdminUser; Gamme.MDF DataBase =");
Line 98: SqlCommand cmd = new SqlCommand ("Insert Into User (Matricule, user_name, PassWord, Type_User) Values (" Unique Number "'," user_name "', '" passWord "', '" Type_User "')" cn);
Line 99: cn.Open ();
Line 100: return cmd.ExecuteNonQuery ();
Line 101:}

That's part of my controller:

 

[AcceptVerbs(HttpVerbs.Post)]
      public ActionResult AddUser(MvcApplication2.Models.AddUserModel am )
      {
          ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
 
          //if (ValidateAjout(Matricule, Nom_User, passWord, Type_User))
           
              if (!ModelState.IsValid)
              {
 
 
                  int _records = am.Insert(am.Matricule, am.Nom_User, am.passWord, am.Type_User);
                  if (_records > 0)
                  {
                      return RedirectToAction("Index", "Home");
                  }
                  else
                  {
                      ModelState.AddModelError("", "Can Not Insert");
                  }
 
              }
 
              return View();
           
      }

That's part of my model :

 

public class AddUserModel
    {
        [Required]
        [Display(Name = "Matricule :")]
        public string Matricule { get; set; }
 
        [Required]
        [Display(Name = "Nom :")]
        public string Nom_User { get; set; }
 
        [Required]
        [StringLength(100, ErrorMessage = "Le {0} doit avoir au minimum {2} caractères.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Mot de passe :")]
        public string passWord { get; set; }
 
        [DataType(DataType.Password)]
        [Display(Name = "Confirmer mot de passe :")]
        [Compare("Password", ErrorMessage = "Le mot de passe ne correspond pas la confirmation !")]
        public string ConfirmPassword { get; set; }
 
        [Required]
        [Display(Name = "Type :")]
        public string Type_User { get; set; }
 
        public int Insert(string Matricule, string Nom_User, string passWord, string Type_User)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=SWEET-DE396641E\SQLEXPRESS;User Id=adminUser;Password=adminUser;DataBase=Gamme.MDF");
            SqlCommand cmd = new SqlCommand("Insert Into User(Matricule, Nom_User,PassWord, Type_User )Values('"+Matricule+"','"+Nom_User+"','"+passWord+"','"+Type_User+"')", cn);
            cn.Open();
            return cmd.ExecuteNonQuery();
        }
    }

Are there a solution please, I await your response

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Apr 19, 2013 06:18 AM

Your connection string is not proper. Try

Data Source = SWEET-DE396641E \ SQLEXPRESS; User Id = AdminUser; Password = AdminUser; Initial Catalog = GameData