0
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;
using System.Data.SqlClient;

namespace StudentDataBase
{
    public partial class Form1 : Form
    {         
        SqlConnection connection = new SqlConnection(Data Source =.; Initial Catalog = Students; Integrated Security = True); //first line that gets errors
    
        public Form1()
        {
            InitializeComponent();
            txtboxFirstName.Focus();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if(txtboxFirstName.Text.Length == 0 && txtboxLastName.Text.Length == 0 && txtboxYear.Text.Length == 0)
            {
                txtboxFirstName.Clear();
                txtboxLastName.Clear();
                txtboxYear.Clear();
                cmbboxDegree.SelectedIndex = -1;
                txtboxFirstName.Focus();
            }
            else
            {
                DialogResult dialogResult = MessageBox.Show("Everything you introduced will be deleted forever. Do you continue?", "WARNING", MessageBoxButtons.YesNo);

                if (dialogResult == DialogResult.Yes)
                {
                    txtboxFirstName.Clear();
                    txtboxLastName.Clear();
                    txtboxYear.Clear();
                    cmbboxDegree.SelectedIndex = -1;
                    txtboxFirstName.Focus();
                }
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            connection.Open();
            SqlCommand command = new SqlCommand("INSERT INTO Info ( First_Name, Last_Name, Degree, Year ) VALUES ('" + txtboxFirstName.Text + '","' + txtboxLastName.Text + '","' + cmbboxDegree.Text + '","' + txtboxYear.Text + '")", connection); //second line that gets errors
    
                connection.Close();
        }
    }
}

On this line

SqlConnection connection = new SqlConnection(Data Source =.; Initial Catalog = Students; Integrated Security = True);

I get this errors:

Data/Source/Students/True does not exist in the current context

The type or namespace name 'Initial/'Integrated' could not be found

Syntax error ',' expected

Invalid expression term '.'

) expected

On this line

SqlCommand command = new SqlCommand("INSERT INTO Info ( First_Name, Last_Name, Degree, Year ) VALUES ('" + txtboxFirstName.Text + '","' + txtboxLastName.Text + '","' + cmbboxDegree.Text + '","' + txtboxYear.Text + '")", connection); 

I get these errors:

Newline in constant

Too many characters in characters literal

Syntax error ',' expected

I think on the last line the errors are connected to the fact that ",connection); shows up highlighted like a quote, but I haven't found out why, and I couldn't fix it without still getting errors.

It's probably a really stupid mistake but I'm a beginner and I really can't see it.

Edit: The first line error is no more, i just had to put " " around the argument. Thanks a lot!

Unfortunately I still have the same errors with the second one.

2
  • 3
    The Sql connection argument is a string. Just add " " around whatever you have there right now. Commented Feb 20, 2021 at 20:47
  • SQL Injection alert - you should not concatenate together your SQL statements - use parametrized queries instead to avoid SQL injection - check out Little Bobby Tables Commented Feb 20, 2021 at 21:18

2 Answers 2

2

You're missing double quotes:

new SqlConnection("Data Source =.; Initial Catalog = Students; Integrated Security = True")
Sign up to request clarification or add additional context in comments.

Comments

1

Try to put the connection string in quotes, e.g. SqlConnection("DataSource=...IntegratedSecurity=True");

Comments

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.