0

Is it possible for a controller to check if record exists in database using ADO.NET?

Here is my controller

[HttpPost]
public ActionResult Add(TemporaryVoucher temporaryVoucher)
{
    string fileName = Path.GetFileNameWithoutExtension(temporaryVoucher.ImageFile.FileName);
    string extension = Path.GetExtension(temporaryVoucher.ImageFile.FileName);
    fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;

    /*temporaryVoucher.VoucherPath = "/Image/" + fileName;
    fileName = Path.Combine(Server.MapPath("/Image/"), fileName);*/
    temporaryVoucher.VoucherPath = fileName;

    using (DBModels db = new DBModels())
    {
        db.TemporaryVouchers.Add(temporaryVoucher);
        db.SaveChanges();
    }

    ModelState.Clear();
    return View();
}

Here is my ado.net in model

public partial class TemporaryVoucher
{
    public int PromoKey { get; set; }
    public string PromoName { get; set; }
    [DisplayName("Upload Image")]
    public string VoucherPath { get; set; }

    public HttpPostedFileBase ImageFile { get; set; }
}

Here is my view

<div class="form-group">
        @*Html.LabelFor(model => model.ImageName)*@
        <label name="PromoName" style="text-align: right; clear: both; float:left;margin-right:15px;">PromoName</label>
        <div class="col-md-10">
            @*Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control", required = "required" } })
                @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })*@
            <input type="text" name="PromoName" id="txtInput" onkeypress="return checkSpcialChar(event)" required />
        </div>
    </div>
    <div class="form-group">
        @*Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" })*@
        <label name="ImagePath" style="text-align: right; clear: both; float:left;margin-right:15px;margin-top:5px;">Upload Image</label>
        <div class="col-md-10">
            <input type="file" style="margin-top:5px;" name="ImageFile" accept=".jpg,.jpeg,.png" required />
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Upload" style="margin-top:5px;" class="btn btn-default" />
        </div>
    </div>
</div>

I just want to ensure no possible duplication for PromoName in the database

3
  • 3
    on what criteria are you judging duplication Commented Apr 29, 2019 at 3:11
  • 1
    also, your TemporaryVoucher is partial class, what is in another part? Commented Apr 29, 2019 at 3:31
  • 1
    You can use LINQ for this. db.TemporaryVouchers.FirstOrDefault. You can read about it learn.microsoft.com/en-us/dotnet/api/… Commented Apr 29, 2019 at 3:42

1 Answer 1

1

You can perform a 'get' within your controller action based on any identity column to check for an existing value in the database. If present, return with a message, else add the incoming model to the database. Something like this:


using (DBModels db = new DBModels())
{   
    TemporaryVoucher tv = (from t1 in db.TemporaryVouchers
                           where t1.PromoKey == temporaryVoucher.PromoKey  // any identifier comparison can be done here
                           select t1).FirstOrDefault();
    if(tv != null)
        // return with a message that incoming temporary voucher already exists
    db.TemporaryVouchers.Add(temporaryVoucher);
    db.SaveChanges();
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.