【问题标题】:System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@TxtTitle".'System.Data.SqlClient.SqlException:'必须声明标量变量“@TxtTitle”。'
【发布时间】:2020-10-20 18:15:24
【问题描述】:

单击提交按钮后,我收到此错误:System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@TxtTitle".' 此错误出现在 cmd.ExecuteNonQuery() 旁边请有人帮我解决这个问题吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace RegistrationForm5
{
    public partial class Registration : System.Web.UI.Page
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = (@"Data Source=DESKTOP-20J5J59;Initial Catalog=RegistrationWebForm1;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
            con.Open();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("insert into reg" + "(Title, Name, ID, Email, Mobile, Major, Note)values(@TxtTitle, @TxtName, @TxtID, @TxtEmail, @TxtMobile, @LstMajor, @TxtNote)", con);


            cmd.Parameters.AddWithValue("@Title", TxtTitle.Text);
            cmd.Parameters.AddWithValue("@Name", TxtName.Text);
            cmd.Parameters.AddWithValue("@ID", TxtID.Text);
            cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
            cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
            cmd.Parameters.AddWithValue("@Major", LstMajor.SelectedItem.ToString());
            cmd.Parameters.AddWithValue("@Note", TxtNote.Text);
            cmd.ExecuteNonQuery();
            Label1.Text = "Your request has been submitted successfully";

            con.Close();
        }
    }
}

【问题讨论】:

  • 您的 SqlCommand 引用了 @TxtTitle,但参数被添加为 @Title您忘记了前导 Txt)。其余所有参数也存在类似的不匹配。

标签: javascript jquery asp.net sql-server webforms


【解决方案1】:

这里的问题是cmd 变量,您添加了参数@Title,正确的是@TxtTitle,我看到其他的都不好,最终代码可能是这样的:

protected void Button1_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand("insert into reg" + "(Title, Name, ID, Email, Mobile, Major, Note)values(@TxtTitle, @TxtName, @TxtID, @TxtEmail, @TxtMobile, @LstMajor, @TxtNote)", con);

    cmd.Parameters.AddWithValue("@TxtTitle", TxtTitle.Text);
    cmd.Parameters.AddWithValue("@TxtName", TxtName.Text);
    cmd.Parameters.AddWithValue("@TxtID", TxtID.Text);
    cmd.Parameters.AddWithValue("@TxtEmail", TxtEmail.Text);
    cmd.Parameters.AddWithValue("@TxtMobile", TxtMobile.Text);
    cmd.Parameters.AddWithValue("@LstMajor", LstMajor.SelectedItem.ToString());
    cmd.Parameters.AddWithValue("@TxtNote", TxtNote.Text);
    cmd.ExecuteNonQuery();
    Label1.Text = "Your request has been submitted successfully";

    con.Close();
}

【讨论】:

  • 在我这样做之后,验证没有出现在表单中,即使缺少某些内容或格式错误我也可以提交,你知道为什么吗?
  • @Mariam 我建议您为此发布另一个问题。但我记得,有一个页面级方法称为this.Validate(),它会检查您的所有验证。然后检查属性this.IsValid仅在值为 true 时运行代码
  • @Mariam 您可以在这里查看示例:docs.microsoft.com/en-us/dotnet/api/…
猜你喜欢
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
相关资源
最近更新 更多