错误:参数化查询需要参数' @LPID',这是未提供的

时间:2017-01-02 04:48:31

标签: asp.net .net

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;




            }

    }
    }
}

点击提交按钮时出现以下错误:

错误:参数化查询'(@ Title nvarchar(4),@ Author nvarchar(4),@ Body nvarchar(4),@ Posti'需要参数' @LPID&#39 ;,没有提供。

知道这里有什么问题吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

如果允许LPID为null,则需要执行此操作:

var lpid = Request.QueryString["PaperId"];

if (string.IsNullOrWhitespace(lpid))
{
    insert.Parameters.AddWithValue("@LPID", DBNull.Value);
}

这将提供数据库可接受的空值。

如果不允许空值,则不要插入。这是完整的代码:

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;
    }
}