Click here to Skip to main content
15,900,378 members
Home / Discussions / C#
   

C#

 
GeneralCreating a Preferences/Settings dialog. Pin
Marlun11-Jul-04 8:02
Marlun11-Jul-04 8:02 
GeneralRe: Creating a Preferences/Settings dialog. Pin
Roman Rodov11-Jul-04 20:31
Roman Rodov11-Jul-04 20:31 
Generalgdi+ problem Pin
kendao11-Jul-04 6:42
kendao11-Jul-04 6:42 
GeneralRe: gdi+ problem Pin
Name Removed11-Jul-04 10:53
Name Removed11-Jul-04 10:53 
GeneralRe: gdi+ problem Pin
Jay Shankar11-Jul-04 18:57
Jay Shankar11-Jul-04 18:57 
GeneralC# WinSock Control Pin
Axonn Echysttas11-Jul-04 6:31
Axonn Echysttas11-Jul-04 6:31 
Generalwindows folder properties Pin
Hugo Hallman11-Jul-04 2:39
Hugo Hallman11-Jul-04 2:39 
Generalconfig file in console app to store connection string help Pin
mtbjr11-Jul-04 1:17
mtbjr11-Jul-04 1:17 
My console project is named UsingConfigSettings in C#, and wanted to store the database connection in an application config file. I added an application config named "UsingConfigSettings.exe.config" in my project. Here is its contents:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="constring" value="server=localhost;database=Northwind;uid=sa;pwd=;"/>
</appSettings>
</configuration>

I even copied it in the bin\debug folder in my C# console application, but it still doesn't work. I acccess it in the console as:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public class UsingConfigSettings
{
public static void Main()
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["constring"]);

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Customers";
cmd.Connection = con;

con.Open();
SqlDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
Console.WriteLine("{0} - {1}", reader.GetString(0), reader.GetString(1));
}

con.Close();

Console.ReadLine();
}

}

but it won't work, it always issued a message of:

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.data.dll

Additional information: The ConnectionString property has not been initialized."

If I don't use any config file, and replace the code above, it works fine:

SqlConnection con = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=;");

Please help.
GeneralQuery for data between dates Pin
Anonymous11-Jul-04 0:24
Anonymous11-Jul-04 0:24 
GeneralRe: Query for data between dates Pin
Dave Kreskowiak11-Jul-04 7:17
mveDave Kreskowiak11-Jul-04 7:17 
GeneralMSIL vs JAVA Bytecode vs Intel Assmebly Language Pin
Hashim Saleem11-Jul-04 0:20
Hashim Saleem11-Jul-04 0:20 
GeneralRe: MSIL vs JAVA Bytecode vs Intel Assmebly Language Pin
Steven Campbell11-Jul-04 5:26
Steven Campbell11-Jul-04 5:26 
GeneralRe: MSIL vs JAVA Bytecode vs Intel Assmebly Language Pin
leppie11-Jul-04 5:28
leppie11-Jul-04 5:28 
GeneralRe: MSIL vs JAVA Bytecode vs Intel Assmebly Language Pin
Dave Kreskowiak11-Jul-04 7:16
mveDave Kreskowiak11-Jul-04 7:16 
Generalread address of I/o Pin
ahmed mohamed abdelhameed10-Jul-04 16:03
ahmed mohamed abdelhameed10-Jul-04 16:03 
GeneralTextbox input. Pin
Nick Z.10-Jul-04 13:48
Nick Z.10-Jul-04 13:48 
GeneralRe: Textbox input. Pin
Nick Parker10-Jul-04 18:52
protectorNick Parker10-Jul-04 18:52 
GeneralRe: Textbox input. Pin
Nick Z.10-Jul-04 19:09
Nick Z.10-Jul-04 19:09 
Generalconfig file in console app to store connection string help Pin
Anonymous10-Jul-04 13:19
Anonymous10-Jul-04 13:19 
GeneralRe: config file in console app to store connection string help Pin
Colin Angus Mackay10-Jul-04 16:14
Colin Angus Mackay10-Jul-04 16:14 
GeneralDelete some files after installation Pin
michalJ10-Jul-04 10:47
michalJ10-Jul-04 10:47 
GeneralRe: Delete some files after installation Pin
mav.northwind11-Jul-04 5:20
mav.northwind11-Jul-04 5:20 
GeneralCountries and towns database Pin
machocr10-Jul-04 9:05
machocr10-Jul-04 9:05 
GeneralRe: Countries and towns database Pin
Colin Angus Mackay10-Jul-04 16:20
Colin Angus Mackay10-Jul-04 16:20 
GeneralWindows Services Pin
RFickling10-Jul-04 7:07
RFickling10-Jul-04 7:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.