-1

I want to auto generate the ID which incremented from my previous ID. ID format is A00001, A00002,... .. I dunno how to auto generate

Controller

[HttpPost]
    public ActionResult Create(Assignment assignment)
    {

        if (ModelState.IsValid)
        {

            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase assignmentFile = Request.Files[0];
                if (assignmentFile.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(assignmentFile.FileName);
                    assignment.FileLocation = Path.Combine(Server.MapPath("~/Content/File"), fileName);
                    assignmentFile.SaveAs(assignment.FileLocation);
                }


            }
            db.Assignments.Add(assignment);

            db.SaveChanges();

            return RedirectToAction("Index");
        }
        return View(assignment);
    }

Model

  public partial class Assignment
   {
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    public string AssignmentID { get; set; }
    public Nullable<System.DateTime> SubmissionDate { get; set; }
    public string Status { get; set; }
    public Nullable<decimal> Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation { get; set; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}

View

 <% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
 { %>
  <%: Html.ValidationSummary(true) %>

<fieldset>
    <legend>Assignment</legend>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.SubmissionDate) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.SubmissionDate) %>
        <%: Html.ValidationMessageFor(model => model.SubmissionDate) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Status) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Status) %>
        <%: Html.ValidationMessageFor(model => model.Status) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Mark) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Mark) %>
        <%: Html.ValidationMessageFor(model => model.Mark) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.Comments) %>
    </div>
    <div class="editor-field">
        <%: Html.EditorFor(model => model.Comments) %>
        <%: Html.ValidationMessageFor(model => model.Comments) %>
    </div>

    <div class="editor-label">
        <%: Html.LabelFor(model => model.FileLocation) %>
    </div>
    <div class="editor-field">
       <%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
    <%: Html.ValidationMessageFor(model => model.FileLocation) %>
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

I want to auto generate the ID which incremented from my previous ID. ID format is A00001, A00002,... .. I dunno how to auto generate

WeakTaenie
  • 247
  • 2
  • 6
  • 14
  • Where are you getting the ID of the last Assignment from the database? –  Aug 05 '14 at 08:07
  • I dunno how to get the last ID using C#/MVC4 << first time using C# asp.net MVC 4 not familiar with it – WeakTaenie Aug 05 '14 at 08:19
  • What is you method for returning the collection of all Assignments? For example `IEnumerable assignments = db.Assignments` –  Aug 05 '14 at 08:50
  • Erm I no yet get the last ID, because I not sure where to to catch it – WeakTaenie Aug 05 '14 at 08:59

3 Answers3

0

I misunderstood your question at first.

Do you need the identity to be a string with first letter A? You could just use identity property of your database for auto increment and use get to build the string if you need it for visual purposes?

The following code is for code first migration. If you are using database first method then no need for [Key] and [NotMapped] annotations. In that case just create Id property with identity(1,1).

public partial class Assignment
{
    public Assignment()
    {
        this.CourseAvailables = new HashSet<CourseAvailable>();
    }

    [Key]
    private int _id; // Auto incremented id

    [NotMapped]
    public string AssignmentID
    {
        get
        {
            return "A" + _id.ToString("D5");
        }
    }
    public DateTime? SubmissionDate { get; set; }
    public string Status { get; set; }
    public decimal? Mark { get; set; }
    public string Comments { get; set; }
    public string FileLocation { get; set; }

    public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}

This way you will have the reliability of your database auto increment and the flexibility of having a special Id formated inside your class definition.

Edit: Reworked my answer complitely

Tomi Niemenmaa
  • 650
  • 4
  • 12
  • An error occurs, The relationship 'Model.FK__CourseAva__Assig__4222D4EF' was not loaded because the type 'Model.Assignment' is not available., – WeakTaenie Aug 05 '14 at 08:15
0

try this

        db.Assignments.Add(assignment);
        db.SaveChanges();
        Var AssignmentID = assignment.Id;
Hiral Nayak
  • 1,062
  • 8
  • 15
0

Try this one dude,

con.Open();
            string sqlQuery = "SELECT TOP 1 kode_user from USERADM order by kode_user desc";
            SqlCommand cmd = new SqlCommand(sqlQuery, con);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                string input = dr["kode_user"].ToString();
                string angka = input.Substring(input.Length - Math.Min(3, input.Length));
                int number = Convert.ToInt32(angka);
                number += 1;
                string str = number.ToString("D3");

                txtKodeUser.Text = "USR" + str;
            }
            con.Close();

The code will get the last id within the database, then get only number from the string and increment the number by one. This is the line where the code get number from string:

string angka = input.Substring(input.Length - Math.Min(3, input.Length));