Asked by:
ASP NET cannot Save or Edit using Model

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
All replies
-
-
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
-
I don't understand this , What is this ?
- Proposed as answer by Rogeralina23 Friday, February 18, 2022 2:25 PM
-
-
-
-
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