Click here to Skip to main content
15,891,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Private Sub Cmb_Line_ID_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cmb_Line_ID.GotFocus
    Cmb_Line_ID.Items.Clear()
    Dim Cmb_Line_ID_SQL As String = "select * from line"
    Call Fill_Combo_Box(Cmb_Line_ID_SQL)
    Do While myData.Read
        Cmb_Line_ID.Items.Add(myData.Item(0))
    Loop
End Sub

Private Function Fill_Combo_Box(ByVal ComboSQL As String)
    Dim Con As OleDbConnection
    Dim sqlCmd As OleDbCommand = New OleDbCommand(ComboSQL)
    Dim myData As OleDbDataReader
    Con = New OleDbConnection(Main_Form.ConString)
    Try
        Con.Open()
        sqlCmd.Connection = Con
        myData = sqlCmd.ExecuteReader
        Return myData
    Catch 'ex As OledbException
        Return Err.Number
    End Try
End Function


In Do While the myData reader is empty.
Why??????

Thanks and regards.
Posted
Comments
Sandeep Mewara 7-Apr-11 11:58am    
Surely a silly title! :)

Thanks all of you for yoyr time............

I have a lot of hours in my job..........
 
Share this answer
 
myData is declared inside Fill_Combo_Box function (local variable). You can't "see" it in your Cmb_Line_ID_GotFocus sub.
Solutions:
1) declare myData as global variable (outside both procedures)
or
2) pass reference to your function:
Private Function Fill_Combo_Box(ByVal ComboSQL As String, ByRef myData as OleDbDataReader)
'body
End Function

Using (in Cmb_Line_ID_GotFocus sub):
Fill_Combo_Box(Cmb_Line_ID_SQL, mData)
 
Share this answer
 
Oh, I shudder to look at this. You've got a function (Fill_Combo_Box) that doesn't have a return type. And you're taking advantage of that by returning an OleDbDataReader on success and by returning a number on failure. To make matters worse, you're not capturing the return value when you call it in Cmb_Line_ID_GotFocus. So, since myData is declared as a local variable in Fill_Combo_Box, and since you're not capturing the return value from Fill_Combo_Box in Cmb_Line_ID_GotFocus, you can't see it in Cmb_Line_ID_GotFocus.

You need to go back and take a look at some instruction on how to properly declare and use functions, including using their return values.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Apr-11 15:20pm    
Well, good catch, my 5.
--SA
Because even though you're calling the Fill_Combo_Box method, you're not actually using it's returned object in the calling method. You probably want this:

VB
Dim myData as OleDbDataReader = Fill_Combo_Box(Cmb_Line_ID_SQL)


Not only that, but the method in question doesn't have a return type, which should be OleDbDataReader.
 
Share this answer
 
v3
Comments
Wild-Programmer 7-Apr-11 11:58am    
+5 to you.
Sergey Alexandrovich Kryukov 7-Apr-11 15:20pm    
Mine too.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900