c# - 为什么我不能将数据添加到新添加的列中
问题描述
我是业余 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!");
}
}
解决方案
如果你添加一个异常处理程序,它会告诉你出了什么问题。
将您的代码包装在 try/catch 块中。
try
{
// your code goes here
}
catch(Exception ex)
{
Debug.Writeln(ex.Message);
}
推荐阅读
- postgresql - 什么是 Oracle 时间戳(6)的 postgreSql 等价物。postreSql 是否支持纳秒格式的时间戳?
- macos - 通过 Firemonkey 访问第三方 Mac API
- css - 在样式化组件上回复此 CSS 代码
- sql - PgAdmin 4:PostgreSQL 错误连接到服务器
- node.js - 后端 Node.js 服务器在 Heroku 部署中不起作用
- list - 如何在颤动的 ReorderableListView 中删除菜单图标
- elasticsearch - 如何为 Elasticsearch 索引中的文档设置 TTL
- python - 训练遇到收敛问题的声学数据
- forms - Google Analytics Event Tracking - 表单提交数据少于实际提交数量
- java - 使用准备状态传递 Nullable 值