注册 登录
编程论坛 ASP.NET技术论坛

求助:未将对象引用设置到对象的实例

yunj1105 发布于 2007-04-21 14:52, 1046 次点击
这是登陆框的登陆按钮的代码,当我输入的用户名和密码正确时程序没有问题,可是当用户名和密码输入错误时就会报错大家帮忙看看是哪出了问题?
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);
}
}
11 回复
#2
冰残剑2007-04-21 15:05
你要先判断读出来的数据是否存在,然后再做接下去的事情
#3
yunj11052007-04-21 15:17
对啊,我程序就是这么写的啊
#4
nic2007-04-21 16:24
dim myex as string= myCommand1.ExecuteScalar()
if myex is DBnull then
myex=""
else
myex=myex.toString()
end if

以上是VB的
#5
yunj11052007-04-21 16:29
能否说说为什么啊?
#6
IPV62007-04-22 11:18
你的连接没有打开
#7
yunj11052007-04-22 19:21
打开了啊,能不能告我哪里出了问题啊?
#8
球球2007-04-22 19:57
myCommand1.Connection.Open();这句不要.
在上面用myConnection.Open();打开试试.
public SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);这样没用过,不知道行不行.
#9
yunj11052007-04-22 20:04

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

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

#10
rstp2007-04-23 00:29
用下myConnecttion.Open()试试看
#11
球球2007-04-23 00:51
当然了,输入错的这个myCommand1.ExecuteScalar()对象就不存在,这个对象不存在又怎么用它的ToString()方法呢??
所以这句
myCommand1.ExecuteScalar().ToString();//未将对象引用设置到对象的实例
会报错了.
不要后面的ToString()
#12
yunj11052007-04-25 17:38
呵呵这段调对了,谢谢球球
1