Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms: form 1 have one textbox in which i want to enter student Id number with an Ok button. after entering student Id number in form 1 and click Ok, i want to get records from the database using OleDbdatareader about the only student loaded into the form 2 textboxes. How can i achieve this? please help! Sorry, Here is what i have under the Ok button click event:

Dim cnn As New OleDbConnection
cnn.ConnectionString = "my connection string..."
Dim cmd AsNew OleDbCommand
cmd.CommandText = "SELECT Id, Fname, Lname FROM student"
cmd.Connection = cnn
Dim sdr As OleDbDataReader
sdr = cmd.ExecuteReader

If sdr.HasRows Then

dr.Read()

Form 2.Load()

txt_Id.text = dr.Item("Id")
txt_Fname.text = dr.Item("Fname")
txt_Lname.text = dr.Item("Lname")


sdr.Close()

End If
cnn.Close()
Posted
Updated 27-May-13 22:13pm
v2
Comments
Basmeh Awad 28-May-13 4:00am    
so where did you stuck???
what efforts did you made???
show your code...

Try something like this:
VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using cmd As New SqlCommand("SELECT myColumn1, MyColumn2 FROM myTable WHERE ID=@ID", con)
                cmd.Parameters.AddWithValue("@ID", studentId)
		Using reader As SqlDataReader = cmd.ExecuteReader()
			While reader.Read()
				Dim col1 As Integer = CInt(reader("myColumn1"))
				Dim col2 As String = DirectCast(reader("myColumn2"), String)
				' and put the values on your form.
			End While
		End Using
	End Using
End Using
 
Share this answer
 
Comments
dflo 28-May-13 4:18am    
Thanks, this is really cool but how can i call the second form?
Basmeh Awad 28-May-13 5:21am    
try Solution2
OriginalGriff 28-May-13 5:23am    
Dim f2 as New Form2
f2.MyProperty1 = value1
f2.MyProperty2 = value2
f2.ShowDialog

When you want to transfer data from a parent form to a child, set up public properties in the child and set the textboxes in the properties. That way the parent doesn't need to know how teh child works.

VB
Dim connetionString As String
        Dim cnn As SqlConnection
        Dim cmd As SqlCommand
        Dim sql As String
        Dim reader As SqlDataReader

        connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
        sql = "Select *  from myTable WHERE ID=" & Textbox1.Text & ""
        cnn = New SqlConnection(connetionString)
        Try
            cnn.Open()
            cmd = New SqlCommand(sql, cnn)
            reader = cmd.ExecuteReader()
          if reader.hasrows() then
           While reader.Read()
               dim frm2 as Form2
               frm2.Textbox1.Text=reader(0)
               frm2.Textbox2.Text=reader(1) ' And so on
               frm2.ShowDialog() 'or you can just use .Show()
            End While
         End If
            reader.Close()
            cmd.Dispose()
            cnn.Close()
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        End Try
 
Share this answer
 

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