Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Session("constr") = "data source=" + Server.MapPath("/") + "workdb/Nur_home.mdb;provider=Microsoft.JET.OLEDB.4.0;Jet OLEDB:Database Password=bwjad"
If Not Page.IsPostBack Then wrongmsg.Visible = False
'Dim msg = Request.QueryString("msg")
'If msg = "relogin" Then
' 'wrongmsg.Text = "System has thought your leaving for long time,please login again!"
' wrongmsg.Visible = True
'End If
Else
wrongmsg.Visible = True
End If If check_reg() <> 0 Then
Imagebutton2.Visible = False
End If
End Sub
Function check_reg()
Dim con As OleDbConnection
Dim dad As OleDbDataAdapter
Dim dst As New DataSet()
Dim dtb As DataTable
Dim drow As DataRow con = New OleDbConnection(Session("constr"))
dad = New OleDbDataAdapter("select * from register where flag=1", con)
dad.Fill(dst, "t")
dtb = dst.Tables("t")
Return dtb.Rows.Count
End Function
Function check_status()
Dim obj As StreamReader
Dim str As String
Dim i As Integer
Dim d As DateTime
If check_reg() <> 0 Then
Exit Function
End If
If File.Exists(Server.MapPath("dd.txt")) Then
obj = File.OpenText(Server.MapPath("dd.txt")) str = obj.ReadLine()
d = CDate(str)
i = DateDiff(DateInterval.Day, d, DateTime.Now) 'Response.Write(i)
' Response.End() If i < 14 And i >= 0 Then Response.Write("<script language=javascript>alert('You have " & 14 - i & " days to use the software!')</script>")
Else
Response.Write("<script language=javascript>alert('The tryout period has passed, please pay the licence and get registered!')</script>")
' Response.Write("<script language=javascript>window.open('login.aspx','_top');</script>")
'Response.End()
End If
Else
Dim objfile As StreamWriter
objfile = File.CreateText(Server.MapPath("dd.txt"))
objfile.WriteLine(DateTime.Now.ToString())
objfile.Close()
Response.Write("<script language=javascript>alert('You have 14 days to use the software!')</script>")
End If
End Function
'Put user code to initialize the page here
Session("constr") = "data source=" + Server.MapPath("/") + "workdb/Nur_home.mdb;provider=Microsoft.JET.OLEDB.4.0;Jet OLEDB:Database Password=bwjad"
If Not Page.IsPostBack Then wrongmsg.Visible = False
'Dim msg = Request.QueryString("msg")
'If msg = "relogin" Then
' 'wrongmsg.Text = "System has thought your leaving for long time,please login again!"
' wrongmsg.Visible = True
'End If
Else
wrongmsg.Visible = True
End If If check_reg() <> 0 Then
Imagebutton2.Visible = False
End If
End Sub
Function check_reg()
Dim con As OleDbConnection
Dim dad As OleDbDataAdapter
Dim dst As New DataSet()
Dim dtb As DataTable
Dim drow As DataRow con = New OleDbConnection(Session("constr"))
dad = New OleDbDataAdapter("select * from register where flag=1", con)
dad.Fill(dst, "t")
dtb = dst.Tables("t")
Return dtb.Rows.Count
End Function
Function check_status()
Dim obj As StreamReader
Dim str As String
Dim i As Integer
Dim d As DateTime
If check_reg() <> 0 Then
Exit Function
End If
If File.Exists(Server.MapPath("dd.txt")) Then
obj = File.OpenText(Server.MapPath("dd.txt")) str = obj.ReadLine()
d = CDate(str)
i = DateDiff(DateInterval.Day, d, DateTime.Now) 'Response.Write(i)
' Response.End() If i < 14 And i >= 0 Then Response.Write("<script language=javascript>alert('You have " & 14 - i & " days to use the software!')</script>")
Else
Response.Write("<script language=javascript>alert('The tryout period has passed, please pay the licence and get registered!')</script>")
' Response.Write("<script language=javascript>window.open('login.aspx','_top');</script>")
'Response.End()
End If
Else
Dim objfile As StreamWriter
objfile = File.CreateText(Server.MapPath("dd.txt"))
objfile.WriteLine(DateTime.Now.ToString())
objfile.Close()
Response.Write("<script language=javascript>alert('You have 14 days to use the software!')</script>")
End If
End Function
Dim uid As String = Trim(username.Text)
Dim pwd As String = Trim(passwd.Text)
Dim str_user As String
If uid.IndexOf("'") >= 0 Or pwd.IndexOf("'") >= 0 Then
wrongmsg.Text = "Wrong username or password!"
wrongmsg.Visible = True
Else Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet = New DataSet()
Dim sql As String = "select * from users where User_name ='" & uid & "' and User_pwd ='" & pwd & "'"
conn = New OleDbConnection(Session("constr"))
da = New OleDbDataAdapter(sql, conn)
da.Fill(ds, "users")
If ds.Tables("users").Rows.Count = 0 Then
wrongmsg.Text = "Wrong username or password!"
ElseIf ds.Tables("users").Rows.Count > 0 Then
check_status()
'If check_reg() = 0 Then
' Response.Write("<script language=javascript>alert('The tryout period has passed, please pay the licence and get registered!')</script>")
'End If If Not IsDBNull(ds.Tables("users").Rows(0).Item("authority")) Then
Select Case ds.Tables("users").Rows(0).Item("authority")
Case "user"
Session("user") = "user"
Case "admin"
Session("user") = "admin"
End Select
str_user = ds.Tables("users").Rows(0).Item("user_name")
FormsAuthentication.RedirectFromLoginPage(str_user, False) 'Response.Write("<script language=javascript>window.open('default.aspx','_top');</script>")
'Response.End()
End If
End If End If
End Sub
myconn.Open()
Dim userid As String
Dim sql As String = "select * from yonghu where name='" + name.Text + "' and pass ='" + pass.Text + "'"
Dim showbm As New OleDbCommand(sql, myconn)
Dim i As Integer = 0
Dim reader As OleDbDataReader = showbm.ExecuteReader
While reader.Read()
i = i + 1
End While reader.Close()
myconn.Close()
If (i > 0) Then Session("name") = name.Text 'Request("name") = "admin" Then
FormsAuthentication.SetAuthCookie(name.Text, False)
If name.Text = "admin" Then
Response.Redirect("main.aspx")
ElseIf name.Text = "hzdm" Then
Response.Redirect("main.aspx")
End If
Else Response.Write("<script>alert('不正确用户名或密码!')</script>")
name.Text = ""
pass.Text = ""
Exit Sub
End If
dim query as string
dim conn as sqlconnection=new sqlconnection(connstr)
dim command as sqlcommand
dim reader as sqldatareadertry
conn.open()
query="select * from youtable where Id='" & txtid.text & "' and password='" & txtpwd.text & "'"
command=new sqlcommand(query)
reader=command.executereader()
if reader.read
print "登入成功"
else
print "faile"
end if
finally
reader.close()
conn.close()
end try
{
SqlDataReader sdr;
SqlConnection myconn=new SqlConnection (strMyConn);
string strsql="select * from myuser where name='"+user+"'"+"And Password='"+password+"'";
SqlCommand mycomm=new SqlCommand (strsql,myconn);
myconn.Open ();
sdr=mycomm.ExecuteReader();
if(sdr.Read ()==true)
{
return true;
}
else
{
return false;
}
}
sqlstr += nametext.Text + "' and YHMM = '" + password + "'";
OleDbConnection objConnection = new OleDbConnection(connstr);
OleDbCommand objCommand = new OleDbCommand(sqlstr,objConnection);
OleDbDataReader objDataReader = null;
try
{
objConnection.Open();
objDataReader = objCommand.ExecuteReader ();
}
catch
{
this.Response.Redirect("database-error.htm");
}
if(objDataReader.Read())
{
//假设用户名和密码存放在名为 UserInfo 的表内。
//假设用户输入用户名和密码的文本框为:
//用户名输入框:txtUserName 密码输入框:txtPassword //下面是代码,关于用户登录检查的方法很多,此处只用一种比较简单的方法 //创建数据库连接 (此处使用Sql Server)
SqlConnection conn=new SqlConnection("数据库连接字符串");
SqlCommand cmd=new SqlCommand();
cmd.Connection=conn; //设置获取用户信息的查询字符串
cmd.CommandText="select UserName,Password from UserInfo where UserName=@UserName";
cmd.Parameters.Add("@UserName",txtUserName.Text.Trim()); //执行命令,返回用户信息
using(SqlDataReader rdr=cmd.ExecuteReader())
{
//开始校验用户信息
if(rdr.Read())
{
if(rdr["Password"]==txtPassword.Text.Trim())
{
//用户名和密码正确
//可以登录
//登录代码、设置cookie,等等。
}
else
{
//密码错误
}
}
else
{
//错误:用户名不存在
}
} conn.Close();
}
登陆按钮代码:
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
string ip= System.Web.HttpContext.Current.Request.UserHostAddress ;
string user_name=user_tb.Text;
string user_psw=psw_tb.Text;
user_name=user_name.Replace("<","<").Replace(">",">").Replace(" "," ").Replace("'","‘");
user_psw=user_psw.Replace("<","<").Replace(">",">").Replace(" "," ").Replace("'","‘");
if(user_name!=""||user_psw!="")
{
SqlConnection myconn=new SqlConnection(Application["strconn"].ToString());//连接数据库
myconn.Open();//打开
string validate_Sql="select * from Web_User where User_Name='"+user_name+"'and User_Psw='"+user_psw+"'";
SqlCommand validate_com=new SqlCommand(validate_Sql,myconn);
SqlDataReader validate=validate_com.ExecuteReader();
string temp="";
while(validate.Read())
{
Session["user_name"]=user_name;
temp="yes";
}
validate.Close();
if(temp=="yes")
{
user_tb.Text="";
psw_tb.Text="";
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(user_name,false);
Response.Redirect("/picc/manager/manage_index.aspx"); }
else
{
Response.Write("<script>alert('您的用户名或密码错误!');</script>");
return ;
}
user_tb.Text="";
psw_tb.Text="";
myconn.Close();
}
}你对号入座改一下字段和连接就行了!嘿嘿!多放点分呀!
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 54: End If
行 55: Finally
行 56: reader.Close()
行 57: conn.Close()
行 58: End Try
源文件: c:\inetpub\wwwroot\Myweb\WebForm1.aspx.vb 行: 56 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Myweb.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Myweb\WebForm1.aspx.vb:56
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()