Why This error is coming...................
There is already an open datareader is associated with this command which must be closed

    Private Sub AppendItem(ByVal Productid As String, ByVal quantity As Integer)
            productid1 = Productid

            myConnection = New SqlConnection(connectionstring)
            myConnection.Open()
            Dim TempData As New DataTable
            Dim pid As String = ComboBox1.SelectedItem
            myCommand1 = New SqlCommand("SELECT P_Selling_Price,Discounted_Price FROM Product_Details WHERE P_Id='" & pid & "' ", myConnection)
            myCommand1.ExecuteNonQuery()
            'sqlcmd = New SqlCommand("SELECT P_Id,P_Selling_Price FROM Product_Details WHERE P_Id='" & pid & "' ", myConnection)
            Dim dr As SqlDataReader = myCommand1.ExecuteReader

            dr.Read()

            ''''''''''''''''''''''''
            ' For i = 0 To TempData.Rows.Count-1
            Dim newItem As New ListViewItem(Productid)
            newItem.SubItems.Add(dr("P_Selling_Price"))

            newItem.SubItems.Add(dr("Discounted_Price"))

            newItem.SubItems.Add(quantity)
            newItem.SubItems.Add(dr("P_Selling_Price") * quantity)

            ' newItem.SubItems.Add()
            ListView1.Items.Add(newItem)


            '   Next
            If dr.HasRows Then
                'ok
                MessageBox.Show("Succesfully updated  !!", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
            Else
                MessageBox.Show("Data doesn't exits  !!", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

            End If


            CalTotal()



            dr.Close()


            Dim productname As String = Product()
            Dim insert As SqlCommand = New SqlCommand("Insert into Sells (Bill_No,P_Id,Category,No_Of_Units_sold,Subtotal,Name) values ('" & Val(TextBox14.Text) & "','" & pid & "','" & ComboBox2.SelectedItem & "','" & TextBox1.Text & "','" & TextBox5.Text & "','" & productname & "')", myConnection)
            insert.ExecuteNonQuery()
            insert.Cancel()
            MessageBox.Show("Data is added. !", "", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)



            ''''''''''''''''''''''''''''''''''''''''''''''''''''''


            myConnection.Close()
        End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

  Public Function Product()
            Dim pname As String
            myConnection = New SqlConnection(Module1.connectionString)
            myConnection.Open()

            myCommand2 = New SqlCommand("SELECT [Name] FROM [VBP].[dbo].[Product_Name_List] WHERE P_Id='" & ComboBox2.Text & "' ", myConnection)
            myCommand2.ExecuteNonQuery()
            Dim dr1 As SqlDataReader = myCommand2.ExecuteReader
            While dr1.Read()

                Debug.WriteLine(dr1(0))

                pname = (dr1(0).ToString)

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

            End While
            Return (pname)
            '' inhere notification is coming the error the varile is using before assiging

            dr1.Close()

            myConnection.Close()

        End Function







  Dim productname As String = Product()

  In here product name is retriving from product() function

Recommended Answers

All 4 Replies

Try changing line 64 to Dim pname As String = ""

Then how to retrive the value from the function............

It is complaining because the compiler has determined that if the loop doesn't get executed then pname will not have a value to return at line 80. By assigning a null string when you Dim pname you ensure that pname will have a value no matter what execution path is taken. However, you have another problem. The last few lines of your function look like

    Return (pname)

    dr1.Close()
    myConnection.Close()

End Function

The two lines following the Return can never be executed because of the Return statement. You should reorder the lines as

    dr1.Close()
    myConnection.Close()
    Return (pname)

End Function

When I changed the coding as you had mention I got error as ExecuteNonQuery is closed have to open it...........
then change the coding as this

 dr1.Close()
 Return (pname)
myConnection.Close()

Then I didn't get any errors but there wasn't any data in the database then changed the sql query as this

 myCommand2 = New SqlCommand("SELECT [VBP].[dbo].[Product_Name_List].[Name] FROM  [VBP].[dbo].[Product_Name_List] FULL JOIN Sells ON [VBP].[dbo].[Product_Name_List].P_Id=Sells.P_Id", myConnection)

        myCommand2.ExecuteNonQuery()

but when i changed like that in the database product name became same
Primary keys of Product_Name_List table are P_Id & Category it is a compsite key
in the Sells table Primary keys are P_Id and Bill_No ,that is also a compsite key and at the same time I made P_Id and Category are foreign keys..........

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.