Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
Given Below My Problem Code for bind Grid. But Problem is Index was outside the bounds of the array.
C#
public void displayGridView()
{

string vori_80 = string.Empty;
while (sdr.Read())
{

string gf_80 = ConvertDBNull.To<string>(sdr["gift_80"], String.Empty);// Get string from read database for Split

string[] stringSeparators_80 = new string[] { "@@" };
var result_80_vori = gf_80.Split(stringSeparators_80, StringSplitOptions.None);// Split this string.
gift_80 = ConvertDBNull.To<string>(result_80_vori[0], String.Empty);// Here Problem Index was outside the bounds of the array
vori_80 = ConvertDBNull.To<string>(result_80_vori[1], string.Empty);
dt.Rows.Add(ops_id, comments, pname, cname, TARGET, SALES, type, thana, dist, division, Target_80, Total_AMT_80, gift_80, vori_80, Target_90, Total_AMT_90, Total_AMT_90, gift_90, vori_90, Target_100, Total_AMT_100,gift_100,vori_100, contact_person,cmobile,wifename,SP_NAME,ZI_NAME,RH_NAME,MG_NAME);

}
sdr.Close();
SqlCon.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}


N.B: Some Row add with DataTable But Full Row don't add With DataTable. Show Error is Index was outside the bounds of the array. This error for split string. But Can not Solved this Problem. How can Solve this problem.
Posted
Updated 17-Aug-14 7:30am
v4
Comments
Richard MacCutchan 17-Aug-14 13:10pm    
You could start by getting rid of all the unnecessary code, and showing us where the error occurs.
[no name] 17-Aug-14 13:34pm    
You are not doing any checking and just assuming that your string contains your delimiter
Member 9245259 17-Aug-14 13:35pm    
How can check give me Example code
[no name] 17-Aug-14 13:44pm    
"Give me"?!?! Really? Know what manners are? Did you bother looking in the documentation for the string class to see if maybe there was a method to find if one string contains another string?
Member 9245259 17-Aug-14 13:56pm    
I was checked but I can not solved this problem.

Hi Well i would say we have to take a step back from the problem You want to solve and look at another, your coding style is leading to your problems.

I would like to introduce 2 coding paradigms to you: First is called 'fail early' and entails that if you can predict at code time any reason why your code cannot continue, you should stop it there and fail the operation in a way that will make a programmer notice so the reason for that logical flaw to happen will be fixed.
The other is 'code defensively' and mean that if there is any chance of any variable in your code getting into a state that could cause an exception, check for that first and handle it intentionally, because othewise you'll throw it streight into the all encompassing error handler which has much less of a clue what would be the right thing to do.

The code doesn't have to be as draconical throwing exception if any data violates, but i suspect this is the problem (some row with invalid data) so well have a look, should pop the problem right out in view:
C#
public void DisplayGridView(SqlDataReader sdr)
        {

            string vori_80 = "";
            while (sdr.Read())
            {
                var gf_80 = sdr["gift_80"] as string;
                if(string.IsNullOrEmpty(gf_80)) throw new Exception("No data supplied");

                const string seperator = "@@";
                if(!gf_80.Contains(seperator)) throw new Exception("There must be more than one value in this field");

                var result_80_vori = gf_80.Split("@@".ToCharArray());
                if (result_80_vori.Length < 2) throw new Exception("Data sould contain at least two values seperated by @@");

                object value = result_80_vori[0];
                var gift_80 = value == null ? "" : value.ToString();

                value = result_80_vori[1];
                vori_80 = value == null ? "" : value.ToString();

                //dt.Rows.Add(ops_id, comments, pname, cname, TARGET, SALES, type, thana, dist, division, Target_80, Total_AMT_80, gift_80, vori_80, Target_90, Total_AMT_90, Total_AMT_90, gift_90, vori_90, Target_100, Total_AMT_100, gift_100, vori_100, contact_person, cmobile, wifename, SP_NAME, ZI_NAME, RH_NAME, MG_NAME);
            }
            sdr.Close();
            //SqlCon.Close();
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
        }


One last piece of advice, stop using global variables in your methods it is confusing and dangerous to have multiple code points touching the same data. The line where you add a row to dt you have a lot of values appearing out of nowhere, whould be much nicer if you were to pass in a struct with those as a parameter and same goes for the datareader which you may notice i moved to the method parameters list.
It seems you have a bit of way before you can fully 'K.I.S.S' your code http://en.wikipedia.org/wiki/KISS_principle[^]
 
Share this answer
 
C#
if (gf_80.Contains("@@"))
          {
             //Do your stuff here.
          }
 
Share this answer
 

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