我在做一个系统,首页上要求是有三个权限,学生,教师和管理员,我用radiobuttonlist设了三个权限,想在button按钮里实现选择任何一个时,登录的界面不一样,代码如下: protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButtonList1.Text = "学生")//运行说这有错误,无法将string转换为bool型。求救,我该怎么改
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [student] where sno='" + TextBox1.Text + "' and sname='" + TextBox2.Text + "'and sdept='"+ TextBox3 .Text +"'and spwd='" + TextBox4.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["sname"] = dr["sname"].ToString();
Session["sno"] = dr["sno"].ToString();
Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
} }
if(RadioButtonList1 .Text ="教师")
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [teacher] where tno='" + TextBox1.Text + "' and tname='" + TextBox2.Text + "'and tdept='" + TextBox3.Text + "'and tpwd='"+TextBox4 .Text +"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write(" <script>alert('登录成功!'); </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
}
}
if(RadioButtonList1 .Text ="管理员")
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [admin] where ano='" + TextBox1.Text + "'and ananme='"+ TextBox2 .Text +"'and adept='"+ TextBox3 .Text + "' and apwd='" + TextBox4.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write(" <script>alert('登录成功!'); </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
}
}
{
if (RadioButtonList1.Text = "学生")//运行说这有错误,无法将string转换为bool型。求救,我该怎么改
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [student] where sno='" + TextBox1.Text + "' and sname='" + TextBox2.Text + "'and sdept='"+ TextBox3 .Text +"'and spwd='" + TextBox4.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["sname"] = dr["sname"].ToString();
Session["sno"] = dr["sno"].ToString();
Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
} }
if(RadioButtonList1 .Text ="教师")
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [teacher] where tno='" + TextBox1.Text + "' and tname='" + TextBox2.Text + "'and tdept='" + TextBox3.Text + "'and tpwd='"+TextBox4 .Text +"'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write(" <script>alert('登录成功!'); </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
}
}
if(RadioButtonList1 .Text ="管理员")
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [admin] where ano='" + TextBox1.Text + "'and ananme='"+ TextBox2 .Text +"'and adept='"+ TextBox3 .Text + "' and apwd='" + TextBox4.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write(" <script>alert('登录成功!'); </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
}
}
而一个等号(=)是赋值运算符。
2、跳转不成功,是不是连接地址不正确啊
location.href='../student/学主页.aspx' ?还是location.href='./student/学主页.aspx'
{
}
else if(RadioButtonList1.Text == "教师")
{
}
else if(RadioButtonList1.Text == "管理员")
{
}
else {
throw new Exception("Unkown: " + RadioButtonList1.Text);
}4. 请确保 Connection 是关闭的,比如使用 usingusing(SqlConnection conn = new SqlConnection()) {
conn.ConnectionString = "data source=.;database=school;uid=sa;pwd=";
string sql = "select *from [student] where sno='" + TextBox1.Text + "' and sname='" + TextBox2.Text + "'and sdept='"+ TextBox3 .Text +"'and spwd='" + TextBox4.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = null;
conn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["sname"] = dr["sname"].ToString();
Session["sno"] = dr["sno"].ToString();
Response.Write(" <script>alert('登录成功!');location.href='../student/学主页.aspx' </script>");
}
else
{
Response.Write(" <script>alert('登录信息有误!无法登录!') </script>");
}
}5. 同学,你需要掌握的知识太多太多了 .... 先多多看书,再动手吧