い夏ㄖじòぴé琦゛ 2022-06-01 01:19 采纳率: 80%
浏览 273
已结题

c#运行弹出索引超出范围,必须为非负值并小于集合大小,但是不知道是哪里出了问题,怎么解决

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;namespace 学生选课系统{ public partial class frmSC : Form { public frmSC() { InitializeComponent(); } string selectcondition; private void frmSC_Load(object sender, EventArgs e) { DataBase dbStudent = new DataBase(); DataSet dataSetStudent = new DataSet(); #region 提取学生信息 string sqlStr = "select Sno,Sname from tbl_Student"; dataSetStudent = dbStudent.GetDataFromDB(sqlStr); if (dataSetStudent.Tables[0].Rows.Count > 0) { cmbStudent.Items.Clear();//清空 for (int i = 0; i < dataSetStudent.Tables[0].Rows.Count; i++) { //学号(4位)--姓名 cmbStudent.Items.Add(dataSetStudent.Tables[0].Rows[i]["Sno"].ToString() + "--" + dataSetStudent.Tables[0].Rows[i]["Sname"].ToString()); } } #endregion DataBase dbCourse = new DataBase(); DataSet dataSetCourse = new DataSet(); #region 提取课程信息 sqlStr = "select Cno,Cname from tbl_Course"; dataSetCourse = dbCourse.GetDataFromDB(sqlStr); if (dataSetCourse.Tables[0].Rows.Count > 0) { cmbCourse.Items.Clear(); for (int i = 0; i < dataSetCourse.Tables[0].Rows.Count; i++) { //课程号(4位)--课程名 cmbCourse.Items.Add(dataSetCourse.Tables[0].Rows[i]["Cno"].ToString() + "--" + dataSetCourse.Tables[0].Rows[i]["Cname"].ToString()); } } #endregion } //成绩录入 private void btnGradeIn_Click(object sender, EventArgs e) { try { if (cmbSelcondition.SelectedIndex == -1 || txtSelText.Text == "") { MessageBox.Show("录入成功"); cmbCourse.SelectedIndex = -1; cmbStudent.SelectedIndex = -1; txtGrade.Text = ""; } else { string sqlStr = "select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC wheretbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and" + selectcondition + "='" + txtSelText.Text.Trim() + "'"; sqlStr = string.Format("select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and {0}='{1}'", selectcondition, this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void btnSelect_Click(object sender, EventArgs e) { try { if (cmbSelcondition.SelectedIndex==-1||txtSelText.Text=="") { MessageBox.Show("请选择查询条件并输入查询值!"); } else { string sqlStr =string.Format( "select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade FROM tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and {0}='{1}'", selectcondition,this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); sqlStr = "select * from tbl_SC"; ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count==0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } void SetHeaderText() { dgrdvResult.Columns[0].HeaderText = "学号"; dgrdvResult.Columns[1].HeaderText = "姓名"; dgrdvResult.Columns[2].HeaderText = "课程号"; dgrdvResult.Columns[3].HeaderText = "课程名"; dgrdvResult.Columns[4].HeaderText = "成绩"; } private void cmbSelcondition_SelectedIndexChanged(object sender, EventArgs e) { switch (cmbSelcondition.SelectedIndex) { case 0: selectcondition = "tbl_SC.Sno"; break; case 1: selectcondition = "tbl_SC.Cno"; break; } } private void btnSelAll_Click(object sender, EventArgs e) { try { string sqlStr = string.Format("select tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade from tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno ", this.txtSelText.Text.Trim()); DataBase db = new DataBase(); DataSet ds = new DataSet(); sqlStr = "select * from tbl_SC"; ds = db.GetDataFromDB(sqlStr); if (ds.Tables[0].Rows.Count == 0) { MessageBox.Show("没有符合条件的成绩记录!"); } else { dgrdvResult.DataSource = ds.Tables[0]; SetHeaderText(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void dgrdvResult_CellContentClick(object sender, DataGridViewCellEventArgs e) { } }}

  • 写回答

8条回答 默认 最新

  • 心蓝168 2022-06-04 09:11
    关注

    F5跟一下吧,应该DataSet中的Table[0]这种有问题吧,这个得看看你查询数据时返回的DataSet是否包含Table。
    有[n]的地方注意一下,是否用错变量。一般情况下都是变量用错才会导致超范围的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月4日
  • 已采纳回答 6月4日
  • 创建了问题 6月1日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵