Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Can you please tell me whats the problem here in code:
at this line
"sb.Append(((int)rdr[mod_int]));"

error is "index was outside the bound of an array"
public static void dispaly_functions_module_role(string proc_fun, int mod_int, int rol_int, CheckedListBox c1)
        {
            try
            {
                int count = 0;
                int count1 = 0;
                string sql_string = ("Data Source=sdggdfgdfgdfg;Initial Catalog=GCERP_SEC;User Id=sdfgdsfg;Password=fgsdfg");
                SqlConnection conn = new SqlConnection(sql_string);
                SqlCommand command = new SqlCommand(proc_fun, conn);
                conn.Open();
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@mid", mod_int);
                command.Parameters.AddWithValue("@rid", rol_int);
                SqlDataReader rdr = command.ExecuteReader();

                if (rdr.HasRows)
                {
                    StringBuilder sb = new StringBuilder();
                    string a;
                    while (rdr.Read())
                    {
                        sb.Append(((int)rdr[mod_int]));
                        sb.Append(((int)rdr[rol_int]));
                        sb.Append("\n");
                        a = sb.ToString().Trim();
                        c1.Items.Add(a);
                       // Items.Insert(count1, a);
                        sb.Length = 0;
                        count1++;
                    }

                }
                else
                {
                    if (count1 != 0)
                        MessageBox.Show(" your data is saved");
                    else
                        MessageBox.Show("not found");
                }

                rdr.Close();
                command.ExecuteNonQuery();
                count++;
                conn.Close();
                conn.Dispose();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Error in database connection");
            }
        }


Please guide me.

Thanks in Advance.
Posted
Updated 20-Jul-11 1:16am
v3
Comments
Toniyo Jackson 20-Jul-11 6:11am    
What is mod_int? What is the value?

what is the value of mod_int and how many columns does the reader have?

If mod_int = 10 and the reader has 3 columns then this error occurs.
 
Share this answer
 
Comments
kami124 20-Jul-11 6:13am    
reader has to read from two column
and output is one column
Herman<T>.Instance 20-Jul-11 6:14am    
is mod_int in column 1 or 2?

or you should use the column name rdr["mod_int"] or use use the columnnumber (0 or 1). You now use the value of mod_int which is not the value of the columnnumber
kami124 20-Jul-11 6:17am    
mod_int is in column 2
and rol_int in column 3
but i am not using column 1 (becouse i have to get value from these two columns)
and the output value is from column 4
Herman<T>.Instance 20-Jul-11 6:20am    
so sb.Append(((int)rdr[mod_int]));
sb.Append(((int)rdr[rol_int]));
sb.Append("\n");
will be
sb.Append(((int)rdr[2]));
sb.Append(((int)rdr[3]));
sb.Append("\n");
kami124 20-Jul-11 6:22am    
the same error is again coming
Check the number of rows and columns and ensure you're not trying to access the array outside those values.

Step through your code and see how each value increases or set a conditional break point to fire just before the limit of the array is reached.

Also, can I ask why you have :
a = sb.ToString().Trim();
inside your loop?

This defeats the purpose of using your StringBuilder surely. Couldn't you call StringBuilder.Clear() at the bottom of each loop and set the value of the StringBuilder to be the CheckBox text just before clearing? Just a thought.
 
Share this answer
 
I think you should remove one thing rdr.HasRows or if rdr.Read()
 
Share this answer
 
v2
Comments
Herman<T>.Instance 20-Jul-11 7:25am    
with rdr.HasRows he knows if there is data. with rdr.Read() he can use the data

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