“has”附近的语法不正确

rdrgkggo  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(479)

我正在尝试使用asp.net创建一个web窗体,但效果不好,我希望有人能帮助我。
如果用户在按钮处输入而不是在开始处输入,我如何才能显示验证?
我希望用户看到的评论,只有当一些东西是混乱或不正确的。
还有,我在点击按钮后发现了他的错误,你知道为什么吗?
异常详细信息:system.data.sqlclient.sqlexception:“has”附近的语法不正确。
下面是一些屏幕截图:validationexception详细信息:system.data.sqlclient.sqlexception:靠近“has”的语法不正确。单击按钮后显示消息

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>
nuypyhwy

nuypyhwy1#

这不是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);

命令中缺少“(”符号。
这是正确的 INSERT 在sql中。

INSERT INTO TABLE (COLUMN1, COLUMN2, COLUMN3,....) VALUES (VALUE1, VALUE2, VALUE3,....)

如果你有参数,你可以这样解析它们:

INSERT INTO TABLE (COLUMN1, COLUMN2, COLUMN3,....) VALUES (@parameter1, @parameter2, @parameter3,....)

相关问题