Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Private Sub tool_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tool_save.Click
        'On Error Resume Next

        Dim cmd As New SqlCommand
        cmd.Connection = _OpenConnect()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO Customer_Info (Cust_Name,Address,City,Phone,Cust_Invoice) Values (@Cust_Name,@Address,@City,@Phone,@Cust_Invoice)"
        With cmd.Parameters
            .AddWithValue("@Cust_Name", txt_custName.Text)
            .AddWithValue("@Address", txt_address.Text)
            .AddWithValue("@City", txt_city.Text)
            .AddWithValue("@Phone", txt_phone.Text)
            .AddWithValue("@Cust_Invoice", txt_cust_Invoice.Text)
        End With

        cmd.ExecuteNonQuery()


        txt_custName.Focus()
        Message("Inserted Successfully")
        ClearAll(Me)
        Cust_Info.AttachData()
        main.GetBasicInfo()
    End Sub


====================================================
Same Problem
This type error are given

There is already an open DataReader associated with this Command which must be closed first.
Posted
Updated 10-Jun-12 21:39pm
v3

Like the error says there is an open Datareader on your connection. Check your code before this routine and close the DataReader first then call this sub.
 
Share this answer
 
Comments
gauravrank 11-Jun-12 3:41am    
Public Function _OpenConnect() As SqlConnection
Try
If _cn.ConnectionString = "" Then
_cn.ConnectionString = _GlobalConString
End If
If _cn.State = ConnectionState.Closed Then
_cn.Open()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return _cn
End Function

This Function is my connection function
Mehdi Gholam 11-Jun-12 3:45am    
A part of your code is using this connection with a datareader. Search for "datareader" in your project and work from there to see where you are not closing the datareader.
gauravrank 11-Jun-12 3:47am    
i alredy check that all datareader is closed
Mehdi Gholam 11-Jun-12 3:52am    
You will have to trust the computer when it says it is not closed :)
gauravrank 11-Jun-12 3:56am    
pls when you free then i share my pc in teamviewer pls chek
Look at that line:
cmd.Connection = _OpenConnect()
and then to a line in that function
_cn.ConnectionString
You use always the same SqlConnection object!
That's the reason for your problems. Create a new SqlConnection object when required.
 
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