Kompilasi Kode

ASP .Net Membuat Form Login

3 Comments

Melanjutkan posting sebelumnya, kali ini kita akan mencoba membuat form loginnya, langsung saja tambahkan Web Form di project kita (FormLogin.aspx) buat tampilannya seperti di bawah ini

form login

kemudian ketik kode programnya di FormLogin.aspx.cs seperti ini

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace UserRegistration
{
public partial class FormLogin : System.Web.UI.Page
{
private SqlConnection con;
private SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=LENOVO-PC\\SQLEXPRESS;Initial Catalog=asp_db;Integrated Security=True");
con.Open();
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("SELECT * FROM users WHERE username=@username AND password=@password", con);
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count > 0)
{
Response.Redirect("Welcome.aspx?user="+txtUsername.Text);
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}
}

Ketika proses login berhasil (username dan password sesuai dengan yang ada di Database) user akan di arahkan ke halaman Welcome.aspx, tetapi apabila login gagal akan muncul pesan kesalahan, untuk kode halaman Welcome.aspx dan Welcome.aspx.cs seperti di bawah ini

Welcome.aspx

</pre>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="UserRegistration.Welcome" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 Selamat datang <asp:Label ID="txtWelcomeUser" runat="server" /> !!
 </div>
 </form>
</body>
</html>

Welcome.aspx.cs

</pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace UserRegistration
{
 public partial class Welcome : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
 //NameValueCollection nvc = Request.Form;
 String s = Request.QueryString["user"];
 if (!string.IsNullOrEmpty(s))
 {
 txtWelcomeUser.Text = s;
 }
 }
 }
}
<pre>
About these ads

3 thoughts on “ASP .Net Membuat Form Login

  1. Gan, kode if (dt.Rows.Count > 0) gak jadi, gmn tuh??

  2. “<script language=’javascript’>alert(‘Invalid Username and Password’)</script>”); ini juga gan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.