C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Connection problem


Posted Date: 29 Nov 2008      Posted By: krutarth      Member Level: Bronze     Points: 1   Responses: 4



I am creating application in asp.net. in Page load i m trying to display data from database (MYSQL). i successed in that. but when i clicked on add button and when i selected item from dropdownlist(combobox) at that time connection error is generated and when i click on save button at that time also i have error of connectionstring. so please help me...


In this my code we generated function of connection. and i calling that function in dropdownlist_selectedIndexChanged Event.

error ::

Not allowed to change the 'ConnectionString' property. The connection's current state is open.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Not allowed to change the 'ConnectionString' property. The connection's current state is open.

Source Error:


Line 13:
Line 14: Public Sub cn()
Line 15: cn2.ConnectionString = "DATABASE=invsys;DRIVER={MySQL ODBC 3.51 Driver};OPTION=3;PORT=3306;SERVER=localhost;UID=root;password=root;"
Line 16: cn2.Open()
Line 17: End Sub





Responses

Author: lalitha    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 3

Hi

You are trying to change the "cn2" value when it is open. we get this error when we are trying to open or change a connection which is already in open state. chack the code once

Regards
Lalitha



Author: Pavani    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 5

Hi

Yes, you are trying to change the cn2 value when the connection is open. Try changing it after closing the connection.

But the best way would be defining the connection strings in webconfig file. Even if you have multiple connecvities, specify them in the webconfig and use it where ever you want... but just the only thing to be cautious is opening and closing of the connections. Otherwise such connectivity problems will occur.

Regards,
Pavani



Author: krutarth    29 Nov 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 3

yaa. i understand what r u try to saying. but i cant understand how i vl do. so please can u send me some example. or can u send me some code or hint. i trying but i m not successful to do this thing


Author: Lalit Vasant Patil    29 Nov 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

Hi,

Add below <connecionString> </connectionString>block in your web config file.

<connectionStrings>

<add name="ConString1" connectionString="Address=TestServer;Initial Catalog=Test1;User ID=Test123;pwd=Test123"

providerName="System.Data.SqlClient" />

<add name="ConString2" connectionString="Address=TestServer;Initial Catalog=Test1;User ID=Test123;Password=Test123"

providerName="System.Data.SqlClient" />

</connectionStrings>


Now in below code i have opened two connection for fetching data
from database.


'Open First Database Connection
Dim cnn1 As New Data.SqlClient.SqlConnection()
Dim strConnectionString1 As String = ""
Dim cmd1 As New Data.SqlClient.SqlCommand


Dim objConnectionStringSettings1 As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("ConString1")
strConnectionString1 = objConnectionStringSettings1.ConnectionString
cnn1.ConnectionString = strConnectionString1
cmd1.Connection = cnn1

If cmd1.Connection.State = Data.ConnectionState.Closed Then
cmd1.Connection.ConnectionString = strConnectionString1
cmd1.Connection.Open()
End If

'Open Second Database Connection
Dim cnn2 As New Data.SqlClient.SqlConnection()
Dim strConnectionString2 As String = ""
Dim cmd2 As New Data.SqlClient.SqlCommand
Dim objConnectionStringSettings2 As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("ConString2")
strConnectionString2 = objConnectionStringSettings2.ConnectionString
cnn2.ConnectionString = strConnectionString2
cmd2.Connection = cnn2

If cmd2.Connection.State = Data.ConnectionState.Closed Then
cmd2.Connection.ConnectionString = strConnectionString2
cmd2.Connection.Open()
End If


-Happy Codding(-_-)



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Connection Problem,
Previous : windows service
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use