我是一名业余C#开发人员。现在我正在使用destkop应用程序,并与MS2015和SQL Management Studio一起使用。我在数据库中添加了新列,并在.cs代码中编写了所需的内容,但无法添加我的数据进入列。即使它不为空,它也始终显示NULL。这是我保存数据的代码:
private void btnSave_Click(object sender, EventArgs e) {
if (txtAmount.Text=="" ||txtBarcode.Text == "" || txtName.Text == "" || txtEntrance.Text == "" || txtExit.Text == "" || txtQuantity.Text == "" || txtStatus.Text == "")
{
SoundPlayer sp = new SoundPlayer(@"C:\Users\Nicat\Downloads\CompError.wav");
sp.Play();
MessageBox.Show("Please fill all gaps!");
}
else
{
model.Amount = txtAmount.Text.Trim();
model.Name = txtName.Text.Trim();
model.Entrance_Time = txtEntrance.Text.Trim();
model.Exit_Time = txtExit.Text.Trim();
model.Status = txtStatus.Text.Trim();
model.Quantity = txtQuantity.Text.Trim();
model.Barcode_No = txtBarcode.Text.Trim();
using (DBEntities db = new DBEntities())
{
if (model.ID == 0)
db.Tables.Add(model);
else
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
Reset();
PopulateDataGridView();
SoundPlayer sp = new SoundPlayer(@"C:\Users\Nicat\Downloads\Button.wav");
sp.Play();
MessageBox.Show("Inventory submitted succesfully!");
}
}
答案 0 :(得分:0)
如果添加异常处理程序,它将告诉您出了什么问题。
将代码包装在try / catch块中。
try
{
// your code goes here
}
catch(Exception ex)
{
Debug.Writeln(ex.Message);
}