ASP.Net MVC application does not connect to the database

Posted on

Question :

I’m developing a solution in object-oriented programming class for web. Let’s create a stock control system. One of the first “screens” of access, is the login, only that at the moment, my whole ‘application’ already goes down.

In the first screen, we have the login field and password, when we click on the ‘login’ button a method is called, which makes the login to the bank, however the return is always ‘invalid login’

Method FazLogin

        return Contexto.tblUsuario
        .Where(u => u.LOGIN == usuario && u.SENHA == senha).FirstOrDefault() != null; 

Through Visual Studio itself, if we query the database, I can see the tblUsuario table and I can also see my users for testing. In the connection string, it looks like this:

<add name="ModeloEstoqueContextoContainer" connectionString="metadata=res://*/ModeloEstoqueContexto.csdl|res://*/ModeloEstoqueContexto.ssdl|res://*/ModeloEstoqueContexto.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LENOVO-PCSQLEXPRESS;initial catalog=ESTOQUE_DATABASE;user id=sa;password=SENHAXXXXX;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

We are working on the MVC model, and yes, references have been checked over and over, both by me and by the teacher.

@Cyana, Metadata (Connection String) is automatically generated by the Entity Framework

@pnet, The password is not encrypted, it is a very simple thing we are doing in college, when entering the password, we have a response.Redirect that directs to the other page. When debugging always ‘count = 0’ and values are null.

I can not get the controller here.


Answer :

Dude, I made a simple login screen with MVC and I worked like this. Build in the controller a method that would take the bank logged in the face, just like you do. Then, it serialized the information through a JsonResult and sent it to a jquery function that deserelized the result of the linq, validated and then called the corresponding screen. That’s how I did it and it never gave me a headache. See the codes below.


        public JsonResult ValidaLogin(string _email, string _senha)
            INETGLOBALEntities db = new INETGLOBALEntities();

            EncriptyDecripty cripto = new EncriptyDecripty();

            string s = cripto.Encrypt(_senha);

              var  result_login = (from login in db.tbl_usuario
                                    where == _email && login.senha_usuario == _senha
                                    select new {, login.nm_usuario }).ToList();

             if(result_login.Count > 0)
                return Json(new { result_login }, JsonRequestBehavior.AllowGet);

                return Json(JsonRequestBehavior.AllowGet);


function ValidaLogin() {

        url: '/Login/ValidaLogin',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify({ _email: $('#inputEmail').val(), _senha: $('#inputPassword').val() }),
        success: function (data) {

            $(data.result_login).each(function () {
                if ( != '' || != null)
                    $(window.document.location).attr('href', '/Pesquisa/Pesquisa');
        error: function (error) {

            alert('Usuário ou senha digitados de forma incorreta.');

That’s it, and it never got me working that way.


Leave a Reply

Your email address will not be published. Required fields are marked *