Я разработал страницу входа и подключился к моей базе данных aws rds через mysql workbench. Я создал таблицы и могу вставить новую регистрацию пользователя. но для login.aspx, я пишу те же коды для login.aspx.cs, что и для подключения к локальной базе данных, но мой не работает, и я не забираю меня на домашнюю страницу при нажатии кнопки входа. Может ли кто-нибудь помочь мне найти то, что я делаю неправильно? Заранее спасибо! здесь мой 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"> </td>
<td class="auto-style4">
<asp:label runat="server"> <b> UserID </b> </asp:label>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
<asp:TextBox id="Txtid" CssClass="auto-style2" placeholder="Enter UserID" runat="server" Height="42px" Width="55%"/></td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </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> </td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
<asp:label runat="server"> <b> Password </b> </asp:label>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </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> </td>
</tr>
<tr>
<td class="auto-style3"> </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> </td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
<asp:Checkbox ID="Chkrmb" CssClass="checkbox-inline" Text="Remember me" runat="server" /></td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </td>
<td class="auto-style4">
<asp:Hyperlink ID="MyHyperLinkControl" NavigateUrl="~/forgetpsw.aspx" runat="server">Forget password? </asp:Hyperlink>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style3"> </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> </td>
</tr>
</table>
</div>
</form>
Файл login.aspx.cs:
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!");
}
}
}
}
Вот мои обновленные коды login.aspx.cs, если кто-то хочет знать;
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!";
}