我该如何摆脱这个错误 [英] How do I get out of this error

查看:68
本文介绍了我该如何摆脱这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。 
该声明已被终止。





在开放调查项目中任何问题的一个选项留空时会出现此错误我正在使用这种代码的和平。每当我点击提交按钮时,它都显示错误。我想如果有人留下任何选项空白它会被保存到数据库中0





 protected void BtnSubmit_Click( object sender,EventArgs e)
{
var collection = Repeater0.Items;

foreach(集合中的RepeaterItem项)
{
HiddenField QID =(HiddenField)item.FindControl(QID);

int q = int.Parse(QID.Value);

string answer = Request.Form [q.ToString()];
string id = Convert.ToString(Session [UserID]);
string id2 = Convert.ToString(Session [ID]);

string connection = ConfigurationManager.AppSettings [connection]。ToString();
SqlConnection connec = new SqlConnection(connection);
connec.Open();
SqlCommand cmd = new SqlCommand(插入t_AnswerSheet(UserId,QId,AnswerId,AnswerResult)值('+ id +','+ q +',(从t_Answers中选择AID,其中QId =' + q +'和AOptions如'%'+ answer +%'),(从t_Answers中选择AnswerResult,其中QId ='+ q +'和AOptions如'%'+ answer +%')) ,connec);
cmd.ExecuteNonQuery();
connec.Close();

}

Response.Redirect(LoginPage.aspx);

}





我的尝试:

$ / $

 foreach(集合中的RepeaterItem项)
{
HiddenField QID =(HiddenField)item.FindControl(QID);

int q = int.Parse(QID.Value);

string answer = Request.Form [q.ToString()];
string id = Convert.ToString(Session [UserID]);
string id2 = Convert.ToString(Session [ID]);

string connection = ConfigurationManager.AppSettings [connection]。ToString();
SqlConnection connec = new SqlConnection(connection);
connec.Open();
SqlCommand cmd = new SqlCommand(插入t_AnswerSheet(UserId,QId,AnswerId,AnswerResult)值('+ id +','+ q +',(从t_Answers中选择AID,其中QId =' + q +'和AOptions如'%'+ answer +%'),(从t_Answers中选择AnswerResult,其中QId ='+ q +'和AOptions如'%'+ answer +%')) ,connec);
cmd.ExecuteNonQuery();
connec.Close();

}

解决方案

如果你谷歌错误信息你会发现很多解释这个问题。



要么这个



(从t_Answers中选择AID) QId ='+ q +'和AOptions如'%'+回答+%')





或者这个



(从t_Answers中选择AnswerResult,其中QId ='+ q +'和AOptions如'%'+ answer +%')





返回多个结果,但是你使用它们的方式需要它们只返回一个结果。我们无法告诉您为什么我们没有您的数据而且我们不知道您的输入。修复查询,使它们只返回一个结果,或者找到更好的方法来发现相应的答案ID等。


不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.



Getting this error whenever one option of any question left blank in open survey project as i am using this peace of code. whenever i click on submit button it show error. And I want if person left any option blank it get saved into database as 0


protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        var collection = Repeater0.Items;

        foreach (RepeaterItem item in collection)
        {
            HiddenField QID = (HiddenField)item.FindControl("QID");

            int q = int.Parse(QID.Value);

            string answer = Request.Form[q.ToString()];
            string id = Convert.ToString(Session["UserID"]);
            string id2 = Convert.ToString(Session["ID"]);
          
        string connection = ConfigurationManager.AppSettings["connection"].ToString();
        SqlConnection connec = new SqlConnection(connection);
        connec.Open();
        SqlCommand cmd = new SqlCommand("Insert into t_AnswerSheet(UserId,QId, AnswerId,AnswerResult) values('" + id + "','" + q + "',(select AID from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%'),(select AnswerResult from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%'))", connec);
        cmd.ExecuteNonQuery();
        connec.Close();
       
        }

        Response.Redirect("LoginPage.aspx");

    }



What I have tried:

foreach (RepeaterItem item in collection)
      {
          HiddenField QID = (HiddenField)item.FindControl("QID");

          int q = int.Parse(QID.Value);

          string answer = Request.Form[q.ToString()];
          string id = Convert.ToString(Session["UserID"]);
          string id2 = Convert.ToString(Session["ID"]);

      string connection = ConfigurationManager.AppSettings["connection"].ToString();
      SqlConnection connec = new SqlConnection(connection);
      connec.Open();
      SqlCommand cmd = new SqlCommand("Insert into t_AnswerSheet(UserId,QId, AnswerId,AnswerResult) values('" + id + "','" + q + "',(select AID from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%'),(select AnswerResult from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%'))", connec);
      cmd.ExecuteNonQuery();
      connec.Close();

      }

解决方案

If you google the error message you'll find lots of explanations to this issue.

Either this

(select AID from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%')



or this

(select AnswerResult from t_Answers where QId='" + q + "' and AOptions like'%" + answer + "%')



are returning more than one result however the way you are using them needs them to return only one result. We can't tell you why as we don't have your data and we don't know your inputs. Fix the queries so they only return one result, or find a better way to discover the appropriate answer id etc.


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于我该如何摆脱这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆