Aquilla
Well-Known Member
I am quite new with C#, i have decided to go from PHP to Software Development and has thus far been studying a whole lot of books and tutorials. But this code down here is giving me trouble, please note this is a very basic operation I'm trying to just understand how things work...
Any help would be appreciated.
The following Code does nothing, it does not INSERT a new row into the database. I get the SELECT procedure to work but not this? Any Ideas?
Thanks
Any help would be appreciated.
The following Code does nothing, it does not INSERT a new row into the database. I get the SELECT procedure to work but not this? Any Ideas?
Thanks
Code:
private void button1_Click(object sender, EventArgs e)
{
string FirstName;
string LastName;
string Country;
FirstName = Convert.ToString(textBox1.Text);
LastName = Convert.ToString(textBox2.Text);
Country = Convert.ToString(textBox3.Text);
//GREATE CONNECTION TO DATABASE
SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLExpress;AttachDbFilename=|DataDirectory|\\DataBaseAccessDb.mdf;Integrated Security=True;User Instance=True;");
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "INSERT INTO Users (FirstName, LastName, Country) VALUES (@FirstName, @LastName, @Country)";
myCommand.Parameters.AddWithValue("@FirstName", FirstName);
myCommand.Parameters.AddWithValue("@LastName", LastName);
myCommand.Parameters.AddWithValue("@Country", Country);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
richTextBox1.Text = "There was an Error: " + Convert.ToString(ex);
throw;
}
myConnection.Close();
}