views:

18

answers:

1

Hello All,

How do insert data to a SQl Server 2008 database server using Entity Framework from a user input on a web application.

I added a new EF connection o my model. I am able to see all the mappings correctly.

I currently have the following in my view:

<% using (Html.BeginForm()) { %> <%: Html.Label("Enter Phone Number:") %> <%: Html.TextBoxFor(m => m.PhoneNumber) %> <% } %>

and in my controller

 public ActionResult DialTone(Models.TelephoneModels telephone)
        {
            List<string> callType = new List<string>();
            callType.Add("Standard");
            callType.Add("Emergency");
            ViewData["TypeOfCalls"] = new SelectList(callType);
            if (!ModelState.IsValid)
            {
                return View("DialTone", telephone);
            }

            ViewData["Number"] = "You are currently connected to: " + telephone.PhoneNumber;

            using (LogEntities db = new LogEntities())
            {
                var log = db.Logs.Single(m => m.PhoneNumber == telephone.PhoneNumber);
                TryUpdateModel(log);
                db.SaveChanges();
            }


            return View();

I want to log all phone numbers (or strings) that the user inputs in the TextBox - phonenNumber into the database.

I created a LOG database table with a column called "PhoneNumber". I cant seem to figure out how to insert the data into the table.

Any ideas?

Thanks!

:)

+1  A: 

I figure it out. I used:

 using (LogEntities db = new LogEntities())
            {
                Random random = new Random();
                Log p = new Log();
                p.ID = "400"; //Just a test
                p.PhoneNumber = telephone.PhoneNumber;
                db.AddToLogs(p);
                db.SaveChanges();
            }
Mage