Sunday, 13 August 2017

c# - Can't save a record twice, because of this error: Connection property has not been initialized



I'm working on a program that inserts and deletes data into a database. The database only have one table with four columns (nombreEmpleados, apellidosEmpleados, departamento and carnet). The program has 4 buttons, save, delete, show and close, the problem is that when I click show or save the first time, the program works, but when I do it again I get this error: The connectionstring property has not been initialized.



EDIT
*This is code from the begining to the save button: *




namespace sampleAdonetWinforms
{

public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection("Data Source=MAINPC;Initial Catalog=Ambar;Integrated Security=True");

public Form1()
{
InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
this.Close();

}

private void button2_Click(object sender, EventArgs e)
{
formBorrar ventanaBorrar = new formBorrar();
ventanaBorrar.ShowDialog();
}

private void button1_Click(object sender, EventArgs e)
{

string query = "INSERT INTO empleados VALUES (@Nombre, @Apellidos, @Departamento, @Carnet)";
SqlCommand vCom = new SqlCommand(query, conn);

int carnetID = int.Parse(tbCarnet.Text);

try
{
vCom.Parameters.AddWithValue("@Nombre", tbNombre.Text);
vCom.Parameters.AddWithValue("@Apellidos", tbApellidos.Text);
vCom.Parameters.AddWithValue("@Departamento", tbDepartamento.Text);

vCom.Parameters.AddWithValue("@Carnet", carnetID);
tbNombre.Focus();
conn.Open();
vCom.ExecuteNonQuery();
vCom.Dispose();
MessageBox.Show("Data stored");

}
catch (Exception ex)
{

MessageBox.Show("Error " + ex.Message);
}

}

Answer



As I have said in my comment, the global SqlConnection object could be the origin of your problems.
From your code is not evident, but something, at the second passage doesn't work as expected and the global connection object is not correctly initialized.
Keeping a global instance of a connection object is a bad practice because you risk to loose control on the lifetime of that instance and precious unmanaged resources are not released to the system.
To cure this kind of problem there is the Connection Pooling infrastructure.
The Connection Pooling helps to implement a coding pattern where the coder should release as soon as possible the connection.



So I suggest to change your code to take advantage of this pattern




private void button1_Click(object sender, EventArgs e)
{
// INITIALIZE
using(SqlConnection conn = new SqlConnection("......"))
{
// OPEN
conn.Open();
string query = "INSERT INTO empleados VALUES (@Nombre, @Apellidos, @Departamento, @Carnet)";
// USE
using(SqlCommand vCom = new SqlCommand(query, conn))

{
....
}
} // <- CLOSE & DISPOSE
}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...