specified cast is invalid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arial
    New Member
    • Sep 2007
    • 109

    specified cast is invalid

    Hi

    I am new to programming. learning c# , asp.net.

    write a simple program: to get the id from database and save it to arraylist to find the max(id).

    and getting exception: Specified cast is invalid.

    at line: int low = (int)arrm[i];

    Appreciate any help.

    Thanks
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by arial
    Hi

    I am new to programming. learning c# , asp.net.

    write a simple program: to get the id from database and save it to arraylist to find the max(id).

    and getting exception: Specified cast is invalid.

    at line: int low = (int)arrm[i];

    Appreciate any help.

    Thanks
    is arrm your table or the row?
    if it is the table then arrm[i] is a row,
    maybe what you want is
    Code:
    int i = (int)arrm[i][j];

    Comment

    • arial
      New Member
      • Sep 2007
      • 109

      #3
      arram is a object of my array list.

      Arraylist arrm = new Arraylist();

      and here is part of my script:

      cmd4.CommandTex t = "select id from dpt" ;
      cmd4.ExecuteNon Query();
      SqlDataReader reader = cmd4.ExecuteRea der();
      while(reader.Re ad())
      {
      values = new object[reader.FieldCou nt];
      reader.GetValue s(values);
      arrm.Add(values );
      }
      arrm.Sort();
      reader.Close();
      cmd4.Connection .Close();

      int max = 0;
      for (int i = 0; i < arrm.Count; i++)
      {
      int min = (int)arrm[i];
      if (max < min)
      max = min;
      }

      }

      Thanks,

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        You could use the .GetType().Name on the arrm[i] to see what type the value is actually being returned as.

        I would guess that if ID is defined as say "bigint" in the database, you would need to cast it to "Int64" and not just "int"

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by arial
          arram is a object of my array list.

          Arraylist arrm = new Arraylist();

          and here is part of my script:

          cmd4.CommandTex t = "select id from dpt" ;
          cmd4.ExecuteNon Query();
          SqlDataReader reader = cmd4.ExecuteRea der();
          while(reader.Re ad())
          {
          values = new object[reader.FieldCou nt];
          reader.GetValue s(values);
          arrm.Add(values );
          }
          arrm.Sort();
          reader.Close();
          cmd4.Connection .Close();

          int max = 0;
          for (int i = 0; i < arrm.Count; i++)
          {
          int min = (int)arrm[i];
          if (max < min)
          max = min;
          }

          }

          Thanks,
          1.) Please use code tags when posting code.
          2.) You have [CODE=css]arrm.Add(values );[/CODE] which adds an array of objects into an arraylist. Then you have
          [CODE=css] int min = (int)arrm[i];[/CODE] which won't work because you're now trying to cast an object array to an int.

          Comment

          • arial
            New Member
            • Sep 2007
            • 109

            #6
            ID is varchar field in sql database.

            and .Getvalue().Nam e returns arraylist.

            What I want to do is, find a max ID from table. I also tried Convert.ToInt32 (arrm[i]), but still no luck on getting what I want.

            Thanks,

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              There are SQL statements that will return to you the largest one. Google some SQL examples about it.

              What r0 said was right (I missed it, good catch on his part) you are trying to cast an array of objects to an intiger.

              And it's GetType().Name, which is available for everything and will identify what the object type is.
              You don't need it now that it's been identified as an object[].

              Comment

              • arial
                New Member
                • Sep 2007
                • 109

                #8
                Yes, I know sql statement will give me largest value but I need to use simple
                select id from dpt and store this query result into array and find a max because I am going to use this array to do some other stuff.

                If I can successfully get my first part right get max value and then it would be easy do the other parts.

                so, is there a way to convert object type to int?

                Thanks

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Since you are only returning a single value, using getValues() is a waste.

                  Code:
                  cmd4.CommandText = "select id from dpt" ; 
                  cmd4.ExecuteNonQuery();
                  SqlDataReader reader = cmd4.ExecuteReader();
                  while(reader.Read())
                  {
                  int temp=reader.GetInt32(1);
                  arrm.Add(temp);
                  }
                  arrm.Sort();
                  reader.Close();
                  cmd4.Connection.Close();
                  
                  int max = 0;
                  for (int i = 0; i < arrm.Count; i++)
                  {
                  int min = (int)arrm[i];
                  if (max < min)
                  max = min;
                  }
                  
                  }
                  This might not be exactly right as I can't currently check it in a project, but hopefully it will make more sense as now your (int)arrm[i] shouldn't throw an exception.

                  Comment

                  • arial
                    New Member
                    • Sep 2007
                    • 109

                    #10
                    Thanks Plater for all your help.

                    By after making a changes you suggested Now I am getting new error:

                    Index was outside the bounds of the array.

                    at line: int temp=reader.Get Int32(1);

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by arial
                      Thanks Plater for all your help.

                      By after making a changes you suggested Now I am getting new error:

                      Index was outside the bounds of the array.

                      at line: int temp=reader.Get Int32(1);
                      Can you post what you have now(using code=css tags ofcourse)?

                      Comment

                      • arial
                        New Member
                        • Sep 2007
                        • 109

                        #12
                        I am not sure how to do CSS. so, here is what I have.

                        Code:
                        cmd4.CommandText = "select id from dpt" ;
                        Code:
                        cmd4.ExecuteNonQuery();
                        Code:
                        SqlDataReader reader = cmd4.ExecuteReader();
                        Code:
                        while(reader.Read())
                        Code:
                        {
                        Code:
                        int temp=reader.GetInt32(1);
                        Code:
                        arrm.Add(temp);}
                        Code:
                        reader.Close();
                        Code:
                        cmcmd4.Connection.Close();
                        Code:
                        int max = 0;
                        Code:
                        for (int i = 0;i<arrm.Count;i++)
                        Code:
                        {
                        Code:
                        int min = (int) arram[i];
                        Code:
                        if (max < min)
                        Code:
                        max = min;
                        Code:
                        }

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by arial
                          I am not sure how to do CSS. so, here is what I have.

                          Code:
                          cmd4.CommandText = "select id from dpt" ;
                          Code:
                          cmd4.ExecuteNonQuery();
                          Code:
                          SqlDataReader reader = cmd4.ExecuteReader();
                          Code:
                          while(reader.Read())
                          Code:
                          {
                          Code:
                          int temp=reader.GetInt32(1);
                          Code:
                          arrm.Add(temp);}
                          Code:
                          reader.Close();
                          Code:
                          cmcmd4.Connection.Close();
                          Code:
                          int max = 0;
                          Code:
                          for (int i = 0;i<arrm.Count;i++)
                          Code:
                          {
                          Code:
                          int min = (int) arram[i];
                          Code:
                          if (max < min)
                          Code:
                          max = min;
                          Code:
                          }
                          No that's not what I meant. I meant that you wrap all the code in one set of code tags not broken up like that and then when you have all the code wrapped in one set of code tags, you add =css to the opening code tag so that your opening code tag looks like this
                          [CODE=css].

                          Comment

                          • arial
                            New Member
                            • Sep 2007
                            • 109

                            #14
                            Ok. I got it, here it is:

                            [CODE=css] cmd4.CommandTex t = "select id from dpt" ;
                            cmd4.ExecuteNon Query();
                            SqlDataReader reader = cmd4.ExecuteRea der();
                            while(reader.Re ad())
                            {
                            int temp=reader.Get Int32(1);
                            arrm.Add(temp);
                            }
                            arrm.Sort();
                            reader.Close();
                            cmd4.Connection .Close();

                            int max = 0;
                            for (int i = 0; i < arrm.Count; i++)
                            {
                            int min = (int)arrm[i];
                            if (max < min)
                            max = min;
                            }[/CODE]

                            Thanks,

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              Try making it:
                              int temp=reader.Get Int32(0);

                              I wasn't sure if it was zero one one based index.

                              Comment

                              Working...