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
-
-