尝试通过存储过程将数据插入表中。继续获取将数据类型nvarchar转换为int的错误

时间:2014-08-14 14:23:56

标签: c# sql-server stored-procedures asp.net-mvc-5

所以我的问题是当我尝试将数据从我的视图传递到我的控制器以通过使用存储过程输入到4个表时我得到'错误将数据类型nvarchar转换为int'sql异常。其中三个表在一个表中有外键,其中两个外键填充下拉列表,最后一个外键将文件上载到目录。

我已尝试更改为字段的数据类型,但仍然会收到错误。我还将控制器代码与只有少一个表的具有相同实现的控制器代码进行了比较,并且代码看起来很好。

我想知道实际导致此错误的原因,以及如何排除故障并解决它。

我的观点特别是外键部分:

                <div class="form-group">
                    @Html.LabelFor(model => model.colourID, "Colour", htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.DropDownList("colourID", String.Empty)
                        @Html.ValidationMessageFor(model => model.colourID)
                    </div>
                </div>

                <div class="editor-label">
                    <label> Upload FCC Logo </label>
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.Upload, new { type = "file" })
                    @Html.ValidationMessageFor(model => model.Upload)
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.formatID, "formatID", htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.DropDownList("formatID", String.Empty)
                        @Html.ValidationMessageFor(model => model.formatID)
                    </div>
                </div>

我的控制器的创建方法:

 public ActionResult Create()
        {
            ViewBag.colourID = new SelectList(db.Colours, "colourDesc", "colourDesc");
            ViewBag.logoDocID = new SelectList(db.FCCLogoDocs, "logoDocID", "logoDocTitle");
            ViewBag.formatID = new SelectList(db.Formats, "formatID", "formatName");
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateFCCLogoViewModel createLogo)
        {
            if (ModelState.IsValid)
            {
                var doc = createLogo.Upload;

                var temp = "";
                var temp2 = "";

                if (doc.ContentLength > 0)
                {
                    var docName = Path.GetFileName(doc.FileName);
                    var path = Path.Combine("/Content/FCCLogoDocs", docName);
                    temp = Path.Combine("/Content/FCCLogoDocs", docName);
                    temp2 = docName;
                    doc.SaveAs(Server.MapPath(path));
                }
                else
                {
                    temp2 = "No documentation available";
                }
                SqlParameter pName = new SqlParameter("@Name", createLogo.name);
                SqlParameter pDesc = new SqlParameter("@Desc", createLogo.desc);
                SqlParameter pYear = new SqlParameter("@Year", createLogo.year);
                SqlParameter pColourID = new SqlParameter("@ColourID", createLogo.colourID);
                SqlParameter pDocPath = new SqlParameter("@DocPath", temp);
                SqlParameter pDocName = new SqlParameter("@DocName", temp2);
                SqlParameter pFormatID = new SqlParameter("@FormatID", createLogo.formatID);

                db.Database.ExecuteSqlCommand("spCreateFCCLogo @Name,@Desc, @Year,@ColourID,@DocPath, @DocName, @FormatID",
                                                pName, pDesc, pYear, pColourID, pDocPath, pDocName, pFormatID);

                db.SaveChanges();

                return RedirectToAction("Index");
            }

            ViewBag.colourID = new SelectList(db.Colours, "colourID", "colourDesc", createLogo.colourID);
            ViewBag.formatID = new SelectList(db.Formats, "formatID", "formatName", createLogo.formatID);
            return View(createLogo);
        }

我的存储过程:

CREATE PROCEDURE [dbo].[spCreateFCCLogo]
    @Name varchar(300),
    @Desc varchar(300),
    @Year DateTime,
    @ColourID int,
    @FormatID int,
    @DocName varchar(300),
    @DocPath varchar(300)
AS

BEGIN
    DECLARE @DocID int

    INSERT INTO [dbo].[FCCLogoDoc]([logoDocPath],[logoDocTitle])
    VALUES(@DocPath,@DocName)

    SELECT @DocID = SCOPE_IDENTITY()

    INSERT INTO [dbo].[FCCLogo]([name],[desc],[year],[colourID],[logoDocID],[formatID])
    VALUES(@Name,@Desc,@Year,@ColourID,@DocID,@FormatID)


END

1 个答案:

答案 0 :(得分:0)

Aaron Bertrand回答了这个问题。问题是与代码相比,存储过程中声明参数的顺序。