priyank gandhi

priyank gandhi

  • NA
  • 1
  • 2.1k

use array value in datareader object

Jan 21 2012 2:53 AM
SqlConnection MyConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\apptitude\projects\database\App_Data\Database.mdf;Integrated Security=True;User Instance=True");//this conn is use for update statement
        MyConn.Open();
        SqlConnection MyConn1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\apptitude\projects\database\App_Data\Database.mdf;Integrated Security=True;User Instance=True");//this conn is use for select statement
        MyConn1.Open();
        int i,n=5;
        String[] month=new String[12]{"January","February","March","April","May","June","July","August","September","Octomber","November","December"};
        int day = DateTime.Now.Day;
        int mon= DateTime.Now.Month;
        mon = mon - 1;      //because month array is with 0
        Label1.Text = day.ToString();
        if (day==21)
        {
            SqlCommand cmd1 = new SqlCommand();
            cmd1.Connection = MyConn1;
            cmd1.CommandText = "select No_of_times,Dustbin_no from mounthly_data";
            SqlDataReader MyReader = cmd1.ExecuteReader();
            while (MyReader.Read())
            {
                String a = MyReader["No_of_times"].ToString();
                String b = MyReader["Dustbin_no"].ToString();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = MyConn;
                cmd.CommandText = "select * from Yearly_data";
                SqlDataReader MyReader1 = cmd.ExecuteReader();
                while (MyReader1.Read())
                {
                    String tt="";
                    for(int j=0;j<12;j++)   //12 use for mounth
                    {
                        if(j!=(mon-1))
                        {
                            String mont = MyReader1["('"+month[j]+"')"].ToString();
                            tt += mont;
                        }
                    }
                    Label1.Text = tt.ToString() ;
                }


got error on bold line

i got error like

'January'

System.IndexOutOfRangeException: 'January'

Answers (1)