Member Avatar for Rahul47

hello people, I have the following piece of code.

Private Sub SR_No()
        Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
        Dim cmd As New SqlCommand("Select MAX(SR_No) from Stud_Master", con)

        con.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        MessageBox.Show(sr)
        con.Close()
    End Sub

I wish to fetch SR_No into a variable sr.
Advices are welcome.

Thanks

Member Avatar for Rahul47

Above Problem is solved as follows.

Private Sub SR_No()
        Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\VB Applications\PROJECT_1\PROJECT_1\examdb.mdf;Integrated Security=True;User Instance=True")
        Dim cmd As New SqlCommand("Select MAX(SR_No) from Stud_Master", con)

        con.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        sr = dr.GetValue(0)
        MessageBox.Show(sr)
        con.Close()
    End Sub

Thanks.

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.