A.N.
A.N.

Reputation: 1

Errors when adding SQL Server database to C#

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.

Upvotes: 0

Views: 97

Answers (2)

cbrox
cbrox

Reputation: 21

You're missing double quotes:

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

Upvotes: 2

endofdata
endofdata

Reputation: 11

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

Upvotes: 1

Related Questions