Search code examples
c#htmlsql-serverasp.net-mvcrazor

my mvc controller for create operation is not uploading data to sql server


I have to upload simple data to my server but in create page after I enter data and submit it, it doesn't no upload the data to database but it works fine as it returns to index menu and shows all other entries in database just it won't upload any new data

Create page

No Changes occur to database but data s fetched properly

following are the codes

Html for 'create page'

 @model SnackoEntities.DealMasterInfo


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        <div class="form-group">
            @Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CreatedBy, "", 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>

My controller

[HttpGet]
[ActionName("Create")]
public ActionResult Create_get()
{
    return View();
}

[HttpPost]
[ActionName("Create")]
public ActionResult Create_post(DealMasterInfo dealMasterInfo)
{
    if (ModelState.IsValid)
    {
        //DealMasterInfo dealMasterInfo = new DealMasterInfo();
        UpdateModel(dealMasterInfo);
        DealMasterBLL dealMasterBLL = new DealMasterBLL();
        dealMasterBLL.AddDealMaster(dealMasterInfo);
        if (dealMasterInfo != null)
        {

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

Bll code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using SnackoEntities;

namespace BusinesssLayer
{
   public class DealMasterBLL
    {
        public IEnumerable<DealMasterInfo> DealMasterInfo
        {
            get
            {
                string connectionString = ConfigurationManager.ConnectionStrings["DealMaster"].ConnectionString;

                List<DealMasterInfo> dealMasterInfos = new List<DealMasterInfo>();

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spGetDealMasterInfo", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    SqlDataReader rdr = cmd.ExecuteReader(); 
                    while (rdr.Read())
                    {
                        DealMasterInfo dealMasterInfo = new DealMasterInfo();
                        dealMasterInfo.DealAutoID = rdr["DealAutoID"].ToString();
                        dealMasterInfo.FK_RestoID = rdr["FK_RestoID"].ToString();
                        dealMasterInfo.FK_SnFoodID = rdr["FK_SnFoodID"].ToString();
                        dealMasterInfo.DealDisPerc = rdr["DealDisPerc"].ToString();
                        dealMasterInfo.FK_RewaID = rdr["FK_RewaID"].ToString();
                        dealMasterInfo.startDate = Convert.ToDateTime(rdr["startDate"]);
                        dealMasterInfo.endDate = Convert.ToDateTime(rdr["endDate"]);
                        dealMasterInfo.ValidOn = rdr["ValidOn"].ToString();
                        dealMasterInfo.UserTypes = rdr["UserTypes"].ToString();
                        dealMasterInfo.DealCode = rdr["DealCode"].ToString();
                        dealMasterInfo.DealPic = rdr["DealPic"].ToString();
                        dealMasterInfo.DealStatus = rdr["DealStatus"].ToString();
                        dealMasterInfo.CreatedOn = Convert.ToDateTime(rdr["CreatedOn"]);
                        dealMasterInfo.ModifiedOn = Convert.ToDateTime(rdr["ModifiedOn"]);
                        dealMasterInfo.CreatedBy = rdr["CreatedBy"].ToString();

                        dealMasterInfos.Add(dealMasterInfo);
                    }
                }

                return dealMasterInfos;
            }
        }

        public void AddDealMaster(DealMasterInfo dealMasterInfo)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DealMaster"].ConnectionString;

            try
            {
                using(SqlConnection con=new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand("spAddDealMaster", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter paramDealAutoID = new SqlParameter();
                    paramDealAutoID.ParameterName = "@DealAuto";
                    paramDealAutoID.Value = dealMasterInfo.DealAutoID;
                    cmd.Parameters.Add(paramDealAutoID);

                    SqlParameter paramFK_RestoID = new SqlParameter();
                    paramFK_RestoID.ParameterName = "@FK_RestoID";
                    paramFK_RestoID.Value = dealMasterInfo.FK_RestoID;
                    cmd.Parameters.Add(paramFK_RestoID);

                    SqlParameter paramFK_SnFoodID = new SqlParameter();
                    paramFK_SnFoodID.ParameterName = "@FK_SnFoodID";
                    paramFK_SnFoodID.Value = dealMasterInfo.FK_SnFoodID;
                    cmd.Parameters.Add(paramFK_SnFoodID);

                    SqlParameter paramFK_RoFoodID = new SqlParameter();
                    paramFK_RoFoodID.ParameterName = "@FK_RoFoodID";
                    paramFK_RoFoodID.Value = dealMasterInfo.FK_RoFoodID;
                    cmd.Parameters.Add(paramFK_RoFoodID);

                    SqlParameter paramDealDisPerc = new SqlParameter();
                    paramDealDisPerc.ParameterName = "@DealDisPerc";
                    paramDealDisPerc.Value = dealMasterInfo.DealDisPerc;
                    cmd.Parameters.Add(paramDealDisPerc);

                    SqlParameter paramFK_RewaID = new SqlParameter();
                    paramFK_RewaID.ParameterName = "@FK_RewaID";
                    paramFK_RewaID.Value = dealMasterInfo.FK_RewaID;
                    cmd.Parameters.Add(paramFK_RewaID);

                    SqlParameter paramstartDate = new SqlParameter();
                    paramstartDate.ParameterName = "@startDate";
                    paramstartDate.Value = dealMasterInfo.startDate;
                    cmd.Parameters.Add(paramstartDate);

                    SqlParameter paramendDate = new SqlParameter();
                    paramendDate.ParameterName = "@endDate";
                    paramendDate.Value = dealMasterInfo.endDate;
                    cmd.Parameters.Add(paramendDate);

                    SqlParameter paramValidOn = new SqlParameter();
                    paramValidOn.ParameterName = "@ValidOn";
                    paramValidOn.Value = dealMasterInfo.ValidOn;
                    cmd.Parameters.Add(paramValidOn);

                    SqlParameter paramUserTypes = new SqlParameter();
                    paramUserTypes.ParameterName = "@UserTypes";
                    paramUserTypes.Value = dealMasterInfo.UserTypes;
                    cmd.Parameters.Add(paramUserTypes);

                    SqlParameter paramDealCode = new SqlParameter();
                    paramDealCode.ParameterName = "@DealCode";
                    paramDealCode.Value = dealMasterInfo.DealCode;
                    cmd.Parameters.Add(paramDealCode);

                    SqlParameter paramDealPic = new SqlParameter();
                    paramDealPic.ParameterName = "@DealPic";
                    paramDealPic.Value = dealMasterInfo.DealPic;
                    cmd.Parameters.Add(paramDealPic);

                    SqlParameter paramDealStatus = new SqlParameter();
                    paramDealStatus.ParameterName = "@DealStatus";
                    paramDealStatus.Value = dealMasterInfo.DealStatus;
                    cmd.Parameters.Add(paramDealStatus);

                    SqlParameter paramCreatedOn = new SqlParameter();
                    paramCreatedOn.ParameterName = "@CreatedOn";
                    paramCreatedOn.Value = dealMasterInfo.CreatedOn;
                    cmd.Parameters.Add(paramCreatedOn);

                    SqlParameter paramModifiedOn = new SqlParameter();
                    paramModifiedOn.ParameterName = "@ModifiedOn";
                    paramModifiedOn.Value = dealMasterInfo.ModifiedOn;
                    cmd.Parameters.Add(paramModifiedOn);

                    SqlParameter paramCreatedBy = new SqlParameter();
                    paramCreatedBy.ParameterName = "@CreatedBy";
                    paramCreatedBy.Value = dealMasterInfo.CreatedBy;
                    cmd.Parameters.Add(paramCreatedBy);

                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            catch(Exception ex){
                Console.WriteLine("SQL Error" + ex.Message.ToString());
            }

        }

    }
}

entity class

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnackoEntities
{
    public class DealMasterInfo
    {
        public string DealAutoID { get; set; }
        public string FK_RestoID { get; set; }
        public string FK_SnFoodID { get; set; }
        public string FK_RoFoodID { get; set; }
        public string DealDisPerc { get; set; }
        public string FK_RewaID { get; set; }
        public DateTime startDate { get; set; }
        public DateTime endDate { get; set; }
        public string ValidOn { get; set; }
        public string UserTypes { get; set; }
        public string DealCode { get; set; } 
        public string DealPic { get; set; }
        public string DealStatus { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime ModifiedOn { get; set; }
        public string CreatedBy { get; set; }
    }
}

I am learning MVC and this is my first project, so please with the solution give an explanation in easy language so that I can understand the problem in future


Solution

  • There was a spelling mistake in the code due to which it wasn't working the

    SqlParameter paramDealDisPerc = new SqlParameter();
                    paramDealDisPerc.ParameterName = "@DealDisPerc";
                    paramDealDisPerc.Value = dealMasterInfo.DealDisPerc;
                    cmd.Parameters.Add(paramDealDisPerc);
    

    here the DealDiscPerc Should have been DealDiscPerc

    SqlParameter paramDealDisPerc = new SqlParameter();
                    paramDealDisPerc.ParameterName = "@DealDiscPerc";
                    paramDealDisPerc.Value = dealMasterInfo.DealDisPerc;
                    cmd.Parameters.Add(paramDealDisPerc);
    

    did this mistake because my table had same variable without "C" whereas while writing SP i added a "C" and this caused the confusion this solved the issue