首页 > 解决方案 > 为什么我不能将数据添加到新添加的列中

问题描述

我是业余 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!");
            }
        }

标签: c#sql-server

解决方案


如果你添加一个异常处理程序,它会告诉你出了什么问题。

将您的代码包装在 try/catch 块中。

try
{
    // your code goes here
}

catch(Exception ex)
{
    Debug.Writeln(ex.Message);
}

推荐阅读