Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i m trying to run my code but it give me an error."Not allowed to change the"connectionstring property".The connection's current state is open " how to solve this error??

below is my code..
VB
  1  Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5. Click
  2          Try
  3              Dim file_name As String = "E:\EXPORTDATA\customerInformation.txt"
  4              Dim txtline, q, data(4) As String
  5  
  6  
  7              If System.IO.File.Exists(file_name) = True Then
  8                  Dim objreader As New System.IO.StreamReader(file_name)
  9                  Do While objreader.Peek <> -1
 10                      ' Storing line to string variable
 11                      txtline = objreader.ReadLine() & vbNewLine
 12                      'breaking the line & stroing to array variable
 13                      data = txtline.Split(",")
 14                      q = "insert into customer_info values(@CUSTOMER_NAME,@CUSTOMER_ID,@CUSTOMER_ADD,@CITY)"
 15                                con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:E\connectivity\MYDB.accdb;"
 16  
 17                      con.Open()
 18  
 19                      cmd = New OleDbCommand(q, con)
 20                      cmd.Connection = con
 21  
 22                      With cmd.Parameters
 23                          .Add("@CUSTOMER_NAME", OleDbType.VarChar).Value = data(0)
 24                          .Add("@CUSTOMER_ID", OleDbType.VarChar).Value = data(1)
 25                          .Add("@CUSTOMER_ADD", OleDbType.VarChar).Value = data(2)
 26                          .Add("@CITY", OleDbType.VarChar).Value = data(3)
 27  
 28                      End With
 29                      cmd.ExecuteNonQuery()
 30                      con.Close()
 31                  Loop
 32              End If
 33  
 34              MessageBox.Show("Data Saved")
 35  
 36          Catch ex As Exception
 37              MsgBox(ex.Message.ToString)
 38          End Try
 39  
 40      End Sub
Posted
Updated 24-Apr-13 23:30pm
v3
Comments
Have you defined the connectionstring at any other place ?
The error means that. Please check your code.
Akinmade Bond 26-Apr-13 10:17am    
Have you tried the solution?
gkelkar 29-Apr-13 4:32am    
yes,i trying ur solution and prb is gone....
SADIQ BJ 31-Mar-14 5:35am    
where is the problem please tell me i am also facing the same problem,

On line 17, you opened a connection to the database and on line 20, you want to change the ConnectionString.

Interchange these two line and that should do it.
 
Share this answer
 
Your variable con is defined somewhere else in your project, and it's state is open from another connection. Double check you program for sql-connections which is not closed after use.
 
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