Search code examples
c#asp.netentity-frameworkef-database-first

Nothing is getting written to database


I'm a beginner in ASP.Net. I'm using Entity Framework Database First Method. I need to store data to my MUSIC Database, in the table called LogTable. I have tried many ways. Nothing works. At last I tried this:

in Login.aspx

using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using MusicShop.Models;

namespace MusicShop.Pages.Accounts
{
public partial class Login : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnLogin_OnClick(object sender, EventArgs e)
    {
        var userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            ConfigurationManager.ConnectionStrings["MUSICConnectionString"].ConnectionString;

        var manager = new UserManager<IdentityUser>(userStore);

        var user = manager.Find(txtUserName.Text, txtPassword.Text);


        if (user != null)
        {
            InsertUserLog();
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = false
            }, userIdentity);


            Response.Redirect("~/Index.aspx");
        }
        else
        {
            litStatus.Text = "Invalid username or password";
        }
    }

    private void InsertUserLog()
    {
            LogModel model = new LogModel(); //Add a new model
            var logtable = new LogTable
            {
                IP = Request.ServerVariables["REMOTE_ADDR"].ToString(),
                //You could set a break point to check the value.
                AGENT = Request.ServerVariables["http_user_agent"].ToString(),
                REQM = Request.ServerVariables["request_method"].ToString(),
                SERVER = Request.ServerVariables["server_name"].ToString(),
                PORT = Request.ServerVariables["server_port"].ToString(),
                SW = Request.ServerVariables["server_software"].ToString(),
                DNS = Request.ServerVariables["REMOTE_HOST"].ToString()
            };

            //set value
            var error = model.InsertLog(logtable);

            if (error != null) Response.Write(error);

    }
}
}

in LogModel.cs

public string InsertLog(LogTable logTable)
{
    try
    {
        MUSICEntities db = new MUSICEntities();
        db.LogTables.Add(logTable);
        db.SaveChanges();
        return null;
    }
    catch (Exception e)
    {
        return "Error:" + e;
    }
}

No error. But nothing is added to database. Can anyone guide me? Please pardon me, if this is a silly mistake.


Solution

  • You probably aren't seeing an error because you aren't checking for it.

    try outputting the error you are returning.

    var error = model.InsertLog(logtable);
    
    if(error != null) Response.Write(error);
    

    I also noticed you are simply concatenating the Exception instance ...

    you probably should say "Error:" + e.Message or "Error:" + e.StackTrace

    Since you seem to be relying on the ToString() method of the Exception class .. not sure how much info you'll get out of it.