in line 20, your sql statement has a trailing ")" which is invalid syntax as suggested by the error message
'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Dejvi Mehdi
21
Reputation points
//Here is my code
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;
namespace Projekt
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Asus\Documents\DataTest12.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into [Table] values (@ID,@Name,@Age)", con);
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@Name", (textBox2.Text));
cmd.Parameters.AddWithValue("@Age", double.Parse(textBox3.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Succesfully Saved");
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Asus\Documents\DataTest12.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand($"Update [Table] set Name=@Name, Age= @Age where ID = @ID)", con);
cmd.Parameters.AddWithValue("@ID", int.Parse(textBox1.Text));
cmd.Parameters.AddWithValue("@Name", (textBox2.Text));
cmd.Parameters.AddWithValue("@Age", double.Parse(textBox3.Text));
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Succesfully Updated");
}
}
}
Additional information: Incorrect syntax near ')'.
The code stuck on cmd.ExecuteNonQuery();