C
C#•12mo ago
Akama Aka

Incorrect syntax near '@address_id'

Hello, I'm currently working on a School project and I want to insert something into the Database but I get everytime a error that says that a incorrect syntax was given that doesnt really makes any sense for me. Code:
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\....\\Database.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand insertPatient = new SqlCommand("INSERT INTO patients (phone, firstname, lastname, address_ID) VALUES (@phoneNumb, @firstname, @lastname, @address_ID", con);
insertPatient.Parameters.AddWithValue("@phoneNumb", this.phoneTextBox.Text);
insertPatient.Parameters.AddWithValue("@firstname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@lastname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@address_ID", this.comboBox1.SelectedIndex.ToString());
Console.WriteLine(this.comboBox1.SelectedIndex);
int i = insertPatient.ExecuteNonQuery();

con.Close(); SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\...Database.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand insertPatient = new SqlCommand("INSERT INTO patients (phone, firstname, lastname, address_ID) VALUES (@phoneNumb, @firstname, @lastname, @address_ID", con);
insertPatient.Parameters.AddWithValue("@phoneNumb", this.phoneTextBox.Text);
insertPatient.Parameters.AddWithValue("@firstname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@lastname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@address_ID", this.comboBox1.SelectedIndex.ToString());
Console.WriteLine(this.comboBox1.SelectedIndex);
int i = insertPatient.ExecuteNonQuery();

con.Close();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\....\\Database.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand insertPatient = new SqlCommand("INSERT INTO patients (phone, firstname, lastname, address_ID) VALUES (@phoneNumb, @firstname, @lastname, @address_ID", con);
insertPatient.Parameters.AddWithValue("@phoneNumb", this.phoneTextBox.Text);
insertPatient.Parameters.AddWithValue("@firstname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@lastname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@address_ID", this.comboBox1.SelectedIndex.ToString());
Console.WriteLine(this.comboBox1.SelectedIndex);
int i = insertPatient.ExecuteNonQuery();

con.Close(); SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\...Database.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand insertPatient = new SqlCommand("INSERT INTO patients (phone, firstname, lastname, address_ID) VALUES (@phoneNumb, @firstname, @lastname, @address_ID", con);
insertPatient.Parameters.AddWithValue("@phoneNumb", this.phoneTextBox.Text);
insertPatient.Parameters.AddWithValue("@firstname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@lastname", this.firstNameBox.Text);
insertPatient.Parameters.AddWithValue("@address_ID", this.comboBox1.SelectedIndex.ToString());
Console.WriteLine(this.comboBox1.SelectedIndex);
int i = insertPatient.ExecuteNonQuery();

con.Close();
7 Replies
Cin
Cin•12mo ago
you seem to be missing a ) should be:
VALUES (@phoneNumb, @firstname, @lastname, @address_ID)
VALUES (@phoneNumb, @firstname, @lastname, @address_ID)
Akama Aka
Akama AkaOP•12mo ago
I looked on the code for 2h tried to change it, tried Google Bard and ChatGPT and all of them didnt founf it shyyDead guess im a AI too <:BL_Dead:1042748662922621008>
Cin
Cin•12mo ago
Did it fix the issue?
Akama Aka
Akama AkaOP•12mo ago
Yes loveHeartHug2
Cin
Cin•12mo ago
🥳
Akama Aka
Akama AkaOP•12mo ago
After 2h searching <:0033neko_sweating:938055567845519371>
Cin
Cin•12mo ago
We've all done it
Want results from more Discord servers?
Add your server