using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace MySchool
{
    public partial class SelectForm : Form
    {
        public SelectForm()
        {
            InitializeComponent();
        }
        private void SelectForm_Load(object sender, EventArgs e)
        {
            string sql = "select difficulty from Question group by difficulty";
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = command.ExecuteReader();
                while (dataReader.Read())
                {
                    comboBox1.Items.Add(dataReader["difficulty"].ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            SearchQuestionByDifficulty();
        }
        private void SearchQuestionByDifficulty()
        {
            try
            {
                string sql = string.Format("select Question,Answer,Difficulty,optiona,optionb,optionc,optiond from Question where Difficulty = {0}", comboBox1.Text);
                SqlCommand commmand = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                SqlDataReader dataReader = commmand.ExecuteReader();
                listView1.Items.Clear();
                while (dataReader.Read())
                {
                    ListViewItem item = new ListViewItem(dataReader["Question"].ToString());
                    listView1.Items.Add(item);
                    item.SubItems.AddRange(new string[]
                {
                    dataReader["OptionA"].ToString(),
                    dataReader["OptionB"].ToString(),
                    dataReader["OptionC"].ToString(),
                    dataReader["OptionD"].ToString(),
                    dataReader["Answer"].ToString(),
                    dataReader["Difficulty"].ToString()});
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("请选择要修改的题目", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                int difficulty = Convert.ToInt32(((ToolStripItem)sender).Tag);                UpdateDifficulty(difficulty);                SearchQuestionByDifficulty();
            }
        }
        public void UpdateDifficulty(int difficulty)
        {
            int result;
            int questionId = Convert.ToInt32(listView1.SelectedItems[0].Tag);
            string sql = string.Format("update Question set Difficulty = '{0}' where QuestionId = '{1}'", difficulty, questionId);
            try
            {
                SqlCommand command = new SqlCommand(sql, DBHelper.connection);
                DBHelper.connection.Open();
                result = command.ExecuteNonQuery();
                if (result >= 1)
                {
                    MessageBox.Show("修改成功", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("修改失败", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                DBHelper.connection.Close();
            }
        }
    }
}
出错的问题在红色现实的地方.
本来这里是应该可以或许到我选到该问题的 数据库ID的 ,但是现在查断点调试后发现是0 郁闷中..
求解

解决方案 »

  1.   


    int questionId = Convert.ToInt32(listView1.SelectedItems[0].Tag.ToString()); 
      

  2.   

    int questionId = Convert.ToInt32((ListViewItem)listView1.SelectedItems[0].Tag.ToString()); 
    这样呢
      

  3.   

    干嘛把得到的数据转换成字符串在转换成int 
    肯定不行 试了
      

  4.   

    你的listView1里是不是没东西啊?保证该控件不为空然后再执行你那条语句。