整数变量正在获取“ad”的字符串值。沿着这条线的某个地方,有人能看到哪里吗?

发布于 2024-08-19 17:57:42 字数 192 浏览 2 评论 0原文

这是我的代码:

我应该以整数形式获取部门 ID (did) 的输出以及所需的模板文件名(结果)。

我得到的错误是:从字符串“ad”到类型“Integer”的转换无效。我对 ASP.NET 相当陌生,看不到 did 变量在哪里拾取“ad”字符串。

任何帮助将不胜感激。

谢谢

Here is my code:

I should get output of the department id (did) as an integer and the templatefilename (result) that is required.

The errors I get are: Conversion from string "ad" to type 'Integer' is not valid. I'm fairly new to asp.net and cannot see where the did variable picks up the "ad" string.

Any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-08-26 17:57:43

错误在于以下几行:

sql = ("select departmentsid from departmentsgroupings where groupingid =" & pageid & "")
did = (cmd.ExecuteScalar)     <---- Wrong command executed here.

您可能是想在 sql 中执行代码,而不是再次在 cmd 中执行。

The mistake is in these lines:

sql = ("select departmentsid from departmentsgroupings where groupingid =" & pageid & "")
did = (cmd.ExecuteScalar)     <---- Wrong command executed here.

You presumably meant to execute the code in sql, not cmd again.

御守 2024-08-26 17:57:42

当您构造对表 departmentsgroupings 的查询时,您将更改 sql 的值,但不会创建新的 SqlCommand。这意味着 cmd 仍然包含旧的 SQL 语句(对 Modules 表的查询),该语句在执行时返回 “ad”

要解决此问题,请按如下方式更改代码:

sql = ("select departmentsid from departmentsgroupings where groupingid =" & pageid & "")
Set cmd = New SqlCommand(sql, conn)    
did = (cmd.ExecuteScalar)

您可能期望对 sql 所做的更改会自动传递给 SqlCommand - 但它不起作用那样。

编辑:您编写的代码很容易受到 SQL 注入攻击。如果您不知道这些是什么,您需要阅读第一个答案:

“Bobby Tables”XKCD 漫画中的 SQL 注入是如何工作的?

要保护自己免受此类攻击,请使用参数化查询。

When you construct the query to the table departmentsgroupings, you're changing the value of sql, but you aren't creating a new SqlCommand. This means that cmd still contains the old SQL statement (the query to the Modules table) which, when executed, returns "ad".

To fix this, change your code as follows:

sql = ("select departmentsid from departmentsgroupings where groupingid =" & pageid & "")
Set cmd = New SqlCommand(sql, conn)    
did = (cmd.ExecuteScalar)

You may have expected the change you made to sql to get passed on automatically to the SqlCommand -- but it doesn't work that way.

Edit: Your code, as written, is vulnerable to SQL injection attacks. If you don't know what these are, you need to read the first answer to this:

How does the SQL injection from the "Bobby Tables" XKCD comic work?

To protect yourself against these kinds of attacks, use parameterized queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文