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");
}
}
}