【问题标题】:Incorrect syntax near 'has'“有”附近的语法不正确
【发布时间】:2020-10-19 11:40:15
【问题描述】:

我正在尝试使用 asp.net 创建一个 Web 表单,但效果不佳,希望有人能帮助我。
如果用户不是在开始时输入按钮,我如何才能显示验证?
我希望用户只有在出现问题或不正确时才能看到评论。
还有,我点击按钮后出现了他的错误,你知道为什么吗?

异常详细信息:System.Data.SqlClient.SqlException:'has' 附近的语法不正确。

以下是一些截图: The validation Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'has'. Message after click at the button

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 RegistrationForm1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-20J5J59; Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.Parameters.AddWithValue("@TTitle", TxtTitle.Text);
            cmd.Parameters.AddWithValue("@TName", TxtName.Text);
            cmd.Parameters.AddWithValue("@TID", TxtID.Text);
            cmd.Parameters.AddWithValue("@TEmail", TxtEmail.Text);
            cmd.Parameters.AddWithValue("@TMobile", TxtMobile.Text);
            cmd.Parameters.AddWithValue("@LMajor", LstMajor.SelectedItem.ToString()););
            cmd.Parameters.AddWithValue("@TNote", TxtNote.Text);
            cmd.CommandText = "Your request has been submitted successfully";
            cmd.ExecuteNonQuery();
            con.Close();
           
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="RegistrationForm1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 247px;
            width: 628px;
        }
        .auto-style3 {
            height: 24px;
        }
        .auto-style4 {
            height: 24px;
            width: 86px;
        }
        .auto-style5 {
            width: 86px;
        }
        .auto-style6 {
            width: 86px;
            height: 30px;
        }
        .auto-style7 {
            height: 30px;
        }
        .auto-style8 {
            color: #FF0000;
        }
        .auto-style9 {
            width: 86px;
            height: 28px;
        }
        .auto-style10 {
            height: 28px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table align="center" class="auto-style1">
                <caption>Registration Form</caption>
                <tr>
                    <td class="auto-style4">Title: </td>
                    <td class="auto-style3">
                        <asp:TextBox ID="TxtTitle" runat="server" placeholder="Title" Width="210px"></asp:TextBox><span class="auto-style8">Enter a title.</span></td>
                </tr>

                <tr>
                    <td class="auto-style6">Name:</td>
                    <td class="auto-style7">
                        <asp:TextBox ID="TxtName" runat="server" placeholder="Full name" Width="210px"></asp:TextBox><span class="auto-style8">Enter full name.</span></td>
                </tr>

                <tr>
                    <td class="auto-style9">ID:</td>
                    <td class="auto-style10">
                        <asp:TextBox ID="TxtID" runat="server" placeholder="201600000" Width="208px" TextMode="Number"></asp:TextBox><span class="auto-style8">Enter ID. </span>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="RegularExpressionValidator" ControlToValidate="TxtID" ForeColor="Red" ValidationExpression="\d(9)">Invalid ID</asp:RegularExpressionValidator>
                    </td>
                </tr>

                <tr>
                    <td class="auto-style5">E-mail:</td>
                    <td>
                        <asp:TextBox ID="TxtEmail" runat="server" placeholder="name@example.com" Width="209px" TextMode="Email"></asp:TextBox><span class="auto-style8">Enter E-mail. </span>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TxtEmail" ErrorMessage="RegularExpressionValidator" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">Invalid E-mail</asp:RegularExpressionValidator>
                    </td>
                </tr>

                <tr>
                    <td class="auto-style5">Mobile No:</td>
                    <td>
                        <asp:TextBox ID="TxtMobile" runat="server" placeholder="966500000000" Width="210px" TextMode="Number"></asp:TextBox><span class="auto-style8">Enter mobile No. </span>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TxtMobile" ErrorMessage="RegularExpressionValidator" ForeColor="Red" ValidationExpression="\d(10)">Invalid Mobile Number</asp:RegularExpressionValidator>
                    </td>
                </tr>

                <tr>
                    <td class="auto-style5">Major:</td>
                    <td>
                        <asp:DropDownList ID="LstMajor" runat="server" Height="17px" Width="218px">
                            <asp:ListItem>Select Option</asp:ListItem>
                            <asp:ListItem>Accounting</asp:ListItem>
                            <asp:ListItem>Architecture</asp:ListItem>
                            <asp:ListItem>Business Administration</asp:ListItem>
                            <asp:ListItem>Computer Science</asp:ListItem>
                            <asp:ListItem>Computer Engineering</asp:ListItem>
                            <asp:ListItem>Civil Engineering</asp:ListItem>
                            <asp:ListItem>Electrical Engineering</asp:ListItem>
                            <asp:ListItem>Finance</asp:ListItem>
                            <asp:ListItem>Graphic Design</asp:ListItem>
                            <asp:ListItem>Human Resource Management</asp:ListItem>
                            <asp:ListItem>Information Technology</asp:ListItem>
                            <asp:ListItem>Interior Design</asp:ListItem>
                            <asp:ListItem>Law</asp:ListItem>
                            <asp:ListItem>Mchanical Engineering</asp:ListItem>
                            <asp:ListItem>Managment Information System</asp:ListItem>
                            <asp:ListItem>Software Engineering</asp:ListItem>
                            <asp:ListItem>Other</asp:ListItem>
                        </asp:DropDownList><span class="auto-style8">Choose major.</span></td>
                </tr>

                <tr>
                    <td class="auto-style5">Note:</td>
                    <td>
                        <asp:TextBox ID="TxtNote" placeholder="Add More Information..." runat="server" Width="210px" Height="76px" TextMode="MultiLine"></asp:TextBox></td>
                </tr>

                <tr>
                    <td align="center" colspan="2">
                        <br />
                        <asp:Button ID="Button1" runat="server" Text="Register" Font-Size="Medium" Height="36px" Width="110px" OnClientClick=" return confirm(Your request has been submitted successfully" OnClick="Button1_Click" /></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

【问题讨论】:

    标签: javascript html sql asp.net webforms


    【解决方案1】:

    这不是 SQL 命令,这就是您出现此错误的原因。

    cmd.CommandText = "Your request has been submitted successfully";,只是一句话。

    cmd.CommandText 中,您必须使用 SQL 语言解析有效的 SQL 命令。

    除了你的命令,还必须包含你已经在.Parameters中添加的参数,并且它们必须具有完全相同的名称。


    在您的评论中回答:

    这不是一个有效的代码:

    ("insert into reg" + "TTitle, TName, TID, TEmail, TMobile, LMajor, TNote)values(@TxtTitle, @TxtName, @TxtID, @TxtEmail, @TxtMobile, @LstMajor, @TxtNote)",con);
    

    这是正确的:

    ("insert into reg" + "(TTitle, TName, TID, TEmail, TMobile, LMajor, TNote)values(@TxtTitle, @TxtName, @TxtID, @TxtEmail, @TxtMobile, @LstMajor, @TxtNote)",con);
    

    您错过了命令中的“(”符号。

    • 这是 SQL 中 INSERT 的正确格式。
    INSERT INTO TABLE (COLUMN1, COLUMN2, COLUMN3,....) VALUES (VALUE1, VALUE2, VALUE3,....)
    
    • 如果你有参数,你可以像这样解析它们:
    INSERT INTO TABLE (COLUMN1, COLUMN2, COLUMN3,....) VALUES (@parameter1, @parameter2, @parameter3,....)
    

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 2013-12-16
      • 1970-01-01
      • 2018-08-14
      • 2011-03-11
      • 2014-02-16
      • 2017-01-11
      • 2020-02-06
      相关资源
      最近更新 更多