tags:

views:

143

answers:

3

I am getting the following error in my MVC application.

The parameters dictionary contains a null entry for parameter 'isFeature' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddProduct(System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, Int32)' in 'ExportJquerygridtoexcel.Controllers.AdminController'. To make a parameter optional its type should be either a reference type or a Nullable type. Parameter name: parameters

i am inserting a product in the database and one of my field is isFeature which is of type int in database

public ActionResult AddProduct(string Categories, string product_name, string product_price, string product_desc, string weight, string image_name, string img_content, string available_qty, int isFeature) { string s1, type;

        foreach (string inputTagName in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[inputTagName];
            if (file.ContentLength > 0)
            {
                image_name = Path.GetFileName(file.FileName);
                img_content = Path.GetFileName(file.ContentType);

                file.SaveAs(HttpContext.Server.MapPath("../Content/upload_ProdImg/") + image_name);
                System.Drawing.Size r = new Size(80, 80);
                System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath("../Content/upload_ProdImg/") + image_name);
                System.Drawing.Image tnImage = srcImage.GetThumbnailImage(r.Width, r.Height, null, IntPtr.Zero);
                System.Drawing.Graphics graphic = Graphics.FromImage(tnImage);
                System.Drawing.Rectangle rect = new Rectangle(0, 0, r.Width, r.Height);
                graphic.DrawImage(tnImage, rect);
                s1 = HttpContext.Server.MapPath("../Content/thumb_ProdImg/") + image_name;
                tnImage.Save(s1);
                type = img_content;

                //string filePath = Path.Combine(HttpContext.Server.MapPath("/Content/thumb_ProdImg/"), Path.GetFileName(file.FileName));
                // string filecontent = Path.Combine(HttpContext.Server.MapPath("/Content/Uploads"), Path.GetFileName(file.ContentType));

                //image_name = Path.GetFileName(file.FileName);
                //img_content = Path.GetFileName(file.ContentType);

              // file.SaveAs(filePath);

            }
        }


        AdminImplementation _adminImplementation = new AdminImplementation();



        Boolean isfeature = Convert.ToBoolean(isFeature);

        if (isfeature)
        {


            isFeature = 0;

        }
        else
        {
            isFeature = 1;

        }



        int i = _adminImplementation.addproduct(Categories, product_name, product_price, product_desc, weight, image_name, img_content, available_qty, isFeature);

        ViewData["succm"] = "Product added successfully";
        return View ();

    }

implementation class code is as follows:

public int addproduct(string categories, string prodName, string price, string prodDesc, string weight, string image_name, string img_content, string qty, int isFeature) { string s1 = string.Empty; string s = string.Empty; int itemInserted = 0; using (EcommerceMVCEntities modelObject = new EcommerceMVCEntities()) {

            tbl_PRODUCTS NewItemToInsert = new tbl_PRODUCTS();
            //NewItemToInsert.tbl_PRODUCT_CATEGORYReference.  =Convert.ToInt32( categories);
            NewItemToInsert.product_name = prodName;
            NewItemToInsert.product_price = Convert.ToDouble (price);
            NewItemToInsert.product_desc = prodDesc;
            NewItemToInsert.weight = weight;
            NewItemToInsert.image_name = image_name;
            NewItemToInsert.img_content = img_content;
            NewItemToInsert.available_qty = qty;
            NewItemToInsert.isFeature = isFeature;

            modelObject.AddTotbl_PRODUCTS(NewItemToInsert);
            itemInserted = modelObject.SaveChanges();
            modelObject.AcceptAllChanges();

        }
        return itemInserted;
    }

view page is as follows:

<%-- Category: <%= Html.DropDownList("Categories", (IEnumerable)ViewData["Categories"])%>

                </td>
            </tr>--%>
            <tr>
                <td align ="right" >
                    <label for="product_name">Product Name:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <%= Html.TextBox("product_name") %>
                    <%= Html.ValidationMessage("product_name", "*") %>
                </td>
            </tr>
             <tr>
                <td align ="right" >
                     <label for="product_desc">Description:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <%= Html.TextBox("product_desc") %>
                    <%= Html.ValidationMessage("product_desc", "*") %>
                </td>
            </tr>
            <tr>
                <td align ="right" >
                    <label for="product_price">Price:<font color="red">*</font></label> 
                </td>
                <td align ="left"> 
                    <%= Html.TextBox("product_price") %>
                    <%= Html.ValidationMessage("product_price", "*") %>
                </td>
            </tr>

            <tr>
                <td align ="right" >
                    <label for="weight">Weight:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <%= Html.TextBox("weight") %>
                    <%= Html.ValidationMessage("weight", "*") %>
                </td>
            </tr>
            <tr>
                <td align ="right" >
                    <label for="image_name">Image:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <input type ="file" name ="upload" id ="imgfile" runat ="server"  />
                    <%= Html.ValidationMessage("image_name", "*") %>
                </td>
            </tr>
            <tr>
                <td align ="right" >
                    <label for="available_qty">Quantity:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <%= Html.TextBox("available_qty") %>
                    <%= Html.ValidationMessage("available_qty", "*") %>
                </td>
            </tr>
            <tr>
                <td align ="right" >
                    <label for="isFeature">IsFeature:<font color="red">*</font></label>
                </td>
                <td align ="left">
                    <%= Html.CheckBox("isFeature") %>
                    <%= Html.ValidationMessage("isFeature", "*") %>
                </td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td align ="left">
                    <input type="submit" value="Add Product" />
                    <input type ="reset" value ="Clear" />
                </td>
            </tr>
        </table>

Does anyone have an idea as to what I am doing wrong?

A: 

try Int32? as the parameter type.

An integer is not nullable. By adding the '?' you are saying it is nullable.

Richard Lennox
A: 

make sure that function you are calling gets all filled parameters (not null). You need to provide example of buggy code just in case.

Sarfraz
A: 

isFeature comes from a Checkbox. If it isn't checked, nothing gets posted. Make the parameter nullable, and check the value before assigning it if your db field is set to NOT NULL:

NewItemToInsert.isFeature = (isFeature == null? 0:1);
MikeB