User2012384
User2012384

Reputation: 4919

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:

enter image description here

And here's the "selectedVal" property on "ddlPriority" user control enter image description here

Here's the ddlPriority_Load function

  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.

How can I solve this problem?


Edited:

Below is the selectPriorities method:

properties.cs

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

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;
        }

Upvotes: 0

Views: 198

Answers (1)

Uri Y
Uri Y

Reputation: 850

It looks like combxPriority.SelectedValue is null (no line is selected) and that's why combxPriority.SelectedValue.ToString() throws the exception. You have to make sure combxPriority.SelectedValue is not null before accessing it.

Upvotes: 1

Related Questions