Home aspx.net login page with rds database via Mysql workbench
Reply: 1

aspx.net login page with rds database via Mysql workbench

Chan Myae Tun
1#
Chan Myae Tun Published in 2018-01-13 01:13:28Z

I have developed a login page and connected to my aws rds database via mysql workbench. I have created tables and can insert new registration of user. but for the login.aspx, I am writing the same codes for login.aspx.cs like local database connection but mine doesn't seem working and it is not taking me to home page when I click login button. Can anyone help me find what I am doing wrong? Thank you in advance! here's my login.aspx;

     <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="loginfinal.aspx.cs" Inherits="Aname.login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="Scripts/moment.min.js"></script>
<script src="Scripts/bootstrap-datetimepicker.min.js"></script>

</head>
<body>
    <form id="form1" runat="server">

            <asp:Image runat="server" imageURL="~/images/PlantManager.jfif" width="200px"/>
            <asp:Image ImageAlign="Middle" runat="server" imageURL="~/images/quote.jfif" height="128px" />
            <asp:Image runat="server" imageURL="~/images/TeamLogo.jfif" width="126px"/>


            <div style="text-align:right">
               <asp:Hyperlink ID="Hyperlink1" NavigateUrl="AboutUsPM.aspx" runat="server">About Plant Manager </asp:Hyperlink>

            </div>

            <div class="form-group">
            <table class="auto-style1">
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">


            <asp:label runat="server"> <b> UserID </b> </asp:label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
            <asp:TextBox id="Txtid" CssClass="auto-style2" placeholder="Enter UserID" runat="server" Height="42px" Width="55%"/></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Txtid" Display="Dynamic" ErrorMessage="You cannot leave this blank." ForeColor="Red"></asp:RequiredFieldValidator>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:label runat="server"> <b> Password </b> </asp:label>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
            <asp:TextBox id="TxtPsw" CssClass="auto-style5" Textmode="Password" placeholder="Enter Password" runat="server" Height="42px" Width="55%"/></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TxtPsw" Display="Dynamic" ErrorMessage="You cannot leave this blank." ForeColor="Red"></asp:RequiredFieldValidator>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Checkbox ID="Chkrmb" CssClass="checkbox-inline" Text="Remember me" runat="server" /></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

                        &nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Hyperlink ID="MyHyperLinkControl" NavigateUrl="~/forgetpsw.aspx" runat="server">Forget password? </asp:Hyperlink>

                    </td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">

            <asp:Button ID="Btnlogin" CssClass="auto-style6" Text="Login" OnClick="Btnlogin_Click" style="color:white" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" runat="server" />


                    </td>
                    <td>&nbsp;</td>
                </tr>
                </table>


            &nbsp;</div>

</form>

login.aspx.cs file:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

namespace Aname
{
    public partial class login : System.Web.UI.Page
    {
//rds connection string
        static string connection = @"Server=rds-mysql.xxxxxxxx.us-west-2.rds.amazonaws.com; Port=xxxx; Database=mydb; User Id=xxxx; password=xxxxx";
    MySqlConnection sqlcon = new MySqlConnection(connection);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData("");

        }
    }

    private void LoadData(string text)
    {
        DataTable dt = new DataTable();
        using (MySqlConnection sqlquery = new MySqlConnection(connection))
        {
            string query = "SELECT user_id, name, contact_no, email, role FROM mydb.User ; ";
            MySqlDataAdapter adpter = new MySqlDataAdapter(query, sqlquery);
            adpter.Fill(dt);
        }
        if (dt.Rows.Count > 0)
        {
            Session["user_id"] = Txtid.Text;
            Response.Redirect("~/home.aspx");
        }
    }
    protected void Btnlogin_Click(object sender, EventArgs e)
    {
        sqlcon.Open();
        string query = "select count(*) from mydb.User where user_id='" + Txtid.Text + "'and pass='" + TxtPsw.Text + "'";

        MySqlCommand cmd = new MySqlCommand(query, sqlcon);
        int output = Convert.ToInt32(cmd.ExecuteScalar().ToString());
        sqlcon.Close();

        if (output == 1)
        {
            sqlcon.Open();
            string checkPasswordQuery = "select password from mydb.User where user_id='" + Txtid.Text + "'";
            MySqlCommand checkpsw = new MySqlCommand(query, sqlcon);
            string password = checkpsw.ExecuteScalar().ToString();

            if (password == TxtPsw.Text)
            {
                Session["user_id"] = Txtid.Text;
                Response.Redirect("~/home.aspx");
            }
            else
            {
                Response.Write("Password is incorrect!");
            }
        }
        else
        {
            Response.Write("UserID is incorrect!");
        }

     }

}
}
Chan Myae Tun
2#
Chan Myae Tun Reply to 2018-01-14 02:39:24Z

Here is my updated login.aspx.cs codes if anyone wants to know;

     public partial class login : System.Web.UI.Page
{
    static string connection = @"Server=rds-mysql.xxxxxxxx.us-west-2.rds.amazonaws.com; Port=xxxx; Database=mydb; User Id=xxxx; password=xxxxx";
    MySqlConnection sqlcon = new MySqlConnection(connection);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            LoadData("");
        }
    }

    private void LoadData(string text)
    {
        sqlcon.Open();
        string query = "select * from mydb.User where user_id='" + Txtid.Text + "'";

        MySqlCommand cmd = new MySqlCommand(query, sqlcon);
        try
        {
            string output = cmd.ExecuteScalar().ToString();
            string checkPasswordQuery = "select password from mydb.User where password='" + TxtPsw.Text + "'";

            MySqlCommand checkpsw = new MySqlCommand(checkPasswordQuery, sqlcon);
            string password = null;
            try
            {
                password = checkpsw.ExecuteScalar().ToString();
                Session["user_id"] = Txtid.Text;
                Response.Redirect("~/home.aspx");
            }
            catch (Exception e)
            {
                Ltlup.Text = "UserID or password is incorrect!";
            }
        }
        catch(Exception e)
        {
            Ltlup.Text = "UserID or password is incorrect!";
        }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338186 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO