Hello guys,
I'm trying to learn asp.net mvc 3, but am having real nightmare with database queries :( I added a new sql server database in my project under app _data folder and created this little table where account_id is primary key and identity column. I added new ADO.net entity and have this table as a class. I run simple view where i have 2 textboxes and a submit button and where i enter values for username and password.
account_id int
username char(50)
password char(50)
image varchar(MAX)
And this is my code in the controller. The variables UsrName and Password are having the correct values from the textboxes. The connection is opened, but after the execution is finished and I check table data in the connection server tab, there's no new value. What am I missing here ? Thanks in advance !
[HttpPost]
public ActionResult Index(TBL_ACCOUNTS modl)
{
string UsrName = modl.username;
string Password = modl.password;
string connectionString = WebConfigurationManager.ConnectionStrings["AccountsEntities"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connectionString);
SqlCommand sqlCmd = new SqlCommand("INSERT INTO TBL_ACCOUNTS (username, password) VALUES (@username, @password)", sqlConn);
sqlCmd.Parameters.AddWithValue("@username", UsrName);
sqlCmd.Parameters.AddWithValue("@password", Password);
try {
sqlConn.Open();
}
catch (Exception ex) {
}
try {
sqlCmd.ExecuteNonQuery();
}
catch (Exception ex) {
}
finally{
sqlConn.Close();
}
return View();
}