Chathz Chathz - 3 months ago 12
C# Question

Insert record to a table using linq query that contains auto increment ID

I'm having issue to insert a record to table that contains auto increment ID ,

I'm going to insert values to table using entity frameworks.

This is my table creation query

USE [DB_Name]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_Name](
[Discussion_ID] [int] IDENTITY(1,1) NOT NULL,
[Discussion_Name] [nvarchar](50) NULL,
[Discription] [nvarchar](255) NULL,

CONSTRAINT [PK_Table_Name] PRIMARY KEY CLUSTERED
(
[Discussion_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


This is my View Page code

@model albaraka.Models.Table_Name

@{
ViewBag.Title = "Discussion_Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Discussion_Create</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>AB_Discussion</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

@Html.HiddenFor(model => model.Discussion_ID)


<div class="form-group">
@Html.LabelFor(model => model.Discussion_Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Discussion_Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Discussion_Name, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Discription, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Discription, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Discription, "", new { @class = "text-danger" })
</div>
</div>


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}


this is model class

namespace project_name.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public partial class Table_Name
{
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int Discussion_ID { get; set; }
public string Discussion_Name { get; set; }
public string Discription { get; set; }

}
}


This is controller class

[HttpGet]
public ActionResult Discussion_Create()
{
return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Discussion_Create(Table_Name discussion)
{

if (ModelState.IsValid)
{
db.Table_Name.Add(discussion);
db.SaveChanges();

return RedirectToAction("Index");
}

return View(discussion);
}


I want to insert record without user enter discussion Id, that's why I put it in a hidden field here, how can I do that?

Answer

There is no need to use any hidden element in your view, you can remove it. You should use a try catch block in your save changes do you can catch EF exceptions and act accordingly :

try{
    db.Table_Name.Add(discussion);
    db.SaveChanges();
}
catch(Exception Ex){
    //trace the exception
}

put a breakpoint in the debugger on the exceptions if there is any and also peek in the db what is actually inserted. I think your code should work, so i don anticipate exceptions but it is good practice to check for them.

Comments