Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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")
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 
Posted
Updated 21-May-18 1:25am
Comments
F-ES Sitecore 21-May-18 5:14am    
Use the debugger to look at what is in "Txtbx_InvNo.Text"

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

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.
-----
C#
strSQL = "select * from VIEW_AXERP_SALES_DETAIL_IOSS where invoiceid = '" + Txtbx_InvNo.Text + "' order by tline";

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[^]
 
Share this answer
 
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:
C#
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);
But these will throw an exception:
C#
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);
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.
 
Share this answer
 
v2
Comments
Member 13318869 21-May-18 22:46pm    
thanks guys for the idea, but i just change the substring to startwith and it works!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900