求助:未将对象引用设置到对象的实例
这是登陆框的登陆按钮的代码,当我输入的用户名和密码正确时程序没有问题,可是当用户名和密码输入错误时就会报错大家帮忙看看是哪出了问题?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);
}
}
{
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);
}
}