Share via

System.Data.SqlClient.SqlException: incorrect syntax near " abc ".

Pankaj Seervi 20 Reputation points
2023-11-20T11:01:20.45+00:00

here is my code please give me suggestion what to do next

error line is ==> "@usercountry"

its telling incorrect syntax near "@usercountry"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using signup_login.DatabaseProject;

namespace signup_login
{

    public partial class SignUp : Form
    {
        DBAccess access = new DBAccess();
        public SignUp()
        {
            InitializeComponent();
        }

        private void btnsignup_Click(object sender, EventArgs e)
        {
            String username = txtname.Text;
            String useremail = txtmail.Text;
            String userpassword = txtpassword.Text;
            String usercountry = cbcountry.Text;
            if (username.Equals(""))
            {
                MessageBox.Show("pls enter your name.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (useremail.Equals(""))
            {
                MessageBox.Show("pls enter your mail.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (userpassword.Equals(""))
            {
                MessageBox.Show("pls enter your password.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if (usercountry.Equals(""))
            {
                MessageBox.Show("pls select your country.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                SqlCommand insertcommand = new SqlCommand("INSERT INTO dbo.users(NAME, EMAIL, PASSWORD, COUNTRY) VALUES (@username , @useremail , @userpassword , @usercountry");

                //BELOW LINES ARE TO SECURE THE DATA FORM THIRD PARTY APPS 

                insertcommand.Parameters.AddWithValue("@username", username);
                insertcommand.Parameters.AddWithValue("@useremail", useremail);
                insertcommand.Parameters.AddWithValue("@userpassword", userpassword);
                insertcommand.Parameters.AddWithValue("@usercountry", usercountry);
                if (access.executeQuery(insertcommand) == 1)
                {
                    MessageBox.Show("the signup is successfully done", "CONFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    Homepage home = new Homepage();
                    home.Show();
                }
                else
                {
                    MessageBox.Show("signup faild please try again", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

AI answer

2023-11-20T11:01:20.5733333+00:00

The issue with your code is that you are missing a closing parenthesis in your SQL INSERT statement. The correct statement should be:

SqlCommand insertcommand = new SqlCommand("INSERT INTO dbo.users(NAME, EMAIL, PASSWORD, COUNTRY) VALUES (@username , @useremail , @userpassword , @usercountry)");

Notice the closing parenthesis at the end of the statement.

References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.