I'm recently new at C# and follow a tutorial on connection to the database, the connection for viewing or SELECT command was ok, I made some revision based on the threads of forums to make my code better, but when I was in INSERT for my SQL Server, I have some problems. I was able to insert the data but when I re-run the program the data that I just inserted earlier was not in the database.
Here is my code on insert
string commandText = "INSERT INTO loginInfo (name, pass, role) VALUES(@name, @pass, @role)";
using (connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(commandText, connection);
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@name", textBox1.Text);
command.Parameters.AddWithValue("@pass", textBox2.Text);
command.Parameters.AddWithValue("@role", textBox3.Text);
try
{
command.Connection.Open();
command.ExecuteNonQuery();
}
catch
{
}
finally
{
command.Connection.Close();
}
}
I'm trying to create a simple CRUD but not save the data... please help
ExecuteNonQuery?usingwill close the connection, you don't have to use thatfinallyblock. The entiretry/catchblock you used simply hides the error, nothing moreAttachDbFileName=clause in there which is known to cause trouble