System.Data.SQLite.SQLiteException:'在“ =”附近的SQL逻辑错误:语法错误“

时间:2019-06-17 22:43:39

标签: c# asp.net-mvc sqlite

我正在尝试建立一个MVC网站,允许用户使用sqlite从数据库中骗取客户和发票条目。我的编辑功能抛出

System.Data.SQLite.SQLiteException:'SQL逻辑错误 靠近“ =”:语法错误“

当我尝试使用索引视图和a

中的链接时出现错误
The parameters dictionary contains a null entry for parameter 'customerId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'FinalProject.Controllers.CustomerController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

当我尝试从“编辑”视图运行该东西时出错

我环顾了Google,似乎找不到与我相关的答案

这是“编辑”视图:

@model FinalProject.Models.CustomerModel

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

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

<div class="form-horizontal">
    <h4>CustomerModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    @Html.HiddenFor(model => model.CustomerId, new { htmlAttributes = new { @class = "form-control" } })

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

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

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

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

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这是CustomerController中的Edit ActionResult:

public ActionResult Edit(int customerId)
        {
            CustomerAdapter customerAdapter = new CustomerAdapter();
            Customer customer = customerAdapter.GetById(customerId);
            if (customer == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                CustomerModel model = new CustomerModel();
                model.CustomerId = customer.CustomerId;
                model.FirstName = customer.FirstName;
                model.LastName = customer.LastName;
                model.Country = customer.Country;
                model.Email = customer.Email;
                return View(model);
            }
        }
        //handles data from form
        [HttpPost]
        public ActionResult Edit(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                CustomerAdapter customerAdapter = new CustomerAdapter();
                Customer customer = new Customer();
                customer.FirstName = model.FirstName;
                customer.LastName = model.LastName;
                customer.CustomerId = model.CustomerId;
                customer.Country = model.Country;
                customer.Email = customer.Email;
                bool returnValue = customerAdapter.UpdateCustomer(customer);
                if (returnValue)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            return View(model);
        }

最后是CustomerAdapter,我从这里得到语法错误:

 public Customer GetById(int customerId)
        {
            Customer returnValue = null;
            using (SQLiteConnection connection = new SQLiteConnection(_connectionString))
            {
                SQLiteCommand command = connection.CreateCommand();
                command.CommandText = "SELECT CustomerId, FirstName, LastName, Country, Email FROM Customer WHERE" +
                    "CustomerId = " + customerId.ToString();
                connection.Open();
                SQLiteDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    returnValue = GetFromReader(reader);
                }
                return returnValue;
            }
        }

当您单击编辑链接时,我希望该链接能够编辑数据库中的任何条目。我猜想从“编辑”视图运行网站时遇到错误,因为我没有给它提供ID,但我至少需要它可以从实际条目旁边的链接开始工作。

非常感谢您的帮助

0 个答案:

没有答案