Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I am learning C#
and I am trying to create simple dictionary which will translate words from english to my native language.
I can't understand where i made mistake, i want to retrieve data from local database when user will press button "button1"
now it shows such message: Unhandled exception has occured in your application...
The following are my code of program:


C#
private void button1_Click(object sender, EventArgs e)
        {
            
                try
                {

                    SqlCeConnection con = new SqlCeConnection("c:\\users\\rustam\\documents\\visual studio 2012\\Projects\\Sozluk\\Sozluk\\dictionary.sdf");
                    SqlCeCommand com = new SqlCeCommand("Select soz, terjime, mysal from sozler where soz='" + soz1.Text + "'", con);
                    con.Open();

                    SqlCeDataReader reader = com.ExecuteReader();

                    if (reader.Read())
                    {
                        soz1.Text = reader.GetString(0);
                        terjime1.Text = reader.GetString(1);
                        mysal1.Text = reader.GetString(2);
                    }
                    else
                    {

                        MessageBox.Show("Invalid Word", "Dictionary", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }


                }
                catch (SqlCeException)
                {
                    MessageBox.Show("Error"); // Would be an improvement than just the word 'Error'
                }


           } 
Posted
Comments
Dave Kreskowiak 17-Jun-14 13:13pm    
...and the exception message would be .....? Hint: It's NOT 'An unhandled exception has occured...'
ZurdoDev 17-Jun-14 14:08pm    
1. You are exposing your database to sql injections.
2. Never just show "Error" when there is an error. Show the message on the exception.
3. What is the actual error?
4. What line of code causes the error?

Please refer to the following link that explains almost everything that you need.

http://technet.microsoft.com/en-us/library/aa226134(v=SQL.80).aspx[^]

I tried it in my machine and it worked.

I think there is a problem in the created table(sozler) or maybe in the connection string.
 
Share this answer
 
Comments
Rustam777 17-Jun-14 14:22pm    
Thank u.
I fixed my problem) but now i want to know how to include database for installer)?
for example if i want to install my app in another pc, it will not work because another pc doesnt have maybe installed sql server and even if installed, he doesnt have database which i created on my sqlServer. Do u know how to include database to installer?
1st question is the SQL database compact? Are you really sure you are suppose to be using SqlCeConnection rather than SqlConnection etc. Thats not causing the error by the way but if it's wrong it wont work anyhow :-)

Next you need to go back and read up again on string basics

Finally if you really do need to use SqlCeConnection then at least look at the Microsoft sample at the bottom of the help on the API call

HINT HERE SCROLL TO BOTTOM => http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection.connectionstring%28v=vs.100%29.aspx[^]
 
Share this answer
 
Comments
Rustam777 17-Jun-14 14:12pm    
Thank u leon de boer.
I fixed my problem) but now i want to know how to include database for installer)?
Rustam777 17-Jun-14 14:14pm    
for example if i want to install my app in another pc, it will not work because another pc doesnt have maybe installed sql server and even if installed, he doesnt have database which i created on my sqlServer. Do u know how to include database to installer?
phil.o 17-Jun-14 17:19pm    
Well, "dictionary.sdf" is pretty obvious on which database engine he needs, isn't it? :)
the following link is explaining how to deploy SQLServer Compact
http://msdn.microsoft.com/en-us/library/aa983326(v=vs.90).aspx[^]

and this one is explaining how to add your database in the deployment

http://msdn.microsoft.com/en-us/library/aa983340(v=vs.90).aspx[^]

Hope it solves the issue
 
Share this answer
 

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