[英]insert stored procedure with set null parameters in entity framework
I want to insert null
parameter in some values, but I get the following error : 我想在某些值中插入
null
参数,但是出现以下错误:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.NullReferenceException:'对象引用未设置为对象的实例。 System.Windows.Forms.ListControl.SelectedValue.get returned null.
System.Windows.Forms.ListControl.SelectedValue.get返回null。
The program is a Windows Forms
application, and the code is as follows: 该程序是
Windows Forms
应用程序,其代码如下:
using (var context = new SamenEntities())
{
person ppp = new person()
{
id = textBox1.Text.Trim(),
name = textBox2.Text.Trim(),
family = textBox3.Text.Trim(),
birth_date = dateTimePicker1.Value,
birth_provice = Convert.ToInt32(comboBox1.SelectedValue.ToString()),
note = richTextBox1.Text.Trim(),
img_personal = ImageToByteArray(pictureBox1.Image),
date_register = DateTime.Now
};
context.persons.Add(ppp);
context.SaveChanges();
}
How to enter null
value or enter default value from SQL server? 如何从SQL Server输入
null
值或输入默认值?
By look at the exception, it says that the ComboBox
does not have any value selected. 通过查看异常,它表示
ComboBox
没有选择任何值。 It is not about SQL Server not accepting null
at this point. 这与SQL Server此时不接受
null
无关。
With that information, you should check that one value is selected, meaning it is not null
, like this: 有了这些信息,您应该检查是否选择了一个值,这意味着它不为
null
,如下所示:
object selectedValue = comboBox1.SelectedValue;
if(selectedValue != null)
{
ppp.birth_provice = Convert.ToInt32(selectedValue.ToString()),
}
On top of that, you may want to consider using Int32.TryParse
to make sure the selected value is an actual integer. 最重要的是,您可能要考虑使用
Int32.TryParse
来确保所选值是实际整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.