Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
i get this error Expected catch or finally in this code :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace movies
{
    public partial class Form1 : Form
    {

        
        string SqlStr;
        SqlCommand SqlCmd;
        SqlDataAdapter SqlDa;
        SqlDataReader SqlDr;


        public Form1()        
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection CN = new SqlConnection("Data Source=(local);Initial Catalog=Movies;Integrated Security=True");

            try 
            
            {
                CN.Open();

                SqlStr = "Insert into Student( [Name]";
                SqlStr = SqlStr + " ,[Director]";
                SqlStr = SqlStr + " ,[Year]";
                SqlStr = SqlStr + " ,[Type]";
                SqlStr = SqlStr + " Values('" + TxtName.Text + "','" + TxtDirector.Text + "',";
                SqlStr = SqlStr + "'" + TxtYear.Text + "'  ,";
                SqlStr = SqlStr + "'" + TxtType.Text + "',";
                SqlCmd = new SqlCommand(SqlStr, CN);
                SqlCmd.ExecuteNonQuery();

                MessageBox.Show("successfully added.");
                Clear();
        }

        
            {
            SqlConnection CN = new SqlConnection("Data Source=(local);Initial Catalog=school;Integrated Security=True");
            try
            {
                CN.Open();
          
                SqlStr = "   select Name,Director,Year,Type ";
                SqlStr = SqlStr +" from Movies" ;
                SqlStr = SqlStr + " where Id_Movies='"+lab4.Text+"'  ";

                SqlCmd = new SqlCommand(SqlStr, CN);
                SqlDr = SqlCmd.ExecuteReader();
              
                SqlDr.Read();

                if (!SqlDr.HasRows)
                {
                    MessageBox.Show("found nothing.");
                }
                else
                {
                    TxtName.Text = SqlDr["Name"].ToString();
                    TxtDirector.Text = SqlDr["Director"].ToString();
                    TxtYear.Text = SqlDr["Year"].ToString();
                    TxtType.Text = SqlDr["Type"].ToString();
      
                }

            }

            finally
            {
                CN.Close();
            }

        }

        {
            SqlConnection CN = new SqlConnection("Data Source=(local);Initial Catalog=school;Integrated Security=True");

            try
            {
                CN.Open();

                SqlStr = "Update  Student set ";
                SqlStr = SqlStr + " Name='"+TxtName.Text+"' , ";
                SqlStr = SqlStr + " Director='" +  TxtDirector.Text + "' , ";
                SqlStr = SqlStr + " Year='" +  TxtYear.Text + "' , ";
                SqlStr = SqlStr + " Type='" +  TxtType. Text + "' , ";
                SqlStr = SqlStr + " where Id_Movies='" + TxtID.Text + "'  ";

                SqlCmd = new SqlCommand(SqlStr, CN);
                SqlCmd.ExecuteNonQuery();

                MessageBox.Show("Edited successfully.");
                Clear();


            }
            finally
            {
                CN.Close();
            }
        }

        {
        
        }
        }
        }
        }

how to fix it ?
Posted
Updated 22-Jul-15 4:54am
v2
Comments
PIEBALDconsult 22-Jul-15 10:54am    
Format your code consistently and you will likely find that you have a BRACE out of place.

Quote:
SqlConnection CN = new SqlConnection("Data Source=(local);Initial Catalog=Movies;Integrated Security=True");

try

{
CN.Open();

SqlStr = "Insert into Student( [Name]";
SqlStr = SqlStr + " ,[Director]";
SqlStr = SqlStr + " ,[Year]";
SqlStr = SqlStr + " ,[Type]";
SqlStr = SqlStr + " Values('" + TxtName.Text + "','" + TxtDirector.Text + "',";
SqlStr = SqlStr + "'" + TxtYear.Text + "' ,";
SqlStr = SqlStr + "'" + TxtType.Text + "',";
SqlCmd = new SqlCommand(SqlStr, CN);
SqlCmd.ExecuteNonQuery();

MessageBox.Show("successfully added.");
Clear();
}

In the above code the catch (or finally) is missing.
 
Share this answer
 
v2
Comments
brandon1999 22-Jul-15 11:05am    
i copy and paste it into C# but i get error alot.
Sergey Alexandrovich Kryukov 22-Jul-15 11:15am    
And?!

Programming is not done by copy and paste; you need to understand all you are doing.

—SA
PIEBALDconsult 22-Jul-15 11:16am    
And you will continue to have trouble until you learn.
CPallini 22-Jul-15 12:54pm    
[OT] Thank you for fixing it.
Sergey Alexandrovich Kryukov 22-Jul-15 11:14am    
5ed.
—SA
You have a try with no catch or finally. It's right after Clear();, you have a closing brace and then an opening one.
 
Share this answer
 
Comments
brandon1999 22-Jul-15 10:59am    
i don't underestand. can u get me the right code ? please
ZurdoDev 22-Jul-15 11:02am    
try should have an opening brace { and a closing brace }. Immediately after you should have either a catch or a finally. Your first try does not have a catch or finally after it.
brandon1999 22-Jul-15 11:12am    
english is my second language and i don't know it well - please fix the code for me. thank you.
PIEBALDconsult 22-Jul-15 11:16am    
No. We don't do that here. Call your mommy.
brandon1999 22-Jul-15 11:19am    
ok just tell me again what should i do ? and what is this error?
Just to add to what RyanDev correctly identified: If you get a problem like this, put your cursor on the open curly bracket after the try and press CTRL+] - it will move to the "Matching" bracket (even if they are wrong) so you can see where the compiler thinks it is.
 
Share this answer
 
One more thing you need to fix: Always use parameters in your SQL statements instead of concatenating text directly to you SQL text. This helps you to handle different data type conversions etc but most importantly, it keeps you safe from SQL injections

For example your update currently looks like
C#
CN.Open();

SqlStr = "Update  Student set ";
SqlStr = SqlStr + " Name='"+TxtName.Text+"' , ";
SqlStr = SqlStr + " Director='" +  TxtDirector.Text + "' , ";
SqlStr = SqlStr + " Year='" +  TxtYear.Text + "' , ";
SqlStr = SqlStr + " Type='" +  TxtType. Text + "' , ";
SqlStr = SqlStr + " where Id_Movies='" + TxtID.Text + "'  ";

SqlCmd = new SqlCommand(SqlStr, CN);
SqlCmd.ExecuteNonQuery();

MessageBox.Show("Edited successfully.");
Clear();

As said you should use parameters so the code should be something like
C#
CN.Open();

SqlStr = @"UPDATE Student 
SET Name     = @name,
    Director = @director,
    Year     = @year,
    Type     = @type,
WHERE Id_Movies = @id";

SqlCmd = new SqlCommand(SqlStr, CN);
SqlCmd.Parameters.AddWithValue("@name", TxtName.Text);
SqlCmd.Parameters.AddWithValue("@director", TxtDirector.Text);
SqlCmd.Parameters.AddWithValue("@year", TxtYear.Text);
SqlCmd.Parameters.AddWithValue("@type", TxtType. Text);
SqlCmd.Parameters.AddWithValue("@id", TxtID.Text);
SqlCmd.ExecuteNonQuery();

MessageBox.Show("Edited successfully.");
Clear();

Of course it would make sense to first check that the inputs in the text boxes are valid. FOr example TxtID being numeric if that is the type of the field and so on.

For more information about parameters, see SqlParameter[^]
 
Share this answer
 
Comments
brandon1999 22-Jul-15 13:28pm    
ok , i change it . thank you.
This compiles just fine, however the "Clean()" method isnt included in your code so I just commented it out.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace movies
{
    public partial class Form1 : Form
    {
        string SqlStr;
        SqlCommand SqlCmd;
        SqlDataAdapter SqlDa;
        SqlDataReader SqlDr;
        SqlConnection CN = new SqlConnection("Data Source=(local);Initial Catalog=Movies;Integrated Security=True");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                CN.Open();

                SqlStr = "Insert into Student( [Name]";
                SqlStr = SqlStr + " ,[Director]";
                SqlStr = SqlStr + " ,[Year]";
                SqlStr = SqlStr + " ,[Type]";
                SqlStr = SqlStr + " Values('" + TxtName.Text + "','" + TxtDirector.Text + "',";
                SqlStr = SqlStr + "'" + TxtYear.Text + "'  ,";
                SqlStr = SqlStr + "'" + TxtType.Text + "',";
                SqlCmd = new SqlCommand(SqlStr, CN);
                SqlCmd.ExecuteNonQuery();

                MessageBox.Show("successfully added.");
                // Clear(); Not sure what this is?
            }
            catch (Exception) { }

            {
                try
                {
                    CN.Open();

                    SqlStr = "   select Name,Director,Year,Type ";
                    SqlStr = SqlStr + " from Movies";
                    SqlStr = SqlStr + " where Id_Movies='" + lab4.Text + "'  ";

                    SqlCmd = new SqlCommand(SqlStr, CN);
                    SqlDr = SqlCmd.ExecuteReader();

                    SqlDr.Read();

                    if (!SqlDr.HasRows)
                    {
                        MessageBox.Show("found nothing.");
                    }
                    else
                    {
                        TxtName.Text = SqlDr["Name"].ToString();
                        TxtDirector.Text = SqlDr["Director"].ToString();
                        TxtYear.Text = SqlDr["Year"].ToString();
                        TxtType.Text = SqlDr["Type"].ToString();

                    }

                }

                finally
                {
                    CN.Close();
                }
            }
            {
                try
                {

                    CN.Open();

                    SqlStr = "Update  Student set ";
                    SqlStr = SqlStr + " Name='" + TxtName.Text + "' , ";
                    SqlStr = SqlStr + " Director='" + TxtDirector.Text + "' , ";
                    SqlStr = SqlStr + " Year='" + TxtYear.Text + "' , ";
                    SqlStr = SqlStr + " Type='" + TxtType.Text + "' , ";
                    SqlStr = SqlStr + " where Id_Movies='" + TxtID.Text + "'  ";

                    SqlCmd = new SqlCommand(SqlStr, CN);
                    SqlCmd.ExecuteNonQuery();

                    MessageBox.Show("Edited successfully.");
                    // Clear(); Not sure what this is?	 
                }
                finally
                {
                    CN.Close();
                }
            }
        }
    }
}
 
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