0
当我将记录保存到使用C#sql窗体Datagrid的DataBase中时,我重新启动应用程序或当我捕获异常错误时丢失了所有记录但是这个问题并不经常发生,但是当出现异常错误时,我的所有记录都将丢失。将记录保存到数据库使用Windows窗体datagrid中的C#sql
这是我的保存按钮代码。
private void btnSave_Click(object sender, EventArgs e)
{
try
{
//Reconnect to Database
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
// cb.DataAdapter.Update(ds.Tables["Impi"]);
//Create a new row
DataRow dr = ds.Tables["tblImpi"].NewRow();
//save the record to first column
dr[0] = txtSerial.Text;
//Check data entry if its no empt
if (txtName.Text != "")
{
dr[1] = txtName.Text;
}
//Check if combox has values and assign one the values to it
if (cbModel.Text == "MKI" || cbModel.Text == "MKII")
{
dr[2] = cbModel.Text;
}
if (cbStatus.Text == "Serviceble" || cbStatus.Text == "Unserviceble")
{
dr[3] = cbStatus.Text;
}
if (cbDeprtmnt.Text == "AIR" || cbDeprtmnt.Text == "LAND" || cbDeprtmnt.Text == "NAVY" || cbDeprtmnt.Text == "SPECIAL FORCE")
{
dr[4] = cbDeprtmnt.Text;
}
if (cmbVesrion.Text == "10" || cmbVesrion.Text == "11" || cmbVesrion.Text == "12")
{
dr[5] = cmbVesrion.Text;
}
if (txtGsm.Text.Length != 0)
{
dr[6] = txtGsm.Text;
}
if (txtImei.Text.Length != 0)
{
dr[7] = txtImei.Text;
}
//Check if serial number value is not zero
if ((txtSerial.Text.Length != 0) || (txtGsm.Text.Length!=0) || (txtImei.Text.Length!=0))
{
//Create a search Method to search before you save the serial number
bool search = SearchRecBeforeSave(txtSerial.Text, txtName.Text,txtGsm.Text,txtImei.Text);
//bool search = SearchSerialNumberBeforeSave(txtSerial.Text);
if (search == false)
{
// DialogResult dr2 = MessageBox.Show("Are you sure you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (txtGsm.Text == "")
{
txtGsm.Text = "";
txtGsm.ForeColor = Color.Red;
tootip.SetToolTip(txtGsm, txtGsm.Text);
}
if (txtImei.Text == "")
{
txtImei.Text = " ";
txtImei.ForeColor = Color.Red;
tootip.SetToolTip(txtImei, txtImei.Text);
}
if (txtName.Text.Length == 0)
{
txtName.Text = "";
txtName.ForeColor = Color.Red;
tootip.SetToolTip(txtName, txtName.Text);
}
else if (txtGsm.Text != "" && txtImei.Text != "" && txtName.Text.Length != 0)
{
DialogResult dr2 = MessageBox.Show("Are you sure you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr2 == DialogResult.Yes)
{
ds.Tables["tblImpi"].Rows.Add(dr);
System.Data.SqlClient.SqlCommandBuilder cb1;
cb1 = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
cb1.DataAdapter.Update(ds.Tables["tblImpi"]);
// dataAdapter.Update(ds,"Impi");
// maxRow = maxRow + 1;
// inc = maxRow - 1;
MessageBox.Show("Serial Number Added Successful");
}
}
else
{
//Fix this part
MessageBox.Show("This Serial Number Exist and will create the duplicate.\nSerial Number not Saved");
MessageBox.Show("Data Entry was not saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("The information that you have enterd already exist\nPlease Enter a Correct Entries ", "Data Entry");
txtSerial.Text = "Please Enter Impi Serial Number";
txtSerial.ForeColor = Color.Red;
tootip.SetToolTip(txtSerial, txtSerial.Text);
txtName.Text = "Please Enter the Track Number";
txtName.ForeColor = Color.Red;
tootip.SetToolTip(txtName, txtName.Text);
txtGsm.Text = "Please Enter the GSM Sim Number";
txtGsm.ForeColor = Color.Red;
tootip.SetToolTip(txtGsm, txtGsm.Text);
txtImei.Text = "Please Enter the IMEI Number ";
txtImei.ForeColor = Color.Red;
tootip.SetToolTip(txtImei, txtImei.Text);
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
//MessageBox.Show("Incorrect Data Entry", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
}
con.Close();
}