-1

I have write a below code in ASP.net to redirect Default page after login successful,but response.write function is not working.Please check below code and advise how to do this...

 protected void LogIn_Clicked(object sender, EventArgs e)
    {
        DataTable Login = new DataTable();

        string username = Request.Form["username"];
        string password = Request.Form["password"];
        bool remember = RememberMe.Checked;

        Login = Db.SetCommand("Select * from KhArind.Dbo.UserMaster Where UserName = '" + username + "' And Password = '" + password + "' ").ExecuteDataTable();

        if (Login.Rows.Count > 0)
        {
            Session["UserName"] = Login.Rows[0]["UserName"];
            Session["UserId"] = Login.Rows[0]["UserId"];
            Response.Redirect("~/Default.aspx",false);
        }
        else
        {
            ErrorMessage.Text = "Invalid UserName or Password";
            ErrorMessage.Visible = true;
        }
    }

Please check aspx code in below link

https://www.dropbox.com/s/igjlylzqe8fbcq0/ASPXCode.txt?dl=0

ScreenShot for Browser :-

enter image description here

KH IT
  • 9
  • 5

2 Answers2

0

Before going to the page transferring methods make sure that you have Ispostback is enabled and in page load following section is there or not

private void Page_Load()
{
    if (!IsPostBack)
    {
        //your code
    }
} 

Now try with redirection method

Response.Redirect("Default.aspx", true);

Still the Response.redirect is not working. Then go to design view select the button which you want to set Response.redirect and press F4 that will open Button Properties then you need to find PostBackUrl option and then locate your URL that you want.

PostackURL

Then locate the file

Files

See the correct path of your page and add the same on your redirect method

enter image description here

AcAnanth
  • 702
  • 1
  • 16
  • 49
  • please check attached screenshot for browser – KH IT Aug 04 '20 at 12:26
  • @KH IT : instead of calling at the end add the response.direct at the top of your code and check whether the navigation is working or not. – AcAnanth Aug 04 '20 at 18:49
  • I have recheck again in browser,it shows an 302 status code (response).Please advise how to do this... – KH IT Aug 05 '20 at 03:55
  • i think the problem is with your aspx file location, can you add the project folder structure in the question? – AcAnanth Aug 05 '20 at 09:44
0
  1. Use Response.Redirect("Default.aspx", false);
  2. If at root, use "~/", Use Response.Redirect("~/Default.aspx", false);
  3. The Transfer method is typically more efficient because it does not cause a round trip to the client. Server.Transfer("Default.aspx", True)
Rajdeep Debnath
  • 1,172
  • 5
  • 12
  • I have tried all above solution but still I am facing the same issue.Actually I have changed the visual studio and windows version.Is there any issue for upgrading windows and visual studio ??? – KH IT Aug 04 '20 at 11:03
  • no it should not cause any issue, which asp.net version are you using? .net framework 4.0 or 5.2 etc? – Rajdeep Debnath Aug 04 '20 at 11:03
  • I was using Visual Studio 2015, but now I am using Visual Studio 2017 with 4.5.2 framework and windows version is windows 10 – KH IT Aug 04 '20 at 11:07
  • Okay, can you set a breakpoint and check if the execution is going inside `if (Login.Rows.Count > 0)` this if block – Rajdeep Debnath Aug 04 '20 at 11:12
  • yes its going but the problem is why response.redirect not executing ?? – KH IT Aug 04 '20 at 12:02
  • Okay, so next What's the happening after this `Response.Redirect("~/Default.aspx",false);`? What are you seeing in browser? any exception? Do you have 'Default.aspx' or any spelling error? Can you wrap the line in try catch and check if any exception is being thrown? – Rajdeep Debnath Aug 04 '20 at 12:17