Answer the question
In order to leave comments, you need to log in
Doesn't save changes to the database
I have a form with a datagridview. When you click on the "change data" button, a new form opens, where the data of the current row from the datagridview is displayed in the text boxes. Further at successful filling, I create DataRow and I change the data. Displays changes in the datagridview, but does not save it to the database itself (i.e., after restarting the application, the data is the same). Adding and deleting data works. I don't understand what is wrong.
form with datagridview
private void button4_Click(object sender, EventArgs e)
{
string kod = dgv_med[0, dgv_med.CurrentRow.Index].Value.ToString();
string name = dgv_med[1, dgv_med.CurrentRow.Index].Value.ToString();
string typemed = dgv_med[2, dgv_med.CurrentRow.Index].Value.ToString();
EditMed EditMed = new EditMed(kod, name, typemed);
if (EditMed.ShowDialog() != DialogResult.OK)
{
MessageBox.Show("Мед. учреждение не изменено");
return;
}
DataRow myRow = DataSet.Tables["MedTable"].Rows.Find(kod);
myRow.BeginEdit();
myRow["Kod_med"] = EditMed.MedId;
myRow["Nazvanie"] = EditMed.MedName;
myRow["Type"] = EditMed.MedType;
myRow.EndEdit();
DataSet.Tables["MedTable"].AcceptChanges();
dal.SaveChanges(DataSet);
}
public partial class EditMed : Form
{
public int MedId
{
get { return int.Parse(txtKod.Text); }
}
public string MedName
{
get { return txtName.Text.Trim(); }
}
public string MedType
{
get { return txtType.Text.Trim(); }
}
public EditMed(string Kod, string name, string typemed)
{
InitializeComponent();
txtKod.Text = Kod;
txtName.Text = name;
txtType.Text = typemed;
txtKod.Text.Trim();
txtName.Text.Trim();
txtType.Text.Trim();
}
private void btn_saveEditMed_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtKod.Text.Trim()) ||
String.IsNullOrEmpty(txtName.Text.Trim()) ||
String.IsNullOrEmpty(txtType.Text.Trim())
)
DialogResult = DialogResult.No;
else
DialogResult = DialogResult.OK;
}
}
class Dal
{
string connectionString = ConfigurationManager.ConnectionStrings["RSPK1managerConnectionString"].ConnectionString;
DataSet DataSet;
SqlDataAdapter medDA, prihodDA;
SqlCommandBuilder MedCB;
public Dal()
{
DataSet = new DataSet("RSPK");
medDA = new SqlDataAdapter("SELECT * FROM Med ORDER by Kod_Med", connectionString);
prihodDA = new SqlDataAdapter("SELECT * FROM prihod ORDER by Kod_prihod", connectionString);
//Билдер вставка удаление обновление данных
MedCB = new SqlCommandBuilder(medDA);
}
public DataSet GetAllData()
{
try
{
medDA.Fill(DataSet,"MedTable");
prihodDA.Fill(DataSet, "PrihodTable");
DataSet.Tables[0].PrimaryKey = new DataColumn[] { DataSet.Tables[0].Columns[0] };
DataSet.Tables[0].Columns[0].Unique = true;
DataRelation prskDR = new DataRelation("Medprihod",
DataSet.Tables["MedTable"].Columns["Kod_med"],
DataSet.Tables["PrihodTable"].Columns["Kod_otprav"]);
DataSet.Relations.Add(prskDR);
}
catch
{
}
return DataSet;
}
internal bool SaveChanges(DataSet DataSet_)
{
try
{
medDA.Update(DataSet_.Tables["MedTable"]);
}
catch
{
return false;
}
return true;
}
Answer the question
In order to leave comments, you need to log in
Fixed the problem. It turned out that DataSet.Tables["MedTable"].AcceptChanges(); not needed.
"DataSet.AcceptChanges marks the dataset as unchanged.
To save the data, you need to call Update on the adapter. In this case, database update commands will be generated for all unsaved changes. Accordingly, if AcceptChanges is called before that, nothing will be saved to the database either."
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question