0

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();
        }
2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by G_Waddell
0

Hi,
Do you have account_id set to be an identity? I don't see you creating an Image value either do you have it set to accept null values?

0

Do you get any error message?
It maybe worth looking at setting the parameter type as well when you add them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.