System.ArgumentException: Invalid value for the 'integrated security' key. and other

  • Thread starter Thread starter I am iRSV
  • Start date Start date
I

I am iRSV

Guest
Hello,

i tried to make a thelephone book/ adddress book app with Visual Studio Basic 2019 with a Microsoft Sql Database (following a youtube tutorial) but i got this error "System.ArgumentException: Invalid value for the 'integrated security' key" (Class: rubrica.Class.cs line: 34). At the beginning the app approximately worked, it started but i couldn't add new contact, let me explain, when i try to add a new contact the app create a colunm but it doesn't write the information like (Firstname, lastname, ecc...). Can anyone help me to solve the exception and to understand why i can't add new contact?


CODE:

RUBRICA.cs

using Rubrica.rubricaClasses;
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;

namespace Rubrica
{
public partial class RUBRICA : Form
{
public RUBRICA()
{
InitializeComponent();
}
rubricaClass c = new rubricaClass();
private void PictureBox1_Click(object sender, EventArgs e)
{

}

private void TextBox1_TextChanged(object sender, EventArgs e)
{

}

private void Label1_Click(object sender, EventArgs e)
{

}

private void Label1_Click_1(object sender, EventArgs e)
{

}

private void TextBox3_TextChanged(object sender, EventArgs e)
{

}

private void LblSurname_Click(object sender, EventArgs e)
{

}

private void TextBox4_TextChanged(object sender, EventArgs e)
{

}

private void BtnClear_Click(object sender, EventArgs e)
{

}

private void BntUpdate_Click(object sender, EventArgs e)
{

}

private void BtnRemove_Click(object sender, EventArgs e)
{

}

private void BntAdd_Click(object sender, EventArgs e)
{
//Get the value from the imput fields
c.Nome = txtBoxName.Text;
c.Cognome = txtBoxSurname.Text;
c.Telefono1= txtBoxPhone1.Text;
c.Telefono = txtBoxPhone.Text;
c.Email = txtBoxEmail.Text;

//Inserting Data into Database uing the method we created is previous episode
bool success = c.Insert(c);
if (success == true)
{
//Successfully Inserted
MessageBox.Show("Nuovo contatto inserito corettamente!");
//Call the clear Method Here
Clear();
}
else
{
//Failed to add Contact
MessageBox.Show("ERRORE, contatto non aggiunto (controllare i campi obbligatori contrassegnati con l'asterisco *)");
}
//load Data on Data GRidview
DataTable dt = c.Select();
dgvRubrica.DataSource = dt;
}
public void Clear()
{
txtBoxName.Text = "";
txtBoxSurname.Text = "";
txtBoxPhone1.Text = "";
txtBoxPhone.Text = "";
txtBoxEmail.Text = "";
}
private void PictureBox2_Click(object sender, EventArgs e)
{

}

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

private void DgvRubrica_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Get Data from Data Grid view and load it to the textboxes respectively
//identify the row on which mouse is clicked
int rowIndex = e.RowIndex;
txtBoxID.Text = dgvRubrica.Rows[rowIndex].Cells[0].Value.ToString();
txtBoxName.Text = dgvRubrica.Rows[rowIndex].Cells[1].Value.ToString();
txtBoxSurname.Text = dgvRubrica.Rows[rowIndex].Cells[2].Value.ToString();
txtBoxPhone1.Text = dgvRubrica.Rows[rowIndex].Cells[3].Value.ToString();
txtBoxPhone.Text = dgvRubrica.Rows[rowIndex].Cells[4].Value.ToString();
txtBoxEmail.Text = dgvRubrica.Rows[rowIndex].Cells[5].Value.ToString();
}

private void RUBRICA_Load(object sender, EventArgs e)
{
DataTable dt = c.Select();
dgvRubrica.DataSource = dt;
}
}
}


-----


rubricaClass.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rubrica.rubricaClasses
{
class rubricaClass
{
//Getter Setter Properties
//Acts as Data Carrier in Our Application
public int ContactID { get; set; }
public string Nome { get; set; }

public string Cognome { get; set; }

public string Telefono1 { get; set; }

public string Telefono { get; set; }

public string Email { get; set; }

public static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

//Selectiong data from database

public DataTable Select()
{
//Setp 1: Database connection
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();
try
{
//Step 2: Writing SQL Query
string sql = "SELECT * FROM tbl_Rubrica";
//Creating cmd using sql and conn
SqlCommand cmd = new SqlCommand(sql, conn);
//creating SQL DataAdapter using cmd
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);

}
catch (Exception ex)
{

}
finally
{
conn.Close();
}
return dt;
}

public bool Insert (rubricaClass c)
{
bool isSuccess = false;

SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "INSERT INTO tbl_Rubrica (Nome, Cognome, Telefono1, Telefono, Email) VALUES (@Nome, @Cognome, @Telefono1, @Telefono, @Email)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Nome", c.Nome);
cmd.Parameters.AddWithValue("@Cognome", c.Cognome);
cmd.Parameters.AddWithValue("@Telefono1", c.Telefono1);
cmd.Parameters.AddWithValue("@Telefono", c.Telefono);
cmd.Parameters.AddWithValue("@Email", c.Email);

conn.Open();
int rows = cmd.ExecuteNonQuery();

if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{

}
finally
{
conn.Close();
}
return isSuccess;

}
public bool Update (rubricaClass c)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "UPDATE tbl_Rubrica SET Nome=@Nome, Cognome=@Cognome, Telefono1=@Telefono1, Telefono=@Telefono, Email=@Email WHERE ContactID=@ContactID";

SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Nome", c.Nome);
cmd.Parameters.AddWithValue("@Cognome", c.Cognome);
cmd.Parameters.AddWithValue("@Telefono1", c.Telefono1);
cmd.Parameters.AddWithValue("@Telefono", c.Telefono);
cmd.Parameters.AddWithValue("@Email", c.Email);
cmd.Parameters.AddWithValue("ContactID", c.ContactID);

conn.Open();

int rows = cmd.ExecuteNonQuery();

if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}

}
catch (Exception ex)
{

}
finally
{
conn.Close();
}

return isSuccess;
}

public bool Delete (rubricaClass c)
{
bool isSuccess = false;

SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "DELETE FROM tbl_Rubrica WHERE ContactID=@ContactID";

SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@ContactID", c.ContactID);
conn.Open();

int rows = cmd.ExecuteNonQuery();

if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{

}
finally
{
conn.Close();
}

return isSuccess;
}
}
}


----

App.Config


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>

<connectionStrings>
<add name="connstrng" connectionString="Data Source=DESKTOP-JSE6I0L\ADRIANSQL;Initial Catalog=Rubrica;Integrated Security=Truee;"/>
</connectionStrings>
</configuration>




telephone book, address book n
telephone book, address book n
telephone book, address book n
telephone book, address book n
telephone book, address book n
telephone book, address book n
telephone book, address book n

Continue reading...
 
Back
Top