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




            }

    }
    }
}

I am getting the following error when I hit the submit button:

Error: The parameterized query '(@Title nvarchar(4),@Author nvarchar(4),@Body nvarchar(4),@Posti' expects the parameter '@LPID', which was not supplied.

Any idea what could be wrong here? Thank you for your help.

1 Answers1

1

If LPID is allowed to be null, then you need to do this:

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

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

That will provide a null value which is acceptable by the database.

If no null values are allowed then do not insert. Here is the full code:

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;
    }
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Hi there, thank you for your help. PAPER ID should not be 0. The previous page is a database list and PAPER ID should be the ID for the entry opened from the database. The code above should allow the user to add a comment to a selected database entry..... – Stefan Schwarzer Jan 02 '17 at 21:50
  • 0 is not the same as null. If you do not allow nulls then check the value of `Request.QueryString["PaperId"]` and if it is null, do not insert into the database. – CodingYoshi Jan 02 '17 at 22:07
  • How can I check the query if it is null? In general, the value should be defined as PaperID is the ID for the entry opened from the database..... – Stefan Schwarzer Jan 03 '17 at 00:55
  • See my edit please. – CodingYoshi Jan 03 '17 at 01:01
  • Thanks Mate. I will try this.... – Stefan Schwarzer Jan 03 '17 at 01:52
  • HI Mate, I have just tried the code above and still get the error message: Error: The parameterized query '(@Title nvarchar(4),@Author nvarchar(4),@Body nvarchar(4),@Posti' expects the parameter '@LPID', which was not supplied. – Stefan Schwarzer Jan 08 '17 at 01:33
  • Put your whole method's code on pastebin and share the url with me. I will see what the issue is. – CodingYoshi Jan 08 '17 at 01:46
  • HI Mate, What a beginners mistake. I opened the page straight away instead of accessing it from the list so the paperID can be transferred through. It works perfectly when accessing the page through the list. – Stefan Schwarzer Jan 08 '17 at 01:46
  • That is great. I knew something weird was going on. Glad it is resolved. If my answer helped you at all, please read [this](http://stackoverflow.com/help/someone-answers). – CodingYoshi Jan 08 '17 at 01:50