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();
        }

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?

yes account_id is identity, image can be null

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.