Muhammad Ahmed Arif
Muhammad Ahmed Arif

Reputation: 1

Save checkbox data in SQL Server using MVC

I use MVC and want to save the checkbox data in boolean form in sql server. The error is in the Customer Form Model when I try to use the Razor view for the checkboxes model.

The error is:

CS0266: Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

Controller Code:

public ActionResult SaveRecord(CustomerCount cc)
{
    try
    {
        CustomerCounterDBEntities1 dbs = new CustomerCounterDBEntities1();
        List<CustomerInfo> infos = dbs.CustomerInfoes.ToList();
        ViewBag.CustomerInfoList = new SelectList(infos, "Name", "Mobile");
        CustomerInfo ct = new CustomerInfo();
        ct.CustomerID = cc.CustomerID;
        ct.Name = cc.Name;
        ct.Mobile = cc.Mobile;
        ct.Email = cc.Email;
        ct.Comments = cc.Comments;
        ct.Western_Union = cc.Western_Union;
        ct.Ria = cc.Ria;
        ct.Money_Gram = cc.Money_Gram;
        ct.Intel = cc.Intel;
        ct.JazzCash = cc.JazzCash;
        ct.Contact = cc.Contact;
        ct.No_Business = cc.No_Business;
        dbs.CustomerInfoes.Add(ct);
        dbs.SaveChanges();
        int CustomerID = ct.CustomerID;
        return RedirectToAction("Index");
    }

Model Code:

namespace Customer_Counter.Models
{
    public class CustomerCount
    {
        [Key]
        public int CustomerID { get; set; }

        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
        public Boolean Western_Union { get; set; }
        public Boolean Ria { get; set; }
        public Boolean Money_Gram { get; set; }
        public Boolean Intel { get; set; }
        public Boolean JazzCash { get; set; }
        public Boolean Contact { get; set; }
        public Boolean No_Business { get; set; }
    }
}

CustomerInfo:

namespace Customer_Counter.Models
{
    using System;
    using System.Collections.Generic;

    public partial class CustomerInfo
    {
        public int CustomerID { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
        public string Comments { get; set; }
        public Nullable<bool> Western_Union { get; set; }
        public Nullable<bool> Ria { get; set; }
        public Nullable<bool> Money_Gram { get; set; }
        public Nullable<bool> Intel { get; set; }
        public Nullable<bool> JazzCash { get; set; }
        public Nullable<bool> Contact { get; set; }
        public Nullable<bool> No_Business { get; set; }
    }
}

CustomerForm View: //Only the error part

@Html.CheckBoxFor(Model => Model.Western_Union)
@Html.CheckBoxFor(Model => Model.Ria)//error
@Html.CheckBoxFor(Model => Model.Money_Gram)//error
@Html.CheckBoxFor(Model => Model.Intel)//error
@Html.CheckBoxFor(Model => Model.JazzCash)//error
@Html.CheckBoxFor(Model => Model.Contact)//error
@Html.CheckBoxFor(Model => Model.No_Business)//error

Upvotes: 0

Views: 843

Answers (3)

Rafalon
Rafalon

Reputation: 4515

You have this in your CustomerCount class (=SQL table?):

public Boolean Western_Union { get; set; }
public Boolean Ria { get; set; }
public Boolean Money_Gram { get; set; }
public Boolean Intel { get; set; }
public Boolean JazzCash { get; set; }
public Boolean Contact { get; set; }
public Boolean No_Business { get; set; }

but this in your CustomerInfo class (=model?):

public Nullable<bool> Western_Union { get; set; }
public Nullable<bool> Ria { get; set; }
public Nullable<bool> Money_Gram { get; set; }
public Nullable<bool> Intel { get; set; }
public Nullable<bool> JazzCash { get; set; }
public Nullable<bool> Contact { get; set; }
public Nullable<bool> No_Business { get; set; }

So either remove all those Nullable, or convert the value:

// Explicit conversion as suggested by your error message:
ct.Western_Union = (bool)cc.Western_Union;
// Function getting the boolean value or false if null:
ct.Western_Union = cc.Western_Union.GetValueOrDefault();

You could also get rid of your CustomerInfo class and use CustomerCount as your model.


Edit: Hiba T solved your problem (CheckBoxFor has this overload:

CheckBoxFor<TModel>(HtmlHelper<TModel>, Expression<Func<TModel,Boolean>>)

note Boolean, not Nullable<Boolean>), but you really still should ask yourself why you use two (almost identical) classes for the same stuff.

Upvotes: 1

Hiba T
Hiba T

Reputation: 41

ur varible should be true or false (not nullable), check ur database and this link might help u : CheckBoxFor

Upvotes: 0

Jonidas
Jonidas

Reputation: 187

A "?" indicates a nullable type. A bool variable can be true or false. A nullable bool variable can be true, false or null. So mapping from a nullable bool to a non-nullable bool can not be done implicitly.

But to be honest, I think you need to do some reading on c#, MVC and sql in general ... your approach is a bit confusing, at least to me.

Upvotes: 0

Related Questions