简体   繁体   English

C#无法更改用户控件的{name}属性

[英]C# Failed to change {name} property of user control

When I tried to change the {name} property of the user controls on the form, the below error occurs: 当我尝试更改表单上用户控件的{name}属性时,发生以下错误:

在此处输入图片说明

And here's the "selectedVal" property on "ddlPriority" user control 这是“ ddlPriority”用户控件上的“ selectedVal”属性 在此处输入图片说明

Here's the ddlPriority_Load function 这是ddlPriority_Load函数

  Scheduler.DAL.TableClass.Priority pri = new Scheduler.DAL.TableClass.Priority();
            combxPriority.DataSource = pri.selectPriorities();
            combxPriority.DisplayMember = "name";
            combxPriority.ValueMember = "PriorityID";
            Scheduler.DAL.TableClass.Settings set = new Scheduler.DAL.TableClass.Settings();
            if (set.thisTable != null)
                combxPriority.SelectedValue = set.thisTable.DefaultPriorityID;

The ddlPriority user control is just a combo box. ddlPriority用户控件只是一个组合框。

How can I solve this problem? 我怎么解决这个问题?


Edited: 编辑:

Below is the selectPriorities method: 下面是selectPriorities方法:

properties.cs properties.cs

public static string DBConnection = ConfigurationSettings.AppSettings["DB_Connection"];

Priority.cs Priority.cs

public DataTable selectPriorities()
        {
            SqlConnection con = new SqlConnection(properties.DBConnection);
            con.Open();
            string sqlQuery = "select name, PriorityID from Priority";
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand(sqlQuery, con);
            SqlDataReader read = cmd.ExecuteNonQuery();
            dt.Load(read);
            read.Close();
            con.Close();
            return dt;
        }

It looks like combxPriority.SelectedValue is null (no line is selected) and that's why combxPriority.SelectedValue.ToString() throws the exception. 看起来combxPriority.SelectedValuenull (未选择任何行),这就是为什么combxPriority.SelectedValue.ToString()引发异常的原因。 You have to make sure combxPriority.SelectedValue is not null before accessing it. 在访问它之前,必须确保combxPriority.SelectedValue不为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM