首页 新闻 会员 周边 捐助

winform listbox .SelectedIndex = 0 输入字符串格式不正确

0
悬赏园豆:50 [待解决问题]

        public void procate()
        {
          
           // PrintDB pdb = new PrintDB();
            MySqlConnection con = pdb.Creation();
            con.Open();
            MySqlCommand com = new MySqlCommand("select categroy from proprice where type='" + listBox1.Text + "' group by categroy", con);
            MySqlDataReader da = com.ExecuteReader();
            if (listBox2.Items.Count > 0)
            {
                listBox2.Items.Clear();
            }
            if (da.HasRows)
            {
                while (da.Read())
                {
                    listBox2.Items.Add(da["categroy"].ToString());
                   
                }
                listBox2.SelectedIndex = 0;//输入字符串格式不正确
            }
            else
            {
                listBox2.Items.Add("0");
            }
           
            da.Dispose();
            da.Close();
            con.Dispose();
            con.Close();
        }
        /// <summary>
        /// 获取产品样式列表
        /// </summary>
        public void prostyle()
        {
           
            MySqlConnection con = pdb.Creation();
            con.Open();
            MySqlCommand com = new MySqlCommand("select style from proprice where categroy='" + listBox2.Text + "' group by style", con);
            MySqlDataReader da = com.ExecuteReader();
            if (listBox3.Items.Count > 0)
            {
                listBox3.Items.Clear();
            }
          
            if (da.HasRows)
            {
                while (da.Read())
                {
                    listBox3.Items.Add(da["style"].ToString());
                }
               listBox3.SelectedIndex = 0;
             }
            else
            {
                listBox3.Items.Add("0");
            }
           
            da.Dispose();
            da.Close();
            con.Dispose();
            con.Close();
           
        }
        /// <summary>
        /// 获取产品数量
        /// </summary>
        public void proqty()
        {
          // listBox4.Items.Clear();
            MySqlConnection con = pdb.Creation();
            con.Open();
            MySqlCommand com = new MySqlCommand("select qty from proprice where categroy='"+listBox2.Text+"' and style='" + listBox3.Text + "' group by qty", con);
            MySqlDataReader da = com.ExecuteReader();
           
            if (listBox4.Items.Count > 0)
            {
                listBox4.Items.Clear();
            }
            if (da.HasRows)
            {
           
                while (da.Read())
                {
                    listBox4.Items.Add(da["qty"].ToString());
                }
                              listBox4.SelectedIndex = 0;
            }
            else
            {
                listBox4.Items.Add("0");

            }
           
            da.Dispose();
            da.Close();
            con.Dispose();
            con.Close();
           
        }
        /// <summary>
        /// 获取产品规格
        /// </summary>
        public void prounit()
        {
            //listBox5.Items.Clear();
            MySqlConnection con = pdb.Creation();
            con.Open();
            MySqlCommand com = new MySqlCommand("select unit from proprice where categroy='"+listBox2.Text+"' and style='" + listBox3.Text + "' group by unit", con);
            MySqlDataReader da = com.ExecuteReader();
          //listBox5.Items.Clear();
          if (listBox5.Items.Count > 0)
          {
              listBox5.Items.Clear();
          }
            if (da.HasRows)
            {
                while (da.Read())
                {
                    listBox5.Items.Add(da["unit"].ToString());
                }
                                listBox5.SelectedIndex =0;
            }
            else
            {
                listBox5.Items.Add("0");
            }
           
            da.Dispose();
            da.Close();
            con.Dispose();
            con.Close();
           
        }

AndyCa的主页 AndyCa | 初学一级 | 园豆:150
提问于:2011-11-23 09:39
< >
分享
所有回答(2)
0

我这样试了的:

    listBox1.Items.Add("w");
            listBox1.Items.Add("w");
            listBox1.Items.Add("w");
            listBox1.Items.Add("w");
            listBox1.Items.Add("w");
            listBox1.SelectedIndex = 0;没有错啊。

你看一下是否是读取数据库的值得方法有错。

悟行 | 园豆:12559 (专家六级) | 2011-11-23 10:06
0
                while (da.Read())
{
listBox2.Items.Add(da["categroy"].ToString());
//断点下这里,看看da["categroy"].ToString()的值
           
}
diorlv | 园豆:259 (菜鸟二级) | 2011-11-25 10:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册