locked
How to save data into SQL Server? RRS feed

  • Question

  • User-403146133 posted

    I am working on ASP.NET MVC application. I need to save the data to SQL Server. 

    I tried coding all necessary parts and when I enter the data, the program says it is saved but when I check SQL Server Management Studio, nothing shows.

    This is my index view.

    <div class="row">
        <div class="col-sm-4">
    
    
            @using (Html.BeginForm("Save", "return", FormMethod.Post, new { id = "popupForm" }))
            {
                <div>
                    <h1>CAR RENTAL</h1>
                </div>
    
    
                <div class="form-group">
                    <label class="form-label">CAR NUMBER</label>
                    <input type="text" id="carnum" name="carnum" class="form-control" placeholder="Enter Car Number" required>
                </div>
    
                <div class="form-group">
                    <label class="form-label">CUSTOMER ID</label>
                    <input type="text" id="custid" name="custid" class="form-control" placeholder="Enter Customer ID" required>
                </div>
    
                <div class="form-group">
                    <label class="form-label">Date</label>
                    <input type="text" id="rdate" name="rdate" class="form-control" placeholder="Date" required>
                </div>
    
                <div class="form-group">
                    <label class="form-label">Days Elapsed</label>
                    <input type="text" id="elapsed" name="elapsed" class="form-control" placeholder="Enter Days Elapsed" required>
                </div>
    
                <div class="form-group">
                    <label class="form-label">FINE</label>
                    <input type="text" id="fine" name="fine" class="form-control" placeholder="Enter Fine" required>
                </div>
    
                <div class="form-group">
                    <input type="submit" value="Save" class="btn btn-success" />
                </div>
    
            }
    
        </div>
    </div>

    This is my controller.

    public ActionResult Save(returncar returncar)
            {
    
                    if (ModelState.IsValid)
                    {
                        db.returncars.Add(returncar);
    
                        var car = db.carregs.SingleOrDefault(e => e.carnum == returncar.carnum);
                        if (car == null)
                            return HttpNotFound("Car number not found");
    
                        car.availability = "Yes";
                        db.Entry(car).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                return View("~/Views/Return/Index.cshtml", returncar);
            }

    Can someone help me with this? Thank you.

    Sunday, January 17, 2021 2:26 AM

All replies

  • User-1545767719 posted

    You are the only one who can debug the actual code on the Visual Studio. So, why don't use the debugger?

    Put a breakpoint on the first line in the action method. Confirm if the model binding is properly done at the argument returncar. Operate the "Step over" button and check the local variables. 

    Sunday, January 17, 2021 6:45 AM
  • User-403146133 posted

    I tried putting a breakpoint on the first line in the action method but it says "A breakpoint could not be inserted in this location."

    Sunday, January 17, 2021 7:27 AM
  • User-1545767719 posted

    I tried putting a breakpoint on the first line in the action method but it says "A breakpoint could not be inserted in this location."

    Sorry I can't help. Let's me know after you could solve the debugger problem.

    Sunday, January 17, 2021 8:55 AM
  • User-1330468790 posted

    Hi noobprogrammer, 

     

    I tried coding all necessary parts and when I enter the data, the program says it is saved but when I check SQL Server Management Studio, nothing shows.

    What do you mean "the program says it is saved". If you modify the data and you can see it changed on the page, then it means that the data should be stored in the database. Cleaning the building and rebuilding the project would still see this modification.

    In this scenario, the only thing you need to do is to check if you visit a correct database in SSMS.

     

    Regarding the debugger issue, you could try to google and there are many posts related to this issue.

    A breakpoint could not be inserted in this location site:stackoverflow.com

     

    Hope helps.

    Best regards,

    Sean

    Monday, January 18, 2021 5:09 AM