简体   繁体   English

','附近的语法不正确。 说明:执行期间发生未处理的异常

[英]Incorrect syntax near ','. Description: An unhandled exception occurred during the execution

I know this title seems to be repeated a lot but I tried to search and didn't find the answer. 我知道这个标题似乎重复了很多,但是我尝试搜索但没有找到答案。

Code: 码:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {}

    protected void gv_master_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Get the currently selected row using the SelectedRow property.
        GridViewRow row = gv_master.SelectedRow;

        // Display the first name from the selected row.
        // In this example, the third column (index 2) contains
        // the first name.
        lbl_reqNoV.Text = row.Cells[1].Text;
        lbl_reqNoV.Visible = true;
        lbl_reqNo.Visible = true;

        SqlConnection sqlConnection1 = new SqlConnection("Data Source=saitest01;Initial Catalog=SAI_website;Persist Security Info=True;User ID=sa;Password=sai@987");
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "Select * from purchase Where ReqNo = '" + lbl_reqNoV.Text + "', sqlConnection1";

        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();
        SqlDataReader DR1;
        DR1 = cmd.ExecuteReader();
        DR1.Read();

        // Data is accessible through the DataDR1 object here
        gv_full.DataSource = DR1;
        gv_full.DataBind();
    }
}

the problem is you where adding the name of Connection in the query text which is ofcource not recognized by sqlserver the correct format was 问题是你在哪里添加的名字Connection它ofcource没有被认可,在查询文本sqlserver的正确格式为

var cmd = new SqlCommand("Select * from purchase Where ReqNo = @reqno",sqlConnection1)

or you can do this 或者你可以这样做

cmd.CommandText = "Select * from purchase Where ReqNo = @reqno";

cmd.Parameters.AddWithValue("reqno",lbl_reqNoV.Text);
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

you should always use parameters in query to avoid Sql Injection 您应该始终在查询中使用参数以避免Sql Injection

just change following 只是改变以下

 cmd.CommandText = "Select * from purchase Where ReqNo = '" + lbl_reqNoV.Text + "', sqlConnection1";

with, 与,

 cmd.CommandText = "Select * from purchase Where ReqNo = '" + lbl_reqNoV.Text + "' ";

Above will make your code working. 以上将使您的代码正常工作。 But you should modify you code to handle SQL Injection. 但是您应该修改代码以处理SQL注入。 As answered by @Usman 如@Usman回答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行过程中发生未处理的异常 - An unhandled exception occurred during the execution SQL异常未处理'='附近的语法不正确? - Sql Exception Unhandled Incorrect syntax near '='? 当前Web请求“Ненайденуказанныймодуль”的执行期间发生未处理的异常 - An unhandled exception occurred during the execution of the current web request, “Не найден указанный модуль” 当前Web请求执行期间发生未处理的异常 - An unhandled exception occurred during the execution of the current web request 我有异常错误“在 System.Data.dll 中发生了类型为 'System.Data.SqlClient.SqlException' 的未处理异常'ID' 附近的语法不正确。” - i have exception error "An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Incorrect syntax near 'ID'." ',' 附近的用户代码错误语法未处理 SqlException - SqlException was unhandled by user code incorrect syntax near ',' System.Data.dll中的类型'System.Data.SqlClient.SqlException'发生异常'b'附近的语法不正确 - An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Incorrect syntax near 'b' 执行当前 Web 请求期间发生未处理的异常。 ASP.NET - An unhandled exception occurred during the execution of the current web request. ASP.NET 如何修复“在执行当前 Web 请求期间发生未处理的异常。” - How to fix "An unhandled exception occurred during the execution of the current web request. " 当前Web请求的执行期间发生未处理的异常。 型号兼容性无法检查 - An unhandled exception occurred during the execution of the current web request. Model compatibility cannot be checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM