在没有任何数据时进行无效的读取尝试

zhanding2002 2012-07-10 04:58:45
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生学籍管理系统
{
public partial class DR : Form
{
public DR()
{
InitializeComponent();
}
string strcon = @"Data Source=.;Initial Catalog=FATE;Integrated Security=SSPI";

private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strcon);
//MessageBox(0,cn.ConnectionString,null,0);
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
MessageBox.Show("请输入用户名及密码", "登录失败");
else
{
conn.Open();

SqlCommand cmd = new SqlCommand("", conn);
string sql = "select jsmc from dengluxinxi where yh='" + textBox1.Text.Trim() + "' and mm='" + textBox2.Text.Trim() + "'";
cmd.CommandText = sql;
string jsmc;

if (cmd.ExecuteScalar() != null)
{
jsmc = cmd.ExecuteScalar().ToString();
//隐藏登录窗口
this.Visible = false;
//创建并打开主界面
Main main = new Main();
main.Tag = this.FindForm();
sql = "select * from quanxianbiao where jsmc='" + jsmc + "'";
//SqlDataReader dr;
cmd.CommandText = sql;
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
main.用户管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(1));
main.角色管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(2));
main.专业管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(3));
main.班级管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(4));
main.课程管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(5));
main.学生档案管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(6));
main.toolStripStatusLabel3.Text = textBox1.Text.Trim();
main.ShowDialog();

MessageBox.Show("用户名或密码错误", "登录失败");
//dr.Close();
conn.Close();
}
}
}
private void DR_Load(object sender, EventArgs e)
{
textBox1.Focus();
}

private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}



}
运行报错“在没有任何数据时进行无效的读取尝试”,红色为报错部分
...全文
2914 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dcleon 2012-11-29
  • 打赏
  • 举报
回复
if(dr.Read()) { main.用户管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(1)); main.角色管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(2)); main.专业管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(3)); main.班级管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(4)); main.课程管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(5)); main.学生档案管理ToolStripMenuItem.Visible = (bool)(dr.GetValue(6)); main.toolStripStatusLabel3.Text = textBox1.Text.Trim(); main.ShowDialog(); } else { MessageBox.Show("没有查询到有效的数据", "登录失败"); }
gzw13999 2012-11-03
  • 打赏
  • 举报
回复

dr.GetValue(1)  改为方法传值进去 直接返回bool 不行吗?

bool isNull(string a)
{
if(string.IsNullOrEmpty(a))
{
return true;
}
else
{
return  false
}

}

改为
main.用户管理ToolStripMenuItem.Visible = isNull(dr.GetValue(1));
ssxxhh1987923 2012-07-11
  • 打赏
  • 举报
回复
期待大牛
风一样的大叔 2012-07-11
  • 打赏
  • 举报
回复
dr.GetValue(1)没有取到值哇

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧