SqlDataReader.GetInt32 problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devi
    New Member
    • Sep 2011
    • 1

    SqlDataReader.GetInt32 problem

    I'm using this method to populate a comboBox directly from the db but when i execute i get this error: InvalidCastExce ption was unhandled.Speci fied cast is not valid.

    here is the method.
    private void buildcboAuthors ()
    {
    string strQuery = "Select k.Name as State_Name from States k where k.Name_ID=19 or k.Name_ID=17 or k.Name_ID=20 or k.Name_ID=11 or k.Komuna_ID=04" ;

    SqlCommand cmd = new SqlCommand(strQ uery, sqlConn);

    this.sqlConn.Op en();
    SqlDataReader dtRead = cmd.ExecuteRead er();
    ArrayList Komuna = new ArrayList();
    while (dtRead.Read())
    {
    Komuna.Add(new AddValue(dtRead .GetString(1), dtRead.GetInt32 (0)));

    }
    dtRead.Close();
    this.sqlConn.Cl ose();
    this.cmbStates. DataSource = Komuna;
    this.cmbStates. DisplayMember = "View";
    this.cmbStates. ValueMember = "Value";

    }
    what am I doing wrong ???

    can anyone help me
    regards devi
  • PianoMan64
    Recognized Expert Contributor
    • Jan 2008
    • 374

    #2
    Try this Option

    Trying adding a .ToString() to the end of the GetInt32() Method
    Code:
         Komuna.Add(new AddValue(dtRead.GetString(1),
                    dtRead.GetInt32(0)[B].ToString()[/B]));

    Comment

    Working...