当前位置:编程学习 > C#/ASP.NET >>

ASP.NET4.0后台登陆代码,登陆不成功!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace BookStore
{
    public partial class Default : System.Web.UI.Page
    {


        private void Page_Load(Object sender, System.EventArgs e)
        {
            //调用数据绑定函数
            if (!IsPostBack)
                BindDataGrid();
        }
        //响应分页显示换页事件
        void Page_Change(object sender, DataGridPageChangedEventArgs E)
        {
            DgdBook.CurrentPageIndex = E.NewPageIndex;
            BindDataGrid();
        }
        public void BindDataGrid()
        {
            //从文件 Web.config中读取连接字符串
            string strconn = ConfigurationSettings.AppSettings["DB"].ToString();
            SqlConnection cn = new SqlConnection(strconn);
            string mysql = "select ID,Name,Author,Publisher,Price from Book where Type=01 order by ID desc";
            //创建SqlDataAdapter对象
            SqlDataAdapter da = new SqlDataAdapter(mysql, cn);
            //创建DataSet对象
            DataSet ds = new DataSet();
            da.Fill(ds);
            DgdBook.DataSource = ds;
            DgdBook.DataBind();
            cn.Close();
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            //从文件Web.config 中读书连接字符串
            string strconn = ConfigurationSettings.AppSettings["DB"].ToString();
            SqlConnection cn = new SqlConnection(strconn);
            cn.Open();
            string mysql = "select*from UserInfo where UserID=`" + TextBox_ID.Text + "`  and Password=" + TextBox_PW.Text + "`";
            SqlCommand cm = new SqlCommand(mysql, cn);
            SqlDataReader dr = cm.ExecuteReader();
            if (dr.Read())
            {
                Session["UserID"] = dr["UserID"];
                Session["Rigth"] = dr["Rigth"];
                if (Session["Rigth"].ToString() == "1")
                {
                    Response.Redirect("Bookmanage.aspx");
                }
                else
                {
                    Response.Redirect("BookShow.aspx");
                }
            }
            cn.Close();
        }
        private void LinKbutton1_Click(Object sender, System.EventArgs e)
        {
            Response.Redirect("Register.aspx");


        }
    }
}

怎么解决! --------------------编程问答--------------------   string mysql = "select*from UserInfo where UserID=`" + TextBox_ID.Text + "` and Password=" + TextBox_PW.Text + "`";

缺空格. --------------------编程问答--------------------   string mysql = "select*from UserInfo where UserID=`" + TextBox_ID.Text + "` and Password=" + TextBox_PW.Text + "`";
还有, 你是否是用的单引号包括 userid 和password的. 我打出来的单引号是 ' 的啊. 和你的不同. --------------------编程问答-------------------- '"++"' string/double/foat 都得这样, int 型可以"++" --------------------编程问答-------------------- string mysql = "select*from UserInfo where UserID=`" + TextBox_ID.Text + "` and Password=" + TextBox_PW.Text + "`";
改为
string mysql = "select*from UserInfo where UserID=" + TextBox_ID.Text + " and Password=" + TextBox_PW.Text + "";
--------------------编程问答--------------------
  string mysql = "select*from UserInfo where UserID=`" + TextBox_ID.Text + "` and Password=" + TextBox_PW.Text + "`";

=====》

tring mysql = "select * from UserInfo where UserID=`" + TextBox_ID.Text + "` and Password= '" + TextBox_PW.Text + "'";

我猜你Password应该不是个整型的吧。。。字符串类型的要加单引号 --------------------编程问答-------------------- 断点调试  
别 一大堆的代码 丢给别人问原因

你告诉别人在哪一行
--------------------编程问答--------------------
加个try catch捕获一下。下次就知道错误所在了

补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,