Md. Sinin

Md. Sinin

  • NA
  • 2
  • 3.8k

System.InvalidCastException: 'Specified cast is not valid

Oct 21 2018 1:12 PM
I want to make a system where i can show data from two table with two combobox but when i am about to complete i have shown this exception and i cann't solve this so please help me here is my full code..
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using Oracle.DataAccess.Client;  
  11. namespace combobox  
  12. {  
  13. public partial class Form1 : Form  
  14. {  
  15. List movies = new List();  
  16. List catagories = new List();  
  17. public Form1()  
  18. {  
  19. InitializeComponent();  
  20. }  
  21. private void Form1_Load(object sender, EventArgs e)  
  22. {  
  23. string connection = "Data Source = (DESCRIPTION = " +  
  24. "(ADDRESS = (PROTOCOL = TCP)(HOST = Mihadul)(PORT = 1521))" +  
  25. "(CONNECT_DATA =" +  
  26. "(SERVER = DEDICATED)" +  
  27. "(SERVICE_NAME = tick)" +  
  28. ")" +  
  29. ");User ID=mihadul;Password=mihadul123;";  
  30. OracleConnection con = new OracleConnection(connection);  
  31. con.Open();  
  32. OracleCommand cmdcat = new OracleCommand("SELECT * FROM CATOGORY", con);  
  33. OracleDataReader dr = cmdcat.ExecuteReader();  
  34. while (dr.Read())  
  35. {  
  36. cmb_catlist.Items.Add(dr["CAT_NAME"]);  
  37. catagories.Add(new Category()  
  38. {  
  39. cat_id = ((int)dr["cat_id"]),  
  40. cat_name = dr["cat_name"as string  
  41. });  
  42. }  
  43. con.Close();  
  44. con.Open();  
  45. OracleCommand cmdmovie = new OracleCommand("SELECT * FROM MOVIES", con);  
  46. OracleDataReader dr1 = cmdmovie.ExecuteReader();  
  47. while (dr1.Read())  
  48. {  
  49. movies.Add(new Movie()  
  50. {  
  51. id = ((int)dr1["id"]),  
  52. movie_name = dr1["movie_name"as string,  
  53. cat_id = ((int)dr1["cat_id"])  
  54. });  
  55. }  
  56. con.Close();  
  57. }  
  58. private string[] GetMovieById(int id)  
  59. {  
  60. return movies.Where(line => line.cat_id == id).Select(l => l.movie_name).ToArray();  
  61. }  
  62. private void cmb_catlist_SelectedIndexChanged(object sender, EventArgs e)  
  63. {  
  64. cmb_movlist.Items.Clear();  
  65. int id = catagories[cmb_catlist.SelectedIndex].cat_id;  
  66. foreach (string name in GetMovieById(id))  
  67. {  
  68. this.cmb_movlist.Items.Add(name);  
  69. }  
  70. }  
  71. }  
  72. [Serializable]  
  73. class Category  
  74. {  
  75. public int cat_id { getset; }  
  76. public string cat_name { getset; }  
  77. }  
  78. [Serializable]  
  79. class Movie  
  80. {  
  81. public int id { getset; }  
  82. public string movie_name { getset; }  
  83. public int cat_id { getset; }  
  84. }  
  85. } 

Answers (1)