0

Im getting the following error:

"A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Incorrect syntax near ')'. If there is a handler for this exception, the program may be safely continued."

Is this syntax? i really cant figure this out, can anyone tell what i am doing wrong?

        private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("A Atualizar dados...");
        bool check = true;
        do
        {
            string connectionString = @"Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";
            string queryString = string.Empty;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                queryString = "UPDATE  wgcdoccab SET merc1 = merc1/2 WHERE numdoc = (SELECT MAX(numdoc) FROM WGCDOCCAB WHERE serie ='1' and tipodoc ='FSS' and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())), 120))";
                SqlCommand command = new SqlCommand(queryString, connection);
                command.ExecuteNonQuery();
                connection.Close();
            }
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                queryString = "SELECT max(numdoc) FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS' and contribuinte ='999999990' and datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())), 120))";
                using (SqlCommand command = new SqlCommand(queryString, connection))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {

                        check = true;

                    }
                    else
                    {
                        check = false;
                        MessageBox.Show("Dados atualizados com sucesso");
                    }
                    command.Connection.Close();
                }
            }
        }
        while (check);
5
  • well, which of the queries is throwing that error? probably both, since you have too many ) at the end of them... Commented Jul 23, 2015 at 19:29
  • Start to run your queries part by part. Start small parts and go bigger ones. I'm sure you will find your extra/unnecessary ( or ) in your code. Commented Jul 23, 2015 at 19:30
  • Visual Studio is highlighting the SELECT query. I'm not sure, the code seem to be right. Commented Jul 23, 2015 at 19:30
  • why do you have a boatload of ) next to getdate. How about using curdate() or something that exists ? count your opening and closing ( ) Commented Jul 23, 2015 at 19:31
  • Looks like you have an extra ')' at the end of the select statement. Commented Jul 23, 2015 at 20:32

1 Answer 1

1

Check your SQL Statements. This line has too many parentheses:

queryString = "SELECT max(numdoc) FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS' and contribuinte ='999999990' and datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())), 120))";
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was the problem, i ended up checking the lines and parentheses one by one. But still thank you for the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.