错误''语法附近'位置错误'的问题。必须声明标量变量“@”。在C#中

时间:2017-12-13 18:14:36

标签: c# asp.net .net sql-server

当我尝试向我的Web应用程序提交数据时,出现以下错误:

  

System.Data.SqlClient.SqlException:''bit'附近的语法不正确。必须声明标量变量“@”

我检查过,代码中的任何地方都没有“位”。我写的四个列现在是位列。这是基于我问的问题“I'm trying write a Checkbox List to a Database, what am I missing?”。

我实施了所讨论的更改,现在我收到了一个错误,我不确定是否需要提出一个新问题,它会显示新代码,或者添加到该问题中。

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Revocations.Default_2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Styles/StyleSheet1.css" rel="stylesheet" />
</head>
<body>
    <form id="form2" runat="server">
        <div class="t_row_header">
            <asp:Image ID="Image1" runat="server" CssClass="logo" ImageUrl="~/Images/logo.png" />
        </div>
        <h1>Transfer Revocations</h1>
        <div class="form-fields">

            <div class="form-group-left">
                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSchoolYear" CssClass="ErrorMessage" ErrorMessage="Please enter  a    school   year."   SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    School Year:
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtSchoolYear" CssClass="ErrorMessage" ErrorMessage="Please enter a school  year     &gt;=    2017." Operator="GreaterThanEqual" SetFocusOnError="True" ValueToCompare="2017">*</asp:CompareValidator>
                </label>
                &nbsp;<asp:TextBox ID="TextBox1" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtStudentID" CssClass="ErrorMessage" ErrorMessage="Please enter a     student ID."     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Student ID:</label>&nbsp;<asp:TextBox ID="TextBox2" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtFirstName" CssClass="ErrorMessage" ErrorMessage="Please enter a     first name."     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    First Name:</label>&nbsp;<asp:TextBox ID="TextBox3" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtLastName" CssClass="ErrorMessage" ErrorMessage="Please enter a     last name"     SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Last Name:</label>&nbsp;<asp:TextBox ID="TextBox4" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtRevokedSchool" CssClass="ErrorMessage" ErrorMessage="Please   enter   the     revoking school" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Revoked School:</label>&nbsp;<asp:TextBox ID="TextBox5" runat="server" CssClass="txtBox"></asp:TextBox>

                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtRevocationDate" CssClass="ErrorMessage" ErrorMessage="Please    enter  a     revocation date" SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Revocation Date:</label>&nbsp;<asp:TextBox ID="TextBox6" runat="server" TextMode="Date" CssClass="txtBox"></asp:TextBox>

            </div>
            <!-- form-group-left -->

            <div class="form-group-right">
                <label>
                    Reason:<asp:CheckBoxList ID="CheckBoxList1" runat="server">
                        <asp:ListItem text="Academics"  Value="1"></asp:ListItem>
                        <asp:ListItem text="Attendance" Value="2"></asp:ListItem>
                        <asp:ListItem text="Behavior"  Value ="3"></asp:ListItem>
                        <asp:ListItem text="Cooperative Relationship" Value="4"></asp:ListItem>
                    </asp:CheckBoxList>
                    <br />
                </label>
                <label>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txtNotes" CssClass="ErrorMessage" ErrorMessage="Please enter a    note."      SetFocusOnError="True">*</asp:RequiredFieldValidator>
                    Notes:</label>&nbsp;&nbsp;<asp:TextBox ID="TextBox7" runat="server" height="218px" TextMode="MultiLine" CssClass="InsideShadow"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="SUBMIT" CssClass="btn" OnClick="btnSubmit_Click" />
            </div>
        </div>
        <asp:ValidationSummary ID="ValidationSummary2" runat="server" CssClass="ErrorMessage" />

        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PPSS_RevocationConnection %>" DeleteCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"
            SelectCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"
            UpdateCommand="SELECT 
       [SchoolYear]
      ,[Student_ID]
      ,[Last_Name]
      ,[First_Name]
      ,[Revoked_School]
      ,CAST([Revocation_Date] AS DATE) AS [Revocation_Date]
      ,[Notes]
      ,[Academics]
      ,[Attendance]
      ,[Behavior]
      ,[Cooperative Relationship]

  FROM [PPSS_Work].[Transfers].[Revocations]"></asp:SqlDataSource>
    </form>
</body>
</html>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace Revocations
{
    public partial class Default_2 : System.Web.UI.Page
    {
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Establish SQL Connection
            SqlConnection sqlCon = new SqlConnection("Data Source = acctsql; Initial Catalog = PPSS_Work; Integrated Security = True") ;
            {
                // Write INSERT SQL statement
                SqlCommand sqlCom = new SqlCommand("INSERT INTO [PPSS_Work].[Transfers].[t_Revocations](SchoolYear, Student_ID, Last_Name, First_Name,    Revoked_School,Revocation_Date,  Academics, Attendance, Behavior, [Cooperative Relationship], Notes) VALUES(@SchoolYear, @Student_ID, @First_Name, @Last_Name,    @Revoked_School,  @Revocation_Date,@Academics, @Attendance, @Behavior, @[Cooperative Relationship], @Notes)", sqlCon);

                sqlCom.Parameters.AddWithValue("@SchoolYear", txtSchoolYear.Text);
                sqlCom.Parameters.AddWithValue("@Student_ID", txtStudentID.Text);
                sqlCom.Parameters.AddWithValue("@First_Name", txtFirstName.Text);
                sqlCom.Parameters.AddWithValue("@Last_Name", txtLastName.Text);
                sqlCom.Parameters.AddWithValue("@Revoked_School", txtRevokedSchool.Text);
                sqlCom.Parameters.AddWithValue("@Revocation_Date", txtRevocationDate.Text);
                sqlCom.Parameters.AddWithValue("@Notes", txtNotes.Text);

                foreach (ListItem item in chkbxlst1.Items)
                {
                    if (item.Selected)
                        sqlCom.Parameters.AddWithValue("@" + item.Text, true);
                }

                sqlCon.Open();
                sqlCom.ExecuteNonQuery();
                sqlCon.Close();

                if (IsPostBack)
                {
                    txtSchoolYear.Text = "";
                    txtStudentID.Text = "";
                    txtFirstName.Text = "";
                    txtLastName.Text = "";
                    txtRevokedSchool.Text = "";
                    txtRevocationDate.Text = "";
                    chkbxlst1.Text = "";
                    txtNotes.Text = "";
                }
            }
        }
    }
}

造成这些错误的原因是什么?

2 个答案:

答案 0 :(得分:1)

由于参数名称@[Cooperative Relationship]中的空格,您遇到了问题,因此要避免此问题,请首先在列表项Text的{​​{1}}属性中使用下划线替换空格如下所示:

Cooperative Relationship

然后将<label> Reason:<asp:CheckBoxList ID="CheckBoxList1" runat="server"> <asp:ListItem text="Academics" Value="1"></asp:ListItem> <asp:ListItem text="Attendance" Value="2"></asp:ListItem> <asp:ListItem text="Behavior" Value ="3"></asp:ListItem> <asp:ListItem text="Cooperative_Relationship" Value="4"></asp:ListItem> </asp:CheckBoxList> <br /> </label> 中的变量名称@[Cooperative Relationship]修改为@Cooperative_Relationship,如下所示:

sqlCom

不要在现有代码中进行任何其他更改,然后运行程序,我希望它能解决您的问题。

答案 1 :(得分:1)

检查chkbxlst1.Items集合中实际拥有的值,如果你有一个item.Text为空,则会产生以下错误:

foreach (ListItem item in chkbxlst1.Items)
{
if (item.Selected)
    sqlCom.Parameters.AddWithValue("@" + item.Text, true);
}