Asp.net 错误:参数化查询需要参数'@LPID';,这是没有提供的
当我点击提交按钮时,出现以下错误: 错误:参数化查询“(@Title-nvarchar(4),@Author-nvarchar(4),@Body-nvarchar(4),@Posti”需要未提供的参数“@LPID”Asp.net 错误:参数化查询需要参数'@LPID';,这是没有提供的,asp.net,.net,Asp.net,.net,当我点击提交按钮时,出现以下错误: 错误:参数化查询“(@Title-nvarchar(4),@Author-nvarchar(4),@Body-nvarchar(4),@Posti”需要未提供的参数“@LPID” 知道这里可能有什么问题吗?谢谢您的帮助。如果允许LPID为空,则需要执行以下操作: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We
知道这里可能有什么问题吗?谢谢您的帮助。如果允许
LPID
为空,则需要执行以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class detail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request.QueryString["PaperId"]);
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
protected void btnCommentSubmit_Click(object sender, EventArgs e)
{
string connStr = "Data Source=jose.stca.herts.ac.uk;Initial Catalog=dbss15ahd;Persist Security Info=True;User ID=XXXXX;Password=XXXXX";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand insert = new SqlCommand("insert into Comment(Title, Body, Author, PostingTime, LPID) values(@Title, @Body,@Author,@PostingTime,@LPID)", conn);
insert.Parameters.AddWithValue("@Title", tbCommentTitle.Text);
insert.Parameters.AddWithValue("@Author", tbCommentAuthor.Text);
insert.Parameters.AddWithValue("@Body", tbCommentBody.Text);
insert.Parameters.AddWithValue("@PostingTime", DateTime.Now);
insert.Parameters.AddWithValue("@LPID", Request.QueryString["PaperId"]);
try
{
conn.Open();
insert.ExecuteNonQuery();
}
catch (Exception ex)
{
lbl_msg.Text = "Error: " + ex.Message;
}
}
}
}
这将提供一个数据库可以接受的空值
如果不允许空值,则不要插入。以下是完整代码:
var lpid = Request.QueryString["PaperId"];
if (string.IsNullOrWhitespace(lpid))
{
insert.Parameters.AddWithValue("@LPID", DBNull.Value);
}
可能
Request.QueryString[“PaperId”]
没有任何值。@Tarod否,这不是原因。在这种情况下,将向其传递null。@Tarod对不起,你是对的,我的错。@CodingYoshi null仅在传递DbNull.value时才会传递。如果QueryString值为null,则参数将忽略dhi,谢谢你的帮助。纸张ID不应为0。上一页为s数据库列表和纸张ID应为从数据库打开的条目的ID。上面的代码应允许用户向选定的数据库条目添加注释…..0与null不同。如果不允许null,请检查Request.QueryString[“PaperId”]的值
如果为空,请不要插入数据库。如果为空,我如何检查查询?通常,该值应定义为PaperID,即从数据库打开的条目的ID…..请参见我的编辑。谢谢,伙计。我将尝试此。。。。
protected void btnCommentSubmit_Click(object sender, EventArgs e)
{
var lpid = Request.QueryString["PaperId"];
if (string.IsNullOrWhitespace(lpid))
{
// You said nulls are not allowed so return right away
return;
}
// lpid is not null so we should be good now to insert
string connStr = "Data Source=jose.stca.herts.ac.uk;Initial Catalog=dbss15ahd;Persist Security Info=True;User ID=XXXXX;Password=XXXXX";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand insert = new SqlCommand("insert into Comment(Title, Body, Author, PostingTime, LPID) values(@Title, @Body,@Author,@PostingTime,@LPID)", conn);
insert.Parameters.AddWithValue("@Title", tbCommentTitle.Text);
insert.Parameters.AddWithValue("@Author", tbCommentAuthor.Text);
insert.Parameters.AddWithValue("@Body", tbCommentBody.Text);
insert.Parameters.AddWithValue("@PostingTime", DateTime.Now);
insert.Parameters.AddWithValue("@LPID", lpid);
try
{
conn.Open();
insert.ExecuteNonQuery();
}
catch (Exception ex)
{
lbl_msg.Text = "Error: " + ex.Message;
}
}