Asp.net 1.1 and Javascript problem..need help

Last post 05-14-2008 1:29 AM by su.rameshraj. 3 replies.

Sort Posts:

  • Asp.net 1.1 and Javascript problem..need help

    04-28-2008, 4:43 AM
    • Member
      10 point Member
    • su.rameshraj
    • Member since 09-18-2007, 7:10 AM
    • Posts 61

     Need help in ASP.NET 1.1 and Javascript related issue: look at the code below (.cs file). Its a login screen where in I have change password option. The problem is after user has submitted the old username, old password and new password, I am getting error alert that "Invalid old password", but if I look in the database..password has been changed successfully. To my knowledge problem lies in "private void ImageButton1_Click" (Its a change password image button id).  Even though I have commented if(this.IsPostBack)  the control loops back ( I don't know why) ..and at this time there won't be old or new password and hence the alert"Invalid Password" is thrown. Can some one findout why this is happening? what is the solution for this problem?

    .cs file:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Security;
    using System.Configuration;
    using System.Net;
    using System.Data.OleDb ;
    using System.Data.Common;

    namespace SpiGurukul
    {
       
        public class ChangePassword : SpiGurukulPage
        {
            protected int logintypeid;
            private System.Data.OleDb.OleDbConnection Con;
            private string connectionstring;
            private string sqlStmt;
            int pwd,Uname,eid;
            int i=0;
            public string temp="";
            protected System.Web.UI.WebControls.Label Label3;
            protected System.Web.UI.WebControls.TextBox TxtNewPwd;
            protected System.Web.UI.WebControls.TextBox TxtCompareNewPwd;
            protected System.Web.UI.WebControls.TextBox TxtPwd;
            protected System.Web.UI.WebControls.Label Label2;
            protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.TextBox TxtUserID;
            protected System.Web.UI.WebControls.Image Image1;
            protected System.Web.UI.WebControls.ImageButton ImageButton1;
            protected System.Web.UI.WebControls.ImageButton ImageButton2;
            protected System.Web.UI.WebControls.Label Label4;
       
            private void Page_Load(object sender, System.EventArgs e)
            {
               
                if(Request.QueryString["Name"]!=null)
                    {
                        TxtUserID.Text = Request.QueryString["Name"];
                    }
                else
                    {
                        if(Request.Form["TxtUserID"] != null)
                            {
                                Response.Write("vinay13");
                            }
                    }
               
                connectionstring = ConfigurationSettings.AppSettings["ConnectionString"];
            }

            private int ChkAuthentication()
            {       
                try
                {
                    Con = new OleDbConnection(connectionstring);           
                    sqlStmt = "";
                    sqlStmt += "SELECT LoginId, ";
                    sqlStmt += "       COALESCE( EmpId, -1 ) AS EmpId";
                    sqlStmt += "  FROM Login ";
                    sqlStmt += " WHERE LoginId = '" + TxtUserID.Text.Trim() + "' ";
                    sqlStmt += "   AND Password = '" + TxtPwd.Text.Trim() + "'";
                    System.Data.OleDb.OleDbCommand Cmd = new OleDbCommand(sqlStmt, Con);
                    Cmd.Connection.Open();
                    OleDbDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.Read())
                        {  
                            return Convert.ToInt32(dr["EmpId"].ToString());
                        }
                }
                catch( Exception ex )
                    {
                        Response.Write( "There was an error processing the request.<BR><BR>Error Details:<BR>" + ex.Message );
                        Response.End();
                    }
                finally
                    {
                        if( Con.State == ConnectionState.Open )
                            {
                                Con.Close();
                            }
                    }
                return -1;
            }
            private int ChkPwd()
            {       
                try
                {
                    Con = new OleDbConnection(connectionstring);           
                    sqlStmt = "";
                    sqlStmt += "SELECT LoginId, ";
                    sqlStmt += "       COALESCE( EmpId, -1 ) AS EmpId";
                    sqlStmt += "  FROM Login ";
                    sqlStmt += " WHERE Password = '" + TxtPwd.Text.Trim() + "'";                               
                    System.Data.OleDb.OleDbCommand Cmd = new OleDbCommand(sqlStmt, Con);
                    Cmd.Connection.Open();
                    OleDbDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.Read())
                        {  
                            return Convert.ToInt32(dr["EmpId"].ToString());
                        }
                }
                catch( Exception ex )
                    {
                        Response.Write( "There was an error processing the request.<BR><BR>Error Details:<BR>" + ex.Message );
                        Response.End();
                    }
                finally
                    {
                        if( Con.State == ConnectionState.Open )
                            {
                                Con.Close();
                            }
                    }
                return -1;
            }
            private int ChkUserName()
            {       
                try
                {
                    Con = new OleDbConnection(connectionstring);           
                    sqlStmt = "";
                    sqlStmt += "SELECT LoginId, ";
                    sqlStmt += "       COALESCE( EmpId, -1 ) AS EmpId";
                    sqlStmt += "  FROM Login ";
                    sqlStmt += " WHERE LoginId = '" + TxtUserID.Text.Trim() + "' ";               
                    System.Data.OleDb.OleDbCommand Cmd = new OleDbCommand(sqlStmt, Con);
                    Cmd.Connection.Open();
                    OleDbDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.Read())
                        {  
                            return Convert.ToInt32(dr["EmpId"].ToString());
                        }
                }
                catch( Exception ex )
                    {
                        Response.Write( "There was an error processing the request.<BR><BR>Error Details:<BR>" + ex.Message );
                        Response.End();
                    }
                finally
                    {
                        if( Con.State == ConnectionState.Open )
                            {
                                Con.Close();
                            }
                    }
                return -1;
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                //
                InitializeComponent();
                base.OnInit(e);
            }
           
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {   
                this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
                this.ImageButton2.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton2_Click);
                this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion   

            private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
                //if(this.IsPostBack)
                {
                    temp=TxtUserID.Text;               
                    eid = ChkAuthentication();
                    pwd=ChkPwd();                       
                }       
               
                if(pwd==-1)
                    {                   
                        string str1;
                        string strRedirectPath = "http://localhost/SpiGurukul/ChangePassword.aspx?name="+temp;
                        str1 = "";
                        str1+= "<script language='javascript' type='text/javascript'>";
                        str1+=    "window.open('" + strRedirectPath + "', '_top');";
                        str1+=" alert('Invalid Old Password');";
                        str1+= "</script>";
                        Page.RegisterStartupScript("clientScript", str1);           
                    }       
                else
    //            if(eid!=-1 && pwd!=-1)
                {
                    try
                    {
                        string str;
                        string strRedirectPath = "http://localhost/SpiGurukul/spiGurukulLogin.aspx";
                        str = "";
                        str+= "<script language='javascript' type='text/javascript'>";
                        str+= "window.open('" + strRedirectPath + "', '_top');";
                        str+=" alert('Change Password Successful');";
                        str+= "</script>";               
                        Page.RegisterStartupScript("clientScript", str);
                        string connectionstring = ConfigurationSettings.AppSettings["ConnectionString"];
                        Con = new OleDbConnection(connectionstring);
                        string sql = string.Format( "UPDATE Login SET Password = '{0}' WHERE Login.EmpId = {1}", TxtNewPwd.Text.TrimEnd(),eid);
                        System.Data.OleDb.OleDbCommand Cmd = new OleDbCommand(sql, Con);
                        Con.Open();
                        Cmd.ExecuteNonQuery();   
                        i=2;
                    }
                catch (Exception ex)
                    {
                        Response.Write( "There was an error processing the request.<BR><BR>Error Details:<BR>" + ex.Message );
                        Response.End();
                    }
                finally
                    {
                        if( Con.State == ConnectionState.Open )
                        {
                            Con.Close();
                            HttpCookie aCookie;
                            string cookieName;
                            int limit = Request.Cookies.Count;
                            for (int i=0; i<limit; i++)
                            {
                                cookieName = Request.Cookies[i].Name;
                                aCookie = new HttpCookie(cookieName);
                                aCookie.Expires = DateTime.Now.AddDays(-1);
                                Response.Cookies.Add(aCookie);
                            }
                            Response.Redirect("spiGurukulLogin.aspx");
                       
                        }                   
                    }
            }
           
                   
            }

            private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
            {
            Response.Redirect("spiGurukulLogin.aspx");   
            }

        }
    }

     

  • Re: Asp.net 1.1 and Javascript problem..need help

    05-01-2008, 12:52 AM
    Answer

    Hi,

    From your code ,it seems that the following method always return -1 although you have mentioned that the password do has been changed, right?



         private int ChkPwd()
            {       
                try
                {
                    Con = new OleDbConnection(connectionstring);           
                    sqlStmt = "";
                    sqlStmt += "SELECT LoginId, ";
                    sqlStmt += "       COALESCE( EmpId, -1 ) AS EmpId";
                    sqlStmt += "  FROM Login ";
                    sqlStmt += " WHERE Password = '" + TxtPwd.Text.Trim() + "'";                               
                    System.Data.OleDb.OleDbCommand Cmd = new OleDbCommand(sqlStmt, Con);
                    Cmd.Connection.Open();
                    OleDbDataReader dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    if (dr.Read())
                        {  
                            return Convert.ToInt32(dr["EmpId"].ToString());
                        }
                }
                catch( Exception ex )
                    {
                        Response.Write( "There was an error processing the request.<BR><BR>Error Details:<BR>" + ex.Message );
                        Response.End();
                    }
                finally
                    {
                        if( Con.State == ConnectionState.Open )
                            {
                                Con.Close();
                            }
                    }
                return -1;
            }

    I suggest that you can add a breakpoint in the above method, can run it step by step, try to check the sqlstmt variable and the dr object, run it in Query Analyzer alone, to see if it can return the rows.

    Thanks.

    Michael Jin.
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Asp.net 1.1 and Javascript problem..need help

    05-13-2008, 5:14 AM
    • Member
      10 point Member
    • su.rameshraj
    • Member since 09-18-2007, 7:10 AM
    • Posts 61

    I have tried a lot..but when the control returns back for the second time( since java script is used it will come) no counter is retained...please can some one help ...give me the code sample

     Thanks


     

  • Re: Asp.net 1.1 and Javascript problem..need help

    05-14-2008, 1:29 AM
    • Member
      10 point Member
    • su.rameshraj
    • Member since 09-18-2007, 7:10 AM
    • Posts 61

    Hi,

    Let me make the problem more simpler.
    I have to put html image button in change password screen..which will recognise Javascript.

    Example: <input type="image" onclick="password" >

    (They have provided us the image for that) Now when I put html image button...the whole code executes twice (it comes back again from the begining as if it was a loop) . By the second time no variable is present and it will give invalid old password..

    But the same case I am successful when I put html button..here all the commands executes only once..and every thing is fine.

    I tried BREAK command..no use.

    Can you please guide me how to proceed.

    Thanks

    Have a nice day.
     


     

     

     


Page 1 of 1 (4 items)