Resolved SQL Database issue / sqlConnection

Ivo

Member
Joined
Feb 8, 2022
Messages
18
Programming Experience
Beginner
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CRUD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
           string query = "INSERT INTO Employee(Employeeid,EmployeeName,EmployeeContact,EmployeeGender,EmployeeBirthDate)" +
                " VALUES(@name,@contact,@gender,@birthDate)";
            SqlConnection con = new SqlConnection("Data Source=.\sqlexpress;Initial Catalog=samplesdb;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@name", txtName.Text);
            cmd.Parameters.AddWithValue("@contact", txtContact.Text);
            cmd.Parameters.AddWithValue("@gender", cmbGender.Text);
            cmd.Parameters.AddWithValue("@birthdate", dtBirthDate.Value);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Workflow correct added to the database");
        }
    }
}
 

Attachments

  • Schermafbeelding 2022-04-14 201631.png
    Schermafbeelding 2022-04-14 201631.png
    130.3 KB · Views: 12
  • Schermafbeelding 2022-04-14 201704.png
    Schermafbeelding 2022-04-14 201704.png
    15.3 KB · Views: 14
What question do you have?

Anyway, if you look closely on line 25, you need to escape that backslash, otherwise the computer sees '\s' instead of '\' which is what you intended.
 
Back
Top Bottom