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

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 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染