Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, i am able to connect to my local db file .sdf directly with my winform app. but unable to do so when i am using an connstring. here is what i have tried

1. using app.config file

C#
var config =  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.ConnectionStrings["DEVID"].ConnectionString = textBox6.Text;
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
            SqlCeConnection con = new SqlCeConnection();
            string DEVID = ConfigurationManager.ConnectionStrings["DEVID"].ConnectionString;
            con.ConnectionString = DEVID; // getting error as "object reference not set"
//SqlCeConnection con = new SqlCeConnection(DEVID); // in my app.config file i set my connection string = "Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf


2. using direct string variable

C#
//SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf"); // this is the actual conn string value
            string connstring1 = "\"Data Source=" + textBox6.Text + "\"";
            string connstring2 = "@"+connstring1;
            textBox2.Text = connstring2; // matches exactly with my actual connection string, but still not getting to connect with .sdf file
SqlCeConnection con = new SqlCeConnection(connstring2); // getting error as format of initialization string does not conform at index 0""


please help me out to solve the issue

What I have tried:

//SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\john\Documents\Visual Studio 2010\Projects\AECCS\AECCS\Database1.sdf"); // this is the actual conn string value
string connstring1 = "\"Data Source=" + textBox6.Text + "\"";
string connstring2 = "@"+connstring1;
textBox2.Text = connstring2; // matches exactly with my actual connection string, but still not getting to connect with .sdf file
SqlCeConnection con = new SqlCeConnection(connstring2); // getting error as "format of initialization string does not conform at index 0"
Posted
Updated 8-Jul-16 3:36am
Comments
[no name] 8-Jul-16 9:34am    
I think you are missing provider section of the connectionstring.
Member 11905879 8-Jul-16 9:40am    
sorry my mistake that i didn't mention about the providername. i actually set providerName = System.Data.SqlServerCe

1 solution

You don't need the "@" at the front, so leave that bit out. The @ isn't part of the string, it is an instruction to the compiler to treat all characters as literal. It is used when dealing with paths so you don't have to escape the "\" characters.

C#
SqlCeConnection con = new SqlCeConnection(connstring1);
 
Share this answer
 
v2
Comments
Member 11905879 8-Jul-16 9:42am    
still receiving the same error except, now the problem is at index 104 instead of index 0
F-ES Sitecore 8-Jul-16 9:45am    
We don't know what you're putting in your textbox, so you'll just have to compare the working and non-working strings, there has to be a difference somewhere.

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