索引和长度必须指向字符串中的位置。参数名称:长度 [英] Index and length must refer to a location within the string. Parameter name: length

查看:177
本文介绍了索引和长度必须指向字符串中的位置。参数名称:长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataSet CreateDataSet1()
    {
        System.Data.SqlClient.SqlConnection objConn = new System.Data.SqlClient.SqlConnection();
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand();
        System.Data.SqlClient.SqlDataAdapter dtAdapter = new System.Data.SqlClient.SqlDataAdapter();
        DataSet ds = new DataSet();
        string strConnString;
        string strSQL;
        strConnString = (ConfigurationSettings.AppSettings["ConnectionStringAX"]);

        if (Txtbx_InvNo.Text.Substring(0, 2) == "LV" || Txtbx_InvNo.Text.Substring(0, 2) == "EV")
        {
            strSQL = "select * from VIEW_AXERP_SALES_DETAIL_IOSS where invoiceid = '" + Txtbx_InvNo.Text + "' order by tline";
        }
        else if (Txtbx_InvNo.Text.Substring(0,2) == "CI")
        {
            strSQL = "select * from VIEW_AXERP_SALES_DETAIL_IOSS where dot_CINUMBER = '" + Txtbx_InvNo.Text + "' order by tline";
        }
        else if (Txtbx_InvNo.Text.Substring(0,4) == "SWIV")
        {
            strSQL = "select * from AR_TRX_DETAIL where tno = '" + Txtbx_InvNo.Text + "' and tmc = 'SWIT' order by tline";
            strConnString = (ConfigurationSettings.AppSettings["DSN=eb9gf;Port=2138;Uid=dba;Pwd=sql"]);
        }
        else
        {
            strSQL = "select * from TBL_AR_TRX_DETAIL where tno = '" + Txtbx_InvNo.Text + "' order by tline";
        }
        objConn.ConnectionString = strConnString;
        objCmd.Connection = objConn;
        objCmd.CommandText = strSQL;
        objCmd.CommandType = CommandType.Text;
        dtAdapter.SelectCommand = objCmd;
        dtAdapter.Fill(ds);
        dtAdapter = null;
        objConn.Close();
        objConn = null;
        return ds;
    }       





我的尝试:



我收到此错误



What I have tried:

I got this error

Index and length must refer to a location within the string.
Parameter name: length



此行


at this line

if (Txtbx_InvNo.Text.Substring(0, 2) == "LV" || Txtbx_InvNo.Text.Substring(0, 2) == "EV")

这是我的tbl中的数据示例。为什么我收到这个错误?是不是像我一样使用子串?

Here example of data in my tbl. why i got this error? is it wrong use substring like i did??

EV0000019 , EV0000020 , EV0000021 , LV0001532, LV0001533, LV0001534 

推荐答案

Quote:

索引和长度必须引用字符串中的位置。参数名称:length

Index and length must refer to a location within the string. Parameter name: length



查看错误的唯一方法是在错误点使用调试器并检查变量以查看字符串是什么。让我猜猜它不是你所期望的。



你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码在做什么,你的任务是与它应该做的比较。

-----


The only way to see what is wrong is by using the debugger at point of error and inspect the variables to see what is the string exactly. Lets guess it is not what you expect.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----

strSQL = "select * from VIEW_AXERP_SALES_DETAIL_IOSS where invoiceid = '" + Txtbx_InvNo.Text + "' order by tline";



不是你的解决方案问题,但你有另一个问题。

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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


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[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


你正在使用的子串的重载有两个参数:起始索引(必须在字符串中),以及要提取的数据的长度 - 和...的索引要提取的字符串中的最后一个字符也必须在原始字符串中。

因此,如果您的输入字符串是ABCD,那么这些将起作用:

The overload of Substring you are using takes two parameters: the start index (which must be within the string), and the length of the data to extract - the and the index of the last character in the string to be extracted must also be within the original string.
So if your input string is "ABCD", then these will work:
string s = "ABCD".Substring(0, 1);
string s = "ABCD".Substring(0, 2);
string s = "ABCD".Substring(0, 3);
string s = "ABCD".Substring(0, 4);
string s = "ABCD".Substring(1, 1);
string s = "ABCD".Substring(1, 2);
string s = "ABCD".Substring(1, 3);
string s = "ABCD".Substring(2, 1);
string s = "ABCD".Substring(2, 2);
string s = "ABCD".Substring(3, 1);

但这些将抛出例外:

string s = "ABCD".Substring(0, 5);
string s = "ABCD".Substring(1, 4);
string s = "ABCD".Substring(2, 3);
string s = "ABCD".Substring(2, 4);
string s = "ABCD".Substring(3, 2);
string s = "ABCD".Substring(3, 3);
string s = "ABCD".Substring(3, 4);

所以使用调试器,并查看Txtbx_InvNo.Text中的确切内容,并确保它包含至少两个(或四个)字符。

So use the debugger, and look at exactly what is in Txtbx_InvNo.Text and make sure it contains at least two (or four) characters.


这篇关于索引和长度必须指向字符串中的位置。参数名称:长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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