在SQL Server错误中添加注释

时间:2015-12-29 13:54:28

标签: sql-server asp.net-mvc sql-insert

当我想在我的数据库中添加注释时,我收到以下错误(订单与数据库中的相同):

$config = \PHRETS\Configuration::load([
    'login_url' => $login_url,
    'username' => $user_name,
    'password' => $user_password,
    'user_agent' => $user_agent,
    'user_agent_password' => $user_agent_password,
    'rets_version' => '1.8',
]);
$rets = new \PHRETS\Session($config);

这是我将项目添加到数据库中的方法。

Add comments method

这是我制作评论对象:

System.Data.SqlClient.SqlException was unhandled by user code
  Class=16
  ErrorCode=-2146232060
  HResult=-2146232060
  LineNumber=0
  Message=The parameterized query '(@UserId nvarchar(36),@TimeStamp datetime,@CategoryId int,@PostI' expects the parameter '@Comment', which was not supplied.
  Number=8178
  Procedure=""
  Server=MAXIM-HP\DATAMANAGEMENT
  Source=.Net SqlClient Data Provider
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at BlogMaxim.Repositories.CommentRepository.Add(Comments comment) in C:\nmct\2e Jaar\1e sem\project\BlogMaxim\BlogMaxim\Repositories\CommentRepository.cs:line 98
       at BlogMaxim.Controllers.CommentController.AddComment(Comments comment) in C:\nmct\2e Jaar\1e sem\project\BlogMaxim\BlogMaxim\Controllers\CommentController.cs:line 43
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
  InnerException: 

这是我的表格:

namespace BlogMaxim.Models {
    public class Comments
    {
        public int Id { get; set; }
         public int CategoryId { get; set; }
         public int PostId { get; set; }
        public string Comment { get; set; }
         public string UserId { get; set; }
         public DateTime Timestamp { get; set; }
    } }

1 个答案:

答案 0 :(得分:0)

您在评论对象的Comment属性中获得了NULL值。

查看表单标记时,您的注释输入字段看起来与您在viewmodel中的名称不同( Description )。要使MVC模型绑定正常工作,您的输入字段名称应与您的模型/视图模型属性名称匹配。

将评论文本框的名称属性值更改为“Comment”,它应该有效。

<form method="post" action="/comment/addcomment">      
   <input type="text" name="Comment" />
   <input type="hidden" name="PostId" value="@ViewBag.pId" />
   <input type="submit" />
</form>

更进一步,每当出现此类错误时,您应该尝试放置breakpoints in your code并尝试调试并查看变量的值。这将帮助您自己解决很多问题。

另外,我注意到的另一件事是,您正在使用comment.CategoryId,但在您的表单中,您没有任何输入字段来设置CategoryId的值。除非它是数据库中的NULLABLE字段,否则在保存注释之前,您还应该从视图中传递该值/将该值设置到注释对象。

如果要设置为查看

<form method="post" action="/comment/addcomment">      
   <input type="text" name="Comment" />
   <input type="hidden" name="PostId" value="@ViewBag.pId" />
   <input type="hidden" name="CategoryId" value="1" />
   <input type="hidden" value="@ViewBag.pId" />
   <input type="submit" />
</form>

我在表单中将CategoryId的值硬编码为1。您可以从某处读取并设置它。

此外,您正在使用comment.UserId。因此,请确保在运行保存代码之前设置该值。