Results 1 to 5 of 5

Thread: Casting Error?? [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Resolved Casting Error?? [RESOLVED]

    What i am trying to do is load a combo box on a form frmStudents. The way i am going about doing it is my frmstudent on load event has this code

    VB Code:
    1. Private stu As Student
    2.     Private Sub frmDisplayStudent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         stu = New Student
    4.         cboStudents.DataSource = stu.GetStudents
    5.         cboStudents.DisplayMember = "FIRSTNAME"
    6.     End Sub

    stu calls to class Student which as a function of GetStudents which does this

    VB Code:
    1. Public Function GetStudents() As ArrayList
    2.  
    3.         conn = New SqlConnection(connstring)
    4.         Dim sql As String = "SELECT * FROM Student"
    5.         comm = New SqlCommand(sql, conn)
    6.         Dim stu As New Student
    7.         Dim students As New ArrayList
    8.  
    9.         conn.Open()
    10.         Dim reader As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
    11.  
    12.         Do While reader.Read
    13.             stu = New Student(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), _
    14.                                 reader.GetString(4), reader.GetString(5), reader.GetString(6), reader.GetString(7))
    15.             students.Add(stu)
    16.         Loop
    17.         reader.Close()
    18.         conn.Close()
    19.  
    20.         Return students
    21.     End Function

    When i try to load the form it says i have a casting error? specifically "Specified cast is not valid."
    Last edited by SaitStudent; Nov 12th, 2006 at 11:43 PM. Reason: Resolved

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: Casting Error??

    o and i am using sqlServer 2005 and visual studio 2005, code is in VB

  3. #3
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Casting Error??

    Quote Originally Posted by SaitStudent
    o and i am using sqlServer 2005 and visual studio 2005, code is in VB
    Hmm...this is just a guess since I don't use VB.NET but maybe you're using .GetString on something that isn't a string, or .GetInt32 on something that isn't an integer. I'd check the data types of the 'Student' table against your code to make sure you're using the right methods, though I've found .GetString doesn't seem to care about the data type so I may just be talking out of my rear end.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: Casting Error??

    Yes it was thank you i was using int32 instead of int64

  5. #5
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: Casting Error??

    Quote Originally Posted by SaitStudent
    Yes it was thank you i was using int32 instead of int64
    Woo, yay! Can you mark the thread as resolved?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width