error when app.config in sharpdevelop for connectionstring

995 views Asked by At

i have very strange problem that made me stuck in my project .. i'm using C#(SharpDevelop 4.3.3 build 9663) ..

  • When i use connection string from app.config i get error when trying to open connection :

c.Open();

app.config <================ file

    <?xml version="1.0"?>
<configuration>


<connectionStrings>

    <add name="databasecon" connectionString="Data Source=ahmed\\sqlexpress;Initial Catalog=abumanahilms;Integrated Security=True" />

</connectionStrings>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

button1 codes .. in this <================ form1

void Button1Click(object sender, EventArgs e)
    {

        string cs=ConfigurationManager.ConnectionStrings["cs"].ToString();


        SqlConnection c = new SqlConnection(cs);


        SqlCommand sc = new SqlCommand("insert into table1 (money) VALUES ('"+textBox1.Text+"')",c);

        SqlDataReader sr;
        c.Open();

        sr = sc.ExecuteReader();


        MessageBox.Show("success");



    }

but when i put direct string <================ form1

string cs="Data Source=ahmed\\sqlexpress;Initial Catalog=test;Integrated Security=True";

it works fine ...

The error i get -------------------------------------------------------------------------

    System.InvalidOperationException: Instance failure.
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at teeest.MainForm.Button1Click(Object sender, EventArgs e) in c:\Users\Ahmed Albusaidi\Documents\SharpDevelop Projects\teeest\teeest\MainForm.cs:line 59
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at teeest.Program.Main(String[] args) in c:\Users\Ahmed Albusaidi\Documents\SharpDevelop Projects\teeest\teeest\Program.cs:line 27

2) I also tried to take the connectionstring from text file

string cs= File.ReadAllText("connectionstring.txt").ToString();

also:

string cs= File.ReadAllText("connectionstring.txt");

i get exactly same error :)

I hope to get help .. Thanks in advance :)

1

There are 1 answers

3
Matt Ward On BEST ANSWER

The problem is that you are using \\ in your app.config. You should only have the single backslash in the app.config. There is no need to encode the string in the app.config. You only need to do that in your C# code. Change the data source to be ahmed\sqlexpress with just a single backslash:

<connectionStrings>
    <add name="databasecon" connectionString="Data Source=ahmed\sqlexpress;Initial Catalog=abumanahilms;Integrated Security=True" />
</connectionStrings>

Another problem with your code is that app.config has a connection string with a name of 'databasecon' but your code is using 'cn'. However ConfigurationManager.ConnectionStrings["cs"] would return a null reference if the 'cn' connection string was missing so I suspect your code and app.config are different to what you have posted in your question.