从使用字典填充的组合框中读取值成员

时间:2018-10-12 14:03:43

标签: c# visual-studio-2013 combobox .net-4.0

combobox事件中,我使用dictionary填充了form_load,其值和显示成员如下所示

Dictionary<string, int> GravelCount = new Dictionary<string, int>
{
    {1,"text value 1"},
    {2,"text value 2"},
};

CB_GravelCount.DataSource = new BindingSource(GravelCount, null);
CB_GravelCount.DisplayMember = "Key";
CB_GravelCount.ValueMember = "Value";
CB_GravelCount.SelectedIndex = -1;

此值成员存储在sql服务器数据库中...一切正常,直到我尝试读取此存储的数据并在发生combobox数据时将其重新分配给form_load分配了组合框值的东西什么也没有显示,因为它获得了没有错误的索引-1

public int GetPersonification()
{
    string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString();
    string cmdStr = @"SELECT ID,
                                             SIZ,
                                             PLACE,
                                             ONE_OR_MORE,
                                             R_OR_L,
                                             EKO_OR_ASH,
                                             NOTICE
                                     FROM PERSONIFICATION
                                   WHERE SEANCE_ID=@SEANCE_ID;";

    using (SqlConnection conn = new SqlConnection(connStr))
    using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
    {

            conn.Open();
            cmd.CommandText = cmdStr;
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add(new SqlParameter("@SEANCE_ID", SqlDbType.Int)).Value = F0102.vSessionID;

            SqlDataReader sqlReader = cmd.ExecuteReader();

            while (sqlReader.Read())
            {
                PersonificationID = Convert.ToInt32(sqlReader[0].ToString());
                TB_Size.Text = sqlReader[1].ToString();                              //SIZ
                CB_TreatmentPlace.SelectedValue = sqlReader[2].ToString(); //PLACE
                CB_GravelCount.SelectedValue = sqlReader[3].ToString();      //ONE_OR_MORE
                CB_Side.SelectedValue = sqlReader[4].ToString();                 //R_OR_L
                CB_TreatmentWay.SelectedValue = sqlReader[5].ToString(); //EKO_OR_ASH
                TB_Note.Text = sqlReader[6].ToString();                             //NOTICE
            }
            return 1;
    }
}

2 个答案:

答案 0 :(得分:1)

解析为int。

CB_GravelCount.SelectedValue = int.Parse(sqlReader[3].ToString());

答案 1 :(得分:0)

从数据库中读取数据很可能会出现错误。在代码的SqlDataReader部分周围进行一次try / catch,以查看到底是什么在失败。我的猜测是,对于导致ToString失败或PersonificationId的Convert.ToInt32失败的数据列之一,您具有null值。阅读返回的异常消息很可能会将您带向正确的方向。 试试这个:

public int GetPersonification()
{
  string connStr = ConfigurationManager.ConnectionStrings["SRJDconnstr"].ToString();
  string cmdStr = @"SELECT ID,
                          SIZ,
                        PLACE,
                  ONE_OR_MORE,
                        R_OR_L,
                    EKO_OR_ASH,
                        NOTICE
          FROM PERSONIFICATION
          WHERE SEANCE_ID=@SEANCE_ID;";

  using (SqlConnection conn = new SqlConnection(connStr))
  using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
  {
    //Add the try here
    try
    {
      conn.Open();
      cmd.CommandText = cmdStr;
      cmd.CommandType = CommandType.Text;

      cmd.Parameters.Add(new SqlParameter("@SEANCE_ID", SqlDbType.Int)).Value = F0102.vSessionID;

      SqlDataReader sqlReader = cmd.ExecuteReader();
      int? persId = null;
      string siz,
      string place = "";
      string one_or_more = "";
      string r_or_l = "";
      string eko_or_ash = "";
      string notice = "";
      while (sqlReader.Read())
      {
          persId = sqlReader["ID"] as int? ?? default(int);
          siz = sqlReader["SIZ"] as string;
          place = sqlReader["PLACE"] as string;
          one_or_more = sqlReader["ONE_OR_MORE"] as string;
          r_or_l = sqlReader["R_OR_L"] as string;
          eko_or_ash = sqlReader["EKO_OR_ASH"] as string;
          notice = sqlReader["NOTICE"] as string;
      }
      PersonificationID = persId ?? 0; //null coalesce
      TB_Size.Text = siz;
      CB_TreatmentPlace.SelectedValue = place;
      CB_GravelCount.SelectedValue = one_or_more;
      CB_Side.SelectedValue = r_or_l;
      CB_TreatmentWay.SelectedValue = eko_or_ash;
      TB_Note.Text = notice;
      return 1;
    }
    catch (Exception ex)
    {
      //Read the exception message using your debugger.
      return 0;
    }
  }
}