Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello I want to show the number of datasets after klicking a button. But if i click the Button the Program crashes. The error message is: The ConnectionString property was not initialized

My code:

private void button2_Click_1(object sender, EventArgs e)
        {
            this.timer1.Start();

            SqlCeConnection conn;
            SqlCeCommand cmd;
            SqlCeDataReader rdr;
                
                conn = new SqlCeConnection();
                cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
                conn.Open();
                MessageBox.Show(cmd.ExecuteScalar().ToString());


What I have tried:

I looked up in the internet but I couldn't find any solution.
Posted
Updated 19-Jul-18 22:41pm

1 solution

Well, yes - it will. Look at the code:
conn = new SqlCeConnection();
cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
conn.Open();
You have created a connection object, but you don't tell it what you want it to connect to! SqlCE is a file-based database: you have to at the very minimum specify the file you want to use and provide that info to your SqlCEConnection instance.
SQL Server Compact connection strings - ConnectionStrings.com[^]
string strConnect = @"Data Source=MyFolder\MyData.sdf;Persist Security Info=False;";
conn = new SqlCeConnection(strConnect);
cmd = new SqlCeCommand("SELECT COUNT(*) FROM Pigeons", conn);
conn.Open();
 
Share this answer
 
Comments
Member 13917889 20-Jul-18 4:45am    
Thanks for your reply i will try!
OriginalGriff 20-Jul-18 4:53am    
You're welcome!

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