| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1041 人关注过本帖
标题:求助:未将对象引用设置到对象的实例
只看楼主 加入收藏
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
 问题点数:0 回复次数:11 
求助:未将对象引用设置到对象的实例
这是登陆框的登陆按钮的代码,当我输入的用户名和密码正确时程序没有问题,可是当用户名和密码输入错误时就会报错大家帮忙看看是哪出了问题?
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "")
{
Response.Write("<script defer>alert('请每项都输入!');</script>");
}
else
{
string strCmd = "select user_name from lab_users where user_ID like '" + TextBox1.Text + "'and password like '" + TextBox2.Text + "'";
Comm1 name_com = new Comm1();
string username = name_com.EXS(strCmd);
if (username != null)
{
banduan(username);
}
else
{
Response.Write("<script defer>alert('密码不正确!');</script>");
TextBox1.Text = " ";
TextBox2.Text = " ";
}
}
}
void banduan( string username)
{
Session["user_ID"] = TextBox1.Text;
Session["user_name"] = username;
string strCmd_type = "select user_type from lab_users where user_ID like'" + TextBox1.Text + "'";
Comm1 flag_com = new Comm1();
int flag = int.Parse(flag_com.EXS(strCmd_type));
Session["type"] = flag;
if (flag == 0)
{ Response.Redirect("Admin.aspx"); }
else if (flag == 1)
{ Response.Redirect("Leader.aspx"); }
else if (flag == 2)
{ Response.Redirect("A_Teacher.aspx"); }
else
{ Response.Redirect("A_Student.aspx"); }
}
public class Comm1
{
public SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);

public Comm1()
{

}
public string EXS(string str1)
{
SqlCommand myCommand1 = new SqlCommand(str1, myConnection);
myCommand1.Connection.Open();
string myex = myCommand1.ExecuteScalar().ToString();//未将对象引用设置到对象的实例
myConnection.Close();
return(myex);
}
}
搜索更多相关主题的帖子: 实例 对象 
2007-04-21 14:52
冰残剑
Rank: 1
等 级:新手上路
威 望:2
帖 子:179
专家分:0
注 册:2006-3-6
收藏
得分:0 
你要先判断读出来的数据是否存在,然后再做接下去的事情

剑雪无情冰封流 残阳血照
2007-04-21 15:05
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
得分:0 
对啊,我程序就是这么写的啊

2007-04-21 15:17
nic
Rank: 2
等 级:新手上路
威 望:4
帖 子:175
专家分:0
注 册:2005-11-1
收藏
得分:0 
dim myex as string= myCommand1.ExecuteScalar()
if myex is DBnull then
myex=""
else
myex=myex.toString()
end if

以上是VB的

2007-04-21 16:24
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
得分:0 
能否说说为什么啊?

2007-04-21 16:29
IPV6
Rank: 1
等 级:新手上路
威 望:2
帖 子:265
专家分:0
注 册:2006-9-7
收藏
得分:0 
你的连接没有打开

2007-04-22 11:18
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
得分:0 
打开了啊,能不能告我哪里出了问题啊?

2007-04-22 19:21
球球
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:1146
专家分:265
注 册:2005-11-28
收藏
得分:0 
myCommand1.Connection.Open();这句不要.
在上面用myConnection.Open();打开试试.
public SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);这样没用过,不知道行不行.

好累
2007-04-22 19:57
yunj1105
Rank: 1
等 级:新手上路
威 望:1
帖 子:757
专家分:0
注 册:2007-2-13
收藏
得分:0 

但是输入正确的用户名密码就运行正常,输入错误的才报这样的错

[此贴子已经被作者于2007-4-22 20:07:27编辑过]


2007-04-22 20:04
rstp
Rank: 1
等 级:新手上路
威 望:1
帖 子:50
专家分:0
注 册:2006-10-13
收藏
得分:0 
用下myConnecttion.Open()试试看
2007-04-23 00:29
快速回复:求助:未将对象引用设置到对象的实例
数据加载中...
 
   



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

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