“/”应用程序中的服务器错误。不正确的语法)的附近

时间:2021-07-05 00:58:55

Im creating and INSERT form and i got this error, this is my code in codebehind:

我创建并插入表单,我得到了这个错误,这是我的代码在codebehind:

protected void btnSave_Click(object sender, EventArgs e)
{
    string Selected = category_ddl.SelectedValue;
    lb_date.Text = System.DateTime.Now.ToLongDateString();
    process();
}

private void process()
{
    string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename= |DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
    string sql = "INSERT INTO News (date_time,title,author,source,category,description,) VALUES (@date, @title, @author,@source,@Selected,@description)";
    SqlConnection conn = new SqlConnection(connectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.AddWithValue("@date", lb_date.Text.ToString());
    cmd.Parameters.AddWithValue("@title", title_tb.Text.ToString());
    cmd.Parameters.AddWithValue("@author", author_tb.Text.ToString());
    cmd.Parameters.AddWithValue("@source", source_tb.Text.ToString());
    cmd.Parameters.AddWithValue("@Selected", category_ddl.Text.ToString());
    cmd.Parameters.AddWithValue("@description", desc_tb.Text.ToString());
    cmd.ExecuteNonQuery();
    conn.Close();
}

and this is my error:

这是我的错误

Server Error in '/' Application.

Incorrect syntax near ')'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.

Source Error: 


Line 37:             cmd.Parameters.AddWithValue("@Selected", category_ddl.Text.ToString());
Line 38:             cmd.Parameters.AddWithValue("@description", desc_tb.Text.ToString());
Line 39:             cmd.ExecuteNonQuery();
Line 40:             conn.Close();
Line 41:         }

the Line 39: cmd.ExecuteNonQuery(); is highlighted in color red. what did I do something wrong?,i have this using:

的39行:cmd.ExecuteNonQuery();用红色突出显示。我做错什么了?我有使用:

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;
using System.Data;

1 个解决方案

#1


2  

You have an extra comma after description:

在描述之后你有一个额外的逗号:

string sql = "INSERT INTO News (date_time,title,author,source,category,description,)

string sql = "INSERT INTO News (date_time,title,author,source,category,description)

#1


2  

You have an extra comma after description:

在描述之后你有一个额外的逗号:

string sql = "INSERT INTO News (date_time,title,author,source,category,description,)

string sql = "INSERT INTO News (date_time,title,author,source,category,description)