| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4594 人关注过本帖
标题:[求助]老是说在没有任何数据时进行无效的读取尝试
取消只看楼主 加入收藏
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
 问题点数:0 回复次数:2 
[求助]老是说在没有任何数据时进行无效的读取尝试

我做了一个用SQL储存选课信息的小程序,代码如下:
那个string)dr["判断是否已经选课"]!="1")是用户登录时自动赋给字段"判断是否已经注册"的,赋值为"1",
但是他老是提示"在没有任何数据时进行无效的读取尝试",不知道怎么办啊,有没有更先进的判断方法啊,请高手指教个啊,在这里先谢谢了



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data ;
using System.Data .SqlClient ;

namespace WindowsApplication2
{
/// <summary>
/// Form5 的摘要说明。
/// </summary>
public class Form5 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public int a=0;
private string resource()
{

string strbox="";
if(checkBox1.Checked )
{
a=a+1;
strbox+=checkBox1.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox2.Checked )
{
a=a+1;
strbox+=checkBox2.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox3.Checked )
{
a=a+1;
strbox+=checkBox3.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox4.Checked )
{
a=a+1;
strbox+=checkBox4.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}
if(checkBox5.Checked )
{
a=a+1;
strbox+=checkBox5.Text+"\0";
}
else
{
if (strbox=="")
strbox="";
}

return strbox;

}
private bool exit()
{
SqlCommand cmd;
SqlDataReader dr;
Form1 form1=new Form1 ();
string strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
string str;
str="select * from 注册信息 where 帐号='"+form1.Userid +"'";
cmd=new SqlCommand();
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
dr=cmd.ExecuteReader();
if ((string)dr["判断是否已经选课"]!="1")
{
return false;
}
else
{
return true;
}
cmd.Connection.Close ();
dr.Close ();
}


/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(40, 24);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "网页设计";
this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
//
// checkBox2
//
this.checkBox2.Location = new System.Drawing.Point(40, 72);
this.checkBox2.Name = "checkBox2";
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "ADCsee";
//
// checkBox3
//
this.checkBox3.Location = new System.Drawing.Point(40, 120);
this.checkBox3.Name = "checkBox3";
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "Flash";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox5);
this.groupBox1.Controls.Add(this.checkBox4);
this.groupBox1.Controls.Add(this.checkBox3);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Location = new System.Drawing.Point(112, 24);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(216, 264);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "请选择课程";
//
// checkBox5
//
this.checkBox5.Location = new System.Drawing.Point(40, 208);
this.checkBox5.Name = "checkBox5";
this.checkBox5.TabIndex = 4;
this.checkBox5.Text = "3Dmax";
//
// checkBox4
//
this.checkBox4.Location = new System.Drawing.Point(40, 160);
this.checkBox4.Name = "checkBox4";
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "photoshop";
//
// label1
//
this.label1.ForeColor = System.Drawing.Color.Red;
this.label1.Location = new System.Drawing.Point(344, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 72);
this.label1.TabIndex = 4;
this.label1.Text = "注意!每位学生最多只能选择3门!!";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 320);
this.button1.Name = "button1";
this.button1.TabIndex = 5;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 320);
this.button2.Name = "button2";
this.button2.TabIndex = 6;
this.button2.Text = "退出";
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(568, 366);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Name = "Form5";
this.Text = "选课";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{
if(exit())
{
SqlCommand cmd;
try
{


string strsql;
string str;
int row;
Form1 form1=new Form1 ();
strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";

string str1 =resource();
if (a==0||a>3)
{
MessageBox.Show ("每位同学最多只能选三门课程","提示");
a=0;
return;
}
else
{

str="update 注册信息 set\n";
str+="所选择的课程='"+str1+"\n'";
str+="where 帐号='"+form1.Userid+"'" ;
}
cmd=new SqlCommand ( );
cmd.Connection =new SqlConnection (strsql);
cmd.Connection .Open ();
cmd.CommandText =str;
row=cmd.ExecuteNonQuery ();
if (row==1)
{
MessageBox.Show ("成功");
cmd.Connection .Close ();

}
else
{
MessageBox.Show ("失败");
return ;
}
}


catch(Exception ex)
{
MessageBox.Show (ex.Message );
}
}
else
{
MessageBox.Show ("你已经选过课程,不能再更改!","提示");
}





}
}
}

搜索更多相关主题的帖子: 数据 using FONT 尝试 System 
2006-06-26 11:11
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 

不行啊,这样的话,又出现
"F:\c#例题\课题(2)\WindowsApplication2\Form5.cs(84): “WindowsApplication2.Form5.exit()” : 并非所有的代码路径都返回值"
这样的错误.


一切都是快乐的
2006-06-26 16:35
hl0071
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2005-11-11
收藏
得分:0 

靠,不是吧


一切都是快乐的
2006-06-27 08:39
快速回复:[求助]老是说在没有任何数据时进行无效的读取尝试
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017055 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved