Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi,

I am new to asp.net. So just learning everything step by step.

I have created a login page and now on button click on login, i want the page to redirect it to next page "change password" page.

I have written the code, but its not redirecting to next page.

I will post the code here.
can anyone help me,where I am wrong. I may be wrong as I am new to this language.

Waiting for your help
Thanks
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data


Public Class Login1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("loginConnectionString").ConnectionString)
        con.Open()
        Dim cmd As New SqlCommand("select * from log where username =@username and Pass=@password", con)
        cmd.Parameters.AddWithValue("@username", TextBox1.Text)
        cmd.Parameters.AddWithValue("@password", TextBox2.Text)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            Response.Redirect("ChangePassword.aspx")
        Else
            ClientScript.RegisterStartupScript(Page.[GetType](), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>")
        End If

    End Sub

   
End Class


this is my html code
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Login.aspx.vb" Inherits="WebApplication2.Login1" %>

<!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>
    <style type="text/css">
        .style1
        {
            font-size: medium;
            font-family: Dotum;
        }
        .style2
        {
            color: #FFFFFF;
            background-color: #3333FF;
        }
        .style3
        {
            width: 55%;
            height: 69px;
            margin-top: 33px;
        }
        .style4
        {
            width: 152px;
        }
        .style5
        {
            width: 190px;
        }
    </style>
</head>
<body style="height: 226px">
    <form id="form1" runat="server">
    <div class="style1">
    
                                 
        <span class="style2">Login Page<br />
        </span>
    
    </div>
    <table class="style3">
        <tr>
            <td class="style4">
                UserName</td>
            <td class="style5">
                <asp:TextBox ID="TextBox1" runat="server" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="TextBox1" ErrorMessage="You must enter Username..!!!" 
                    ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Password</td>
            <td class="style5">
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ControlToValidate="TextBox2" ErrorMessage="You must enter the password ...!!" 
                    ForeColor="Red"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style4">
                 </td>
            <td class="style5">
                <asp:Button ID="Button1" runat="server" BackColor="Blue" ForeColor="White" 
                    Text="Login" Width="100px" />
            </td>
            <td>
                 </td>
        </tr>
        <tr>
            <td class="style4">
                 </td>
            <td class="style5">
                 </td>
            <td>
                <asp:HyperLink ID="HyperLink1" runat="server" 
                    NavigateUrl="~/Account/Register.aspx">New User Register Here</asp:HyperLink>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
Posted
Updated 6-Feb-20 18:41pm
v2
Comments
Yuvaraj Arasu 25-Sep-13 23:52pm    
This is not redirecting-OK, But what's happenning ? like Anything displayed on screen as error or the same page still reloads.. etc.,
Check the ChangePassword.aspx page also resides in the same directory in which your login1.aspx page resides.
sudeshna from bangkok 25-Sep-13 23:55pm    
Yes the ChangePassword.aspx and login.aspx are in same account directory
sudeshna from bangkok 25-Sep-13 23:57pm    
sorry this url is showing but the page is not getting redirected

http://localhost:13523/Account/Login.aspx?ReturnUrl=%2fAccount%2fChangePassword.aspx

use Response.Redirect("~/ChangePassword.aspx"),if it is in root directory. if it is inside any directory, then use directory name like Response.Redirect("~/Account/ChangePassword.aspx")
 
Share this answer
 
Comments
sudeshna from bangkok 26-Sep-13 0:39am    
I did but same.no change. is anything wrong with my code? in the if statement? please can you re check it.
Hi sudeshna from bangkok,

If you want to redirect to any page in the same web site we can use two ways.

1. Response.Redirect(" Here is the complete path of the destination page");
2. Server.Transfer("Here is the complete path of the destination page");

There is slight difference in between Response.Redirect and  Server.Transfer.
 
Share this answer
 
Comments
sudeshna from bangkok 26-Sep-13 0:40am    
I used response.redirect with full path, but no change,page is not getting redirected to other page
[no name] 26-Sep-13 0:53am    
what you are getting after executing Response.Redirect statement.
sudeshna from bangkok 26-Sep-13 0:59am    
same login page is displaying
[no name] 26-Sep-13 1:13am    
first debug your code and send me the same redirecting method code.
[no name] 26-Sep-13 1:04am    
Could you post your c# Redirecting code now?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900