Dim major As String = TextBox1.Text
        Dim depa As String = TextBox2.Text
        Dim sname As String = TextBox3.Text
        Dim tbStudent As DataTable
        Dim dcPrimaryKey(0) As DataColumn
        tbStudent = sqlDataset.Tables("Major")

        Dim c As Integer
        'selecting the entire rows from dbase
        myCommand = New SqlCommand("SELECT  count(*) FROM Major", myConnection)
        myConnection.Open()

        'sqlquery1 = " select count(*) from <tablename>"
        ' cmd = New SqlCommand(sqlquery1, con)
        c = myCommand.ExecuteScalar
        Dim drSubject As System.Data.DataRow
        drSubject = tbStudent.Rows.Find(major)

        If c <> 0 Then
            '
            myCommand = New SqlCommand("SELECT  MajorName,Departement,Suject_Name from Major order by MajorName", myConnection)

            Dim dr As SqlDataReader = myCommand.ExecuteReader


            '''''check from here
            While dr.Read
                tbStudent.Rows.Add()


                TextBox1.Text = drSubject.Item("MajorName")
                TextBox2.Text = drSubject.Item("Departement")
                TextBox3.Text = drSubject.Item("Suject_Name")


            End While

        End If

        myConnection.Dispose()

Recommended Answers

All 12 Replies

can you give us what is the problem with the code ???

what error message message do you get?

From looking at your code:

Line 6:

tbStudent = sqlDataset.Tables("Major")

Is sqlDataSet filled with data?

Line 17:

drSubject = tbStudent.Rows.Find(major)

Is your TextBox1 filled?

Line 31 - 33

TextBox1.Text = drSubject.Item("MajorName")
TextBox2.Text = drSubject.Item("Departement")
TextBox3.Text = drSubject.Item("Suject_Name")

You pull data from Line 21, and read it in Line 27 - 36, but the textboxes are pulling data from drSubject and not the reader (declared dr in Line 23). Also, Lines 21 and 33 is the field Suject or Subject?

Possible Lines 31-33 should be:

TextBox1.Text = dr.Item("MajorName")
TextBox2.Text = dr.Item("Departement")
TextBox3.Text = dr.Item("Suject_Name")

thanks every body.... i'll try

but i'm getting the error like this
"Object reference not set to an instance of an object."
and i show in the error in the

>

 drSubject = tbStudent.Rows.Find(major)

17 line

@ Maligui the field is Suject it is a spelling mistake........

Public Class Form3
    Dim connectionstring As String = Module1.connectionString
    Dim sqlconn As New SqlConnection(connectionString)
    Public sqlDataset As New DataSet
    Dim myCommand1 As SqlCommand
    Public SQLdr As SqlDataReader
    Dim myConnection As SqlConnection
    Dim nxtmajor As String
    Dim sqlcmd As New SqlCommand
    'Dim dep As String
    Dim tbStudent As DataTable
    Public n As Integer
    'Dim sname As String

    Dim myCommand As SqlCommand

these are the varible i declare ................

Again I changed th coding like this

 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim major As String = TextBox1.Text
        Dim depa As String = TextBox2.Text
        Dim sname As String = TextBox3.Text
        Dim tbStudent As DataTable
        Dim dcPrimaryKey(0) As DataColumn
        tbStudent = sqlDataset.Tables("Major")

        Dim c As Integer
        'selecting the entire rows from dbase
        myCommand = New SqlCommand("SELECT  count(*) FROM Major", myConnection)
        myConnection.Open()
        Dim dr As SqlDataReader = myCommand.ExecuteReader
        'sqlquery1 = " select count(*) from <tablename>"
        ' cmd = New SqlCommand(sqlquery1, con)
        dr = myCommand.ExecuteScalar
        Dim drSubject As System.Data.DataRow
        drSubject = tbStudent.Rows.Find(major)

        If c <> 0 Then
            '
            myCommand = New SqlCommand("SELECT  MajorName,Departement,Suject_Name from Major order by MajorName", myConnection)


            '''''check from here
            While dr.Read
                tbStudent.Rows.Add()


                TextBox1.Text = dr.Item("MajorName")
                TextBox2.Text = dr.Item("Departement")
                TextBox3.Text = dr.Item("Suject_Name")


            End While

        End If

        myConnection.Dispose()

    End Sub

The error message i gettin is :There is already an open DataReader associated with this Command which must be closed first.

 dr = myCommand.ExecuteScalar

It looks like you are trying to use the same objects for every operation. Try to avoid this since it is back practice and may cause exceptions when two methods are trying to access the same oject at the same time - such as this case. Your data reader (dr) was already being used somewhere else. and since it was not closed, the reader is still open.

After your read is finished reading use the dr.Close method.

 '''''check from here
 While dr.Read
     tbStudent.Rows.Add()


     TextBox1.Text = dr.Item("MajorName")
     TextBox2.Text = dr.Item("Departement")
     TextBox3.Text = dr.Item("Suject_Name")
 End While

 ' Close the reader
 dr.Close()

Best practice is that you scope your objects to where they are needed. This will avoid memory locks, and helps the overall readability of the design. For example, your data reader is really only needed here. So, you should declare a datareader for the scope of this method. That way is is disposed after the method executes.

Maligui
i create new varible to read the data now i gettin error like this
"Object reference not set to an instance of an object."

THE error is showing in
this line

     drSubject = tbStudent.Rows.Find(major)



Dim major As String = TextBox1.Text
        Dim depa As String = TextBox2.Text
        Dim sname As String = TextBox3.Text
        Dim tbStudent As DataTable
        Dim dcPrimaryKey(0) As DataColumn
        tbStudent = sqlDataset.Tables("Major")

        Dim c As Integer
        'selecting the entire rows from dbase
        myCommand = New SqlCommand("SELECT  count(*) FROM Major", myConnection)
        myConnection.Open()
        'Dim dr As SqlDataReader = myCommand.ExecuteReader
        'sqlquery1 = " select count(*) from <tablename>"
        ' cmd = New SqlCommand(sqlquery1, con)
        Dim SQLdr As SqlDataReader = myCommand.ExecuteReader
        Dim drSubject As System.Data.DataRow


        If c <> 0 Then
            '
            myCommand1 = New SqlCommand("SELECT  MajorName,Departement,Suject_Name from Major order by MajorName", myConnection)


            '''''check from here     drSubject = tbStudent.Rows.Find(major)
            While SQLdr.Read
                tbStudent.Rows.Add()


                TextBox1.Text = SQLdr.Item("MajorName")
                TextBox2.Text = SQLdr.Item("Departement")
                TextBox3.Text = SQLdr.Item("Suject_Name")


            End While

        End If
        SQLdr.Close()

        myConnection.Dispose()

Did you assign it?

Dim MyNewDataReader As OleDbDataReader = command.ExecuteScalar

What line is the error comming from?

i'm using Sql 2005

       Dim SQLdr As SqlDataReader = myCommand.ExecuteReader

THE ERROR IS :
"ExecuteReader requires an open and available Connection. The connection's current state is closed."

Thank you Maligui I got it .

     myCommand = New SqlCommand("SELECT Quantity FROM Product_Details WHERE P_Id = '" & ComboBox2.Text & "'", myConnection)
        Dim dr As SqlDataReader = myCommand.ExecuteReader
        While dr.Read()

            TextBox4.Text = dr(0).ToString



            'currentbalance = Double.Parse(dr(2).ToString)

        End While
        dr.Close()
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.