none
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()
            {
                try
                {
                    myConnection.ConnectionString = GlobalVariabel.connString;
                    myConnection.Open();
                    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";
                    }
                    else
                    {
                        flag = "FAIL";
                    }
                }
                catch (Exception ex)
                {
                    flag = ex.Message;
                }
                finally
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                        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
  • Solution 1
    Remove the Read-only attribute on the file by following the steps below.

    Locate your file in the File Explorer.
    Right-click the file.
    Select Properties.
    Under the General tab, in the Attributes section, uncheck the Read-Only check box.
    Click OK.
    You should now be able to save changes to the file.

    Solution 2
    If you cannot change the Read-only attribute, try using the Save As option to save the file to another location on the computer, such as your desktop. This file does not have the read-only attribute, so you can edit it and save your changes.

    The file is on a read-only medium
    If the file you opened is on a read-only medium, like a CD-ROM or DVD-ROM, you cannot make changes and save them to the original file.

    Greeting,

    Ross Smith

    Tuesday, August 9, 2022 5:40 AM