0

I'm creating a window form that displays a Calendar Event.

And when I add an event on specific date, I get this error:

System.Data.SqlClient.SqlException: 'Incorrect syntax near 'value'

at the line cmd.ExecuteNonQuery();.

This is my code:

namespace PRN_Project
{
    public partial class EventForm : Form
    {
        String ConnectionString = "server=DESKTOP-7NUQVBN; database=Calendar; uid=sa; pwd=123";

        public EventForm()
        {
            InitializeComponent();
        }

        private void EventForm_Load(object sender, EventArgs e)
        {
            tbDate.Text = UserControlDays.static_day + "/" + CalendarForm.static_month + "/" + CalendarForm.static_year;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            conn.Open();
            String sql = "INSERT INTO CalendarEvent (TimeDate, EventName) VALUE (?, ?)";

            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("TimeDate", tbDate.Text);
            cmd.Parameters.AddWithValue("EventName", tbEvent.Text);

            cmd.ExecuteNonQuery();

            MessageBox.Show("Saved!");

            cmd.Dispose();
            conn.Close();
        }
    }
}

I am using SQL Server authentication.

The datatype for TimeDate and EventName is varchar(255).

3
  • 2
    VALUES not VALUE. INSERT INTO TABLE_NAME VALUES (COLUMN_1, COLUMN2) Commented Nov 7, 2021 at 4:51
  • 2
    VALUES not VALUE. And you should ideally use named parameters INSERT INTO CalendarEvent(TimeDate, EventName) values (@TimeDate, EventName). Also note: AddWithValue is Evil, specify parameter types and lengths explicitly. And you need using blocks on the connection and command objects Commented Nov 7, 2021 at 4:55
  • @Charlieface can you explain specific for me please, i m new to this Commented Nov 7, 2021 at 5:05

1 Answer 1

1

try this one:

private void btSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            String sql = "INSERT INTO CalendarEvent(TimeDate, EventName)values(@TimeDate,@EventName)";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("@TimeDate", tbDate.Text);
            cmd.Parameters.AddWithValue("@EventName", tbEvent.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Saved !!!");
            cmd.Dispose();
            conn.Close();
        }

and you can use TRY Catch Finally

private void btSave_Click(object sender, EventArgs e)
            {
                SqlConnection conn = new SqlConnection(ConnectionString);
                String sql = "INSERT INTO CalendarEvent(TimeDate, EventName)values(@TimeDate,@EventName)";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@TimeDate", tbDate.Text);
                cmd.Parameters.AddWithValue("@EventName", tbEvent.Text);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Saved !!!");
            }
            catch
            {
            }
            finally
            {
                conn.Close();
            }
                
                
                
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of the try..catch--finally, just use using (SqlConnection conn = new SqlConnection(ConnectionString)) { ... }
@Klaus Gütter thanks. and your way is another good way and good idea.

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.