ASP NET cannot Save or Edit using Model RRS feed

  • Question

  • Hy Friends, I have a problem with ASP.Net, Why I cannot save or edit this data? When I Click save or edit, it will show failed 

        public string kode_dosen, kode_jurusan, nama_dosen, telp, alamat;
            SqlConnection myConnection = new SqlConnection();
            string flag;
            public DataSet ds = new DataSet();
            public string insertData()
                    myConnection.ConnectionString = GlobalVariabel.connString;
                    string query = "insert into Dosen values(@kode_dosen, @kode_jurusan, @nama_dosen, @telp, @alamat)";
                    SqlCommand com = new SqlCommand(query, myConnection);
                    com.Parameters.AddWithValue("@kode_dosen", kode_dosen);
                    com.Parameters.AddWithValue("@kode_jurusan", kode_jurusan);
                    com.Parameters.AddWithValue("@nama_dosen", nama_dosen);
                    com.Parameters.AddWithValue("@telp", telp);
                    com.Parameters.AddWithValue("@alamat", alamat);
                    int i = com.ExecuteNonQuery();
                    if (i > 0)
                        flag = "OK";
                        flag = "FAIL";
                catch (Exception ex)
                    flag = ex.Message;
                    if (myConnection.State == ConnectionState.Open)
                        myConnection = null;
                return flag;

    Thanks For Your Help

    Friday, January 7, 2022 5:06 PM

All replies

  • Hi, did you get an exception?
    Saturday, February 12, 2022 7:31 AM
  • hello

    t's hard to tell from your code, but it looks like you are loading the object from the database based on the id coming back from the view, setting it's State to Modified and then saving the same object.

    It doesn't look like you're actually setting the properties from the view back to the object before saving it to the database. What ORM are you using? Most ORMs will handle the State of the object when each property is modified and essentially set its own state to Modified for when it's committed.

    Set a breakpoint before you load your object from the database, quick watch the values coming back from the view and then quickwatch them again after you've loaded from the database, I think you'll discover you've overwritten them

    Saturday, February 12, 2022 11:27 PM
  • I don't understand this , What is this ?
    • Proposed as answer by Rogeralina23 Friday, February 18, 2022 2:25 PM
    Friday, February 18, 2022 11:13 AM
  • Thanks for the post, in my case I was able to fix the problem of exception generated when trying to save a new entry.
    Monday, February 28, 2022 7:19 AM
  • I am new here , so don't have any idea about it.
    Friday, March 4, 2022 3:44 AM
  • n this tutorial you'll update related data. For most relationships, this can be done by updating either foreign key fields
    Monday, June 13, 2022 4:05 AM