我有25个ComboBox,我想将所有值移动到数组中,以便可以将它们放入SQL Server。我正在使用的当前方法是
string[] Answers = { cbxIntro.SelectedValue.ToString(),
cbxAge.SelectedValue.ToString(), etc
然后抛出错误代码
System.NullReferenceException: 'Object reference not set to an instance of
an object.'
任何帮助将不胜感激
答案 0 :(得分:-1)
听起来像您的值之一为空,请尝试添加Coalesce和ifNotNull运算符
即
string[] Answers = { cbxIntro?.SelectedValue?.ToString() ?? "default", cbxAge?.SelectedValue?.ToString() ?? "default", ...
读为 如果cbxIntro不为null,则调用SelectedValue 如果SelectedValue不为null,则调用Tostring 否则返回“默认”字符串