Hi All,

I am working on a windows application in vb.net. (School Management Project).
My problem is that, when I am adding the Personal Details of a student,(which include enrolmentno(primary key in the database),name,class,section,etc), when I click the save button what I want is that the enrolmentno should appear on the ParentDetail Form without again entering it. (later that would be used as a foreign key to the PARENTDETAIL table in the database), but I am not able to get the value of enrolmentno from my previous form to the next form.

I declared the variable as Public in the PersonalDetail Form and then tried to access that variable in the ParentDetail Form by instantiating the object of the previous form, but that doesnt work.

Public Class PersonalDetail
Public enrolmentno As Integer
....
End Class

....

Public Class ParentDetail

Public Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click

Dim obj As New PersonalDetail

Dim _enrol As Integer
...
_enrol = obj.enrolmentno ' this shows the value 0 in the form
....
End Sub
....
End Class

I also tried to get the value of enrolmentno from the database using the ExecuteRead command. but I couldn't findout how to retrieve the last entered columns field using ExecuteRead.

Dim dr As SqlDataReader
Dim er As Integer
Dim cmd1 As SqlCommand

con = New SqlConnection("Server=localhost;" & "DataBase=SCHOOL;" & "Integrated Security=SSPI")
cmd1 = New SqlCommand("SELECT * FROM PERSONALDETAIL ", con)
dr = cmd1.ExecuteReader

While dr.Read
_er = dr.GetInt32(0)
  _enrol = er
  End While
  dr.Close()

End Sub

'but this enters the value of the first record int the database.

I am new to VB.Net. Please Help. I hope my problem is clear.

Thanks.

Recommended Answers

All 2 Replies

Here, I have two froms:

Form1

public Class Form1
        public dataForm1 as Integer
       ......     
       Sub button1_click(..)
            Form2.dataForm2=10
           Form2.Show()
      End Sub
 End Class

Form2

public Class Form2
        public dataForm2 as Integer
       ......     
       Sub button1_click(..)
            MsgBox("Data from Form1 " & dataForm2)
            Form1.dataForm1=20
            Form1.Label1.Text="Test"
      End Sub
 End Class

For the latter approach

I couldn't findout how to retrieve the last entered columns field using ExecuteRead

use

Dim oCmd As SqlCommand
.
.
strSQL = "INSERT INTO <table> (<fields>) VALUES (<values>)"
strSQL = strSQL & "; SELECT @@IDENTITY AS 'Identity' "
oCmd.CommandText = strSQL

NewIdentity = CInt(oCmd.ExecuteScalar())

in your insert statement. See Save binary data to SQL Server with VB.NET in my blog for a more complete example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.