I'm trying to insert into a database table from visual studio but I'm getting the same error and I don't know what could it be.
System.Data.SqlClient.SqlException: 'Invalid column name'
This is my code, I made 2 classes, Gateway, Dept and the Form1:
namespace insertar
{
class Dept
{
public string Dept_No { get; set; }
public string DNombre { get; set; }
public string Loc { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace insertar
{
class Gateway
{
public bool Save(Dept dept)
{
Form1 form = new Form1();
string connectionString = @"Data Source=DESKTOP-IE39262;Initial Catalog=Hospital;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectionString);
//connection.Open();
/*string query = @"INSERT INTO Dept VALUES (" + dept.Dept_No + "," + dept.DNombre +
"," + dept.Loc + ")";*/
SqlCommand command = new SqlCommand("INSERT INTO Dept(Dept_No, DNombre, Loc)" + "VALUES (" + dept.Dept_No + "," + dept.DNombre +
"," + dept.Loc + ")", connection);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
return true;
}
}
}
private void guardarbtn_Click(object sender, EventArgs e)
{
Dept dept = new Dept();
dept.Dept_No = dept_no.Text;
dept.DNombre = dnombre.Text;
dept.Loc = loc.Text;
Gateway gateaway = new Gateway(); //class gateway
gateaway.Save(dept);
MessageBox.Show("Departamento insertado exitosamente");
dept_no.Text = "";
dnombre.Text = "";
loc.Text = "";
}
SqlParameter.