我使用文本框創建了一個基本的登錄頁面。我正在驗證創建的數據庫中的用戶。下面是我的後端代碼:登錄頁面沒有正確重定向
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlDataReader sdrDatanew = null; //Read rows one by one.
string strnew;
string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString; //Define new connectionstring
SqlConnection connew = new SqlConnection(connectionString); //establishes a new sql connection
connew.Open(); //Opens Connection
strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'";
SqlCommand sqlCommnew = new SqlCommand(strnew, connew); //passes the command and connection
sdrDatanew = sqlCommnew.ExecuteReader(); //For select command
int userType = 0;
if (sdrDatanew.HasRows)
{
if (sdrDatanew.Read())
{
userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString());
}
}
switch (userType)
{
case 0:
Response.Redirect("Lic_Gen.aspx");
break;
case 1:
Response.Redirect("Cust_Page.aspx");
break;
default:
lblDisp.Text= "Invalid User/Password";
break;
}
connew.Close();
}
這是我的前端代碼:
<div>
<h2 class="style12">
LOGIN</h2>
<p class="style10">
</p>
<table align="center" cellpadding="2" cellspacing="5" style="border-width: thick;
border-style: outset; height: 195px;" class="style14">
<tr>
<td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif;
font-size: 17.5px; font-weight: bold; font-style: normal">
User Type</td>
<td class="style17">
<asp:DropDownList ID="ddlUserSel" runat="server" Height="25px" Width="260px" CssClass="drp">
<asp:ListItem Value="0">Admin</asp:ListItem>
<asp:ListItem Value="1">Customer</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif;
font-size: 17.5px; font-weight: bold; font-style: normal">
Login
</td>
<td class="style17">
<asp:TextBox ID="txtUserName" runat="server" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqUserName" runat="server" ErrorMessage="*" ForeColor="Red"
ControlToValidate="txtUserName" Font-Size="Large"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style16" style="font-family: 'Times New Roman', Times, serif; font-size: 17.5px;
font-weight: bold; font-style: normal">
Password
</td>
<td class="style17">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqPassword" runat="server" ErrorMessage="*" ForeColor="Red"
ControlToValidate="txtPassword" Font-Size="Large"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblDisp" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" CssClass="btn1" Text="Submit"
OnClick="btnSubmit_Click" Width="80px"/>
<asp:Button ID="btnCancel" runat="server" CssClass="btn1" Text="Cancel"
OnClick="btnCancel_Click" Width="80px"/>
</td>
</tr>
</table>
</div>
現在我有以下問題:
- 無論用戶類型,總頁面被重定向到
Lic_Gen.aspx
。 - 即使我輸入了錯誤的密碼,頁面仍然被重定向到
Lic_Gen.aspx
。 - 該頁面還應檢查是否輸入了userType,即
Admin
或Customer
應該與該特定的Login ID
和Password
一致。但我也不明白。
我知道這些問題正在到達,因爲我已明確將UserType
的值定義爲0
。
當我試圖用下面的代碼轉換它: int userType = Convert.ToInt32(strnew);
它不起作用。
那麼有關如何改進我的基本頁面的任何指導?
你有沒有測試是否來自數據庫的值是正確的?請使用F10或F11跟蹤代碼,並使用手錶查看這些值。 – Dev
是。我做到了。我在'strnew'處添加了一個斷點。 User_Type存儲的值即使爲'1',仍由於'UserType = 0'的明確定義而中斷它,並且交換機在情況0時着陸。 – Esha