Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Specified cast is not valid. i can't understand error

Specified cast is not valid. i can't understand error

Scheduled Pinned Locked Moved C#
help
8 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 12899746
    wrote on last edited by
    #1

    public void FillCompVouType()
    {
    try
    {
    this.Cursor = Cursors.WaitCursor;
    con = new SqlConnection(cs.sourceConn1);
    con.Open();
    scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
    SqlDataReader DR = default(SqlDataReader);

                vochcombox.Items.Clear();
                DR = scmd.ExecuteReader();
                foreach (int VouType in DR)
                {
                    vochcombox.Items.Add(VouType);
                }
                while (DR.Read())
                {
                    this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
                }
                DR.Close();
                con.Close();
                this.Cursor = Cursors.Default;
    
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }
    
    OriginalGriffO R Richard DeemingR N 4 Replies Last reply
    0
    • M Member 12899746

      public void FillCompVouType()
      {
      try
      {
      this.Cursor = Cursors.WaitCursor;
      con = new SqlConnection(cs.sourceConn1);
      con.Open();
      scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
      SqlDataReader DR = default(SqlDataReader);

                  vochcombox.Items.Clear();
                  DR = scmd.ExecuteReader();
                  foreach (int VouType in DR)
                  {
                      vochcombox.Items.Add(VouType);
                  }
                  while (DR.Read())
                  {
                      this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
                  }
                  DR.Close();
                  con.Close();
                  this.Cursor = Cursors.Default;
      
              }
              catch (Exception ex)
              {
                  this.Cursor = Cursors.Default;
                  MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  if (con.State == ConnectionState.Open)
                  {
                      con.Close();
                  }
              }
          }
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Which line is it complaining about? I see no explicit casts in there.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Which line is it complaining about? I see no explicit casts in there.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        M Offline
        M Offline
        Member 12899746
        wrote on last edited by
        #3

        foreach (int VouType in DR)

        R 1 Reply Last reply
        0
        • M Member 12899746

          public void FillCompVouType()
          {
          try
          {
          this.Cursor = Cursors.WaitCursor;
          con = new SqlConnection(cs.sourceConn1);
          con.Open();
          scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
          SqlDataReader DR = default(SqlDataReader);

                      vochcombox.Items.Clear();
                      DR = scmd.ExecuteReader();
                      foreach (int VouType in DR)
                      {
                          vochcombox.Items.Add(VouType);
                      }
                      while (DR.Read())
                      {
                          this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
                      }
                      DR.Close();
                      con.Close();
                      this.Cursor = Cursors.Default;
          
                  }
                  catch (Exception ex)
                  {
                      this.Cursor = Cursors.Default;
                      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      if (con.State == ConnectionState.Open)
                      {
                          con.Close();
                      }
                  }
              }
          
          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          DR = scmd.ExecuteReader();
          foreach (int VouType in DR)

          Are you sure that VouType is an Integer ?

          M 1 Reply Last reply
          0
          • M Member 12899746

            foreach (int VouType in DR)

            R Offline
            R Offline
            Ralf Meier
            wrote on last edited by
            #5

            see my answer (under this) with it's question ...

            1 Reply Last reply
            0
            • R Ralf Meier

              DR = scmd.ExecuteReader();
              foreach (int VouType in DR)

              Are you sure that VouType is an Integer ?

              M Offline
              M Offline
              Member 12899746
              wrote on last edited by
              #6

              yes i'm its int

              1 Reply Last reply
              0
              • M Member 12899746

                public void FillCompVouType()
                {
                try
                {
                this.Cursor = Cursors.WaitCursor;
                con = new SqlConnection(cs.sourceConn1);
                con.Open();
                scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
                SqlDataReader DR = default(SqlDataReader);

                            vochcombox.Items.Clear();
                            DR = scmd.ExecuteReader();
                            foreach (int VouType in DR)
                            {
                                vochcombox.Items.Add(VouType);
                            }
                            while (DR.Read())
                            {
                                this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
                            }
                            DR.Close();
                            con.Close();
                            this.Cursor = Cursors.Default;
                
                        }
                        catch (Exception ex)
                        {
                            this.Cursor = Cursors.Default;
                            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            if (con.State == ConnectionState.Open)
                            {
                                con.Close();
                            }
                        }
                    }
                
                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Member 12899746 wrote:

                foreach (int VouType in DR) {    vochcombox.Items.Add(VouType); }

                You can't use a SqlDataReader like that. Each record could contain multiple fields, and it would have no way of knowing which field you want to convert to an integer.

                Member 12899746 wrote:

                while (DR.Read()) {    this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); }

                That's closer, but still not right. GetOrdinal returns the index of the specified field, not the value of that field. You need to use the GetInt32 method to retrieve the value of the field as an int. You should also wrap your connection, command, and data-reader objects in using blocks.

                public void FillCompVouType()
                {
                Cursor = Cursors.WaitCursor;
                try
                {
                using (var connection = new SqlConnection(cs.sourceConn1))
                using (var command = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", connection))
                {
                vochcombox.Items.Clear();

                        connection.Open();
                        using (var dr = command.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                vochcombox.Items.Add(dr.GetInt32("VouType"));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Cursor = Cursors.Default;
                }
                

                }


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • M Member 12899746

                  public void FillCompVouType()
                  {
                  try
                  {
                  this.Cursor = Cursors.WaitCursor;
                  con = new SqlConnection(cs.sourceConn1);
                  con.Open();
                  scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
                  SqlDataReader DR = default(SqlDataReader);

                              vochcombox.Items.Clear();
                              DR = scmd.ExecuteReader();
                              foreach (int VouType in DR)
                              {
                                  vochcombox.Items.Add(VouType);
                              }
                              while (DR.Read())
                              {
                                  this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
                              }
                              DR.Close();
                              con.Close();
                              this.Cursor = Cursors.Default;
                  
                          }
                          catch (Exception ex)
                          {
                              this.Cursor = Cursors.Default;
                              MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                              if (con.State == ConnectionState.Open)
                              {
                                  con.Close();
                              }
                          }
                      }
                  
                  N Offline
                  N Offline
                  Nelson Costa Inacio
                  wrote on last edited by
                  #8

                  That error is telling you that he cannot convert DR object type to int. DR type is SQLDataReader, you cannot convert data reader to int. Why fill "vochcombox" twice? While loop should work, but foreach doesn't for sure.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups