Click here to Skip to main content
15,031,085 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
here is the web page for user to confirmation his account, on page load, i get user id and name from the database against the activation code. then ask user to fill out his profile details, On profile details form there are cascading drop down list, when user select a value from parent drop down list it refreshes and now ready to select its child drop down list. but on refresh it sets the Userid, username to null that i get from the database on page load.



C#
public partial class CandidateAccountActivation : System.Web.UI.Page
{
    DBClass db1 = new DBClass();
    int userId;
  string email, FileName, AppUsername, AppUserfname, AppUserlname, hash, password, pincode, constituency;
  string fathername, dob, workphone, province, city, officeaddress, partyname, partyjoinson;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack)
        {
           try{ string activationCode = !string.IsNullOrEmpty(Request.QueryString["ActivationCode"]) ? Request.QueryString["ActivationCode"] : Guid.Empty.ToString();
            db1.sqlcmd = new SqlCommand("uspConfirmUserActivation");
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                db1.sqlcmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                db1.sqlcmd.Parameters.Add("@UserID", SqlDbType.Int);
                db1.sqlcmd.Parameters.Add("@UserEmail", SqlDbType.VarChar, 50);
                db1.sqlcmd.Parameters.Add("@UserName", SqlDbType.VarChar, 20);
                db1.sqlcmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 20);
                db1.sqlcmd.Parameters.Add("@LastName", SqlDbType.VarChar, 20);
                db1.sqlcmd.Parameters["@UserID"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@UserEmail"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@UserName"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@FirstName"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Parameters["@LastName"].Direction = ParameterDirection.Output;
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
               <big> userId = Convert.ToInt32(db1.sqlcmd.Parameters["@UserID"].Value);
                email = Convert.ToString(db1.sqlcmd.Parameters["@UserEmail"].Value);
                AppUsername = Convert.ToString(db1.sqlcmd.Parameters["@UserName"].Value);
                AppUserfname = Convert.ToString(db1.sqlcmd.Parameters["@FirstName"].Value);
                AppUserlname = Convert.ToString(db1.sqlcmd.Parameters["@LastName"].Value);</big>
                Response.Write(userId);
                Response.Write(email);
                Response.Write(AppUsername);
                Response.Write(AppUserfname);
                Response.Write(AppUserlname);
                
            }}
               catch (Exception ex)
           {Response.Write(ex);}
            finally{db1.sqlcon.Close();}
        }

        if (!IsPostBack)
        {
                DDProvince.DataSource = GetData("uspGetProvinces", null);
                DDProvince.DataBind();

                DDParty.DataSource = GetData("uspGetPartyName", null);
                DDParty.DataBind();

                DDDistrict.DataSource = GetData("uspGetDistrict", null);
                DDDistrict.DataBind();

                ListItem lstitemProvince = new ListItem("Select State", "-1");
                DDProvince.Items.Insert(0, lstitemProvince);

                ListItem lstitemCities = new ListItem("Select City", "-1");
                DDCity.Items.Insert(0, lstitemCities);
                DDCity.Enabled = false;

                ListItem lstitemparties = new ListItem("Select Party You Belong", "-1");
                DDParty.Items.Insert(0, lstitemparties);

                ListItem lstitemDistrict = new ListItem("Select District in which You Represent Your Party", "-1");
                DDDistrict.Items.Insert(0, lstitemDistrict);
            
        }
    }

    protected void BttnSumbit_Click(object sender, EventArgs e)
    {
        fathername = TxtBxFathername.Text.Trim();
        dob = Convert.ToDateTime(TxtBxDob.Text.Trim()).ToString("MM-dd-yyyy");
        //pincode = TxtBxPinCode.ToString();
        workphone = TxtBxWorkPhone.Text.Trim();
        officeaddress = TxtBxOfficAdres.Text.Trim();
        city = DDCity.SelectedItem.Text;
        province = DDProvince.SelectedItem.Text;
        partyname = DDParty.SelectedItem.Text;
        constituency = DDConstituency.SelectedItem.Text;
        password = TxtBxConfirmPassword.Text.Trim();
        //string salt = GenerateSalt(8);
        pincode = Convert.ToString(TxtBxPinCode.Text);
        string message = string.Empty;
        try
        {
            //string hashedPassword = ComputeHash(password, salt, pinc);
            if (AppUserimgUpload.PostedFile != null)
            {
                FileName = Path.GetFileName(AppUserimgUpload.PostedFile.FileName);

                //Save files to disk
                AppUserimgUpload.SaveAs(Server.MapPath("Images/" + FileName));
            }
            db1.sqlcmd = new SqlCommand("uspSetUserPassword");

            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                db1.sqlcmd.CommandType = CommandType.StoredProcedure;
                //db1.sqlcmd.Parameters.AddWithValue("@Password", hashedPassword);
                db1.sqlcmd.Parameters.AddWithValue("@Password", password);
                db1.sqlcmd.Parameters.AddWithValue("@UserID", userId);
                db1.sqlcmd.Parameters.AddWithValue("@Fathername", fathername);
                //db1.sqlcmd.Parameters.AddWithValue("@Salt", salt);
                db1.sqlcmd.Parameters.AddWithValue("@DOB", dob);
                db1.sqlcmd.Parameters.AddWithValue("@WorkPhone", workphone);
                db1.sqlcmd.Parameters.AddWithValue("@Avatar", FileName);
                db1.sqlcmd.Parameters.AddWithValue("@AvatarPath", "images/" + FileName);
                db1.sqlcmd.Parameters.AddWithValue("@Pincode", pincode);
                db1.sqlcmd.Parameters.AddWithValue("@OfficeProvince",DDProvince.SelectedItem.ToString());
                db1.sqlcmd.Parameters.AddWithValue("@OfficeCity", DDCity.SelectedItem.Text);
                db1.sqlcmd.Parameters.AddWithValue("@OfficeAddress", officeaddress);
                db1.sqlcmd.Parameters.AddWithValue("@Partyid", DDParty.SelectedValue);
                db1.sqlcmd.Parameters.AddWithValue("@PartyJoiningDate", Convert.ToDateTime(TxtBxPartyJoiningDate.Text.Trim()));
                db1.sqlcmd.Parameters.AddWithValue("@CId", DDConstituency.SelectedValue);
                db1.sqlcmd.Connection = db1.sqlcon;
                db1.sqlcon.Open();
                db1.sqlcmd.ExecuteScalar();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            db1.sqlcon.Close();
            message = "Registration Completed, Click Ok and You Will be Redirct to the Login Page";
            SendActivationEmail();
            //Response.Redirect("Home.aspx");
        }
    }

    private DataSet GetData(string SpName, SqlParameter SPParameter)
    {
        SqlDataAdapter sqlda = new SqlDataAdapter(SpName, db1.sqlcon);
        sqlda.SelectCommand.CommandType = CommandType.StoredProcedure;
        if (SPParameter != null)
        {
            sqlda.SelectCommand.Parameters.Add(SPParameter);
        }
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        return ds;
    }

    protected void DDProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDProvince.SelectedIndex == 0)
        {
            DDCity.SelectedIndex = 0;
            DDCity.Enabled = false; 
        }
        else
        {
            DDCity.Enabled = true;
            SqlParameter sqlparamater = new SqlParameter("@ProvinceId", DDProvince.SelectedValue);
            DataSet ds = GetData("uspGetCitiesAgainstProvince", sqlparamater);
            DDCity.DataSource = ds;
            DDCity.DataBind();
            ListItem lstitemProvince = new ListItem("Select City", "-1");
            DDCity.Items.Insert(0, lstitemProvince);

        }
    }

    protected void DDDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDDistrict.SelectedIndex == 0)
        {
            DDConstituency.SelectedIndex = 0;
            DDConstituency.Enabled = false;
        }
        else
        {
            DDConstituency.Enabled = true;
            SqlParameter sqlparamater = new SqlParameter("@DisID", DDDistrict.SelectedValue);
            DataSet ds = GetData("uspGetConstituencyAgainstDistrict", sqlparamater);
            DDConstituency.DataSource = ds;
            DDConstituency.DataBind();
            ListItem lstitemDDDistrict = new ListItem("Select Constituency", "-1");
            DDConstituency.Items.Insert(0, lstitemDDDistrict);

        }
    }

    public static string ComputeHash(string password, string salt)
    {
        SHA512Managed hashAlg = new SHA512Managed();
        byte[] hash = hashAlg.ComputeHash(Encoding.UTF8.GetBytes(password + salt));
        return Convert.ToBase64String(hash);
    }

    private static string GenerateSalt(int byteCount)
    {
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        byte[] salt = new byte[byteCount];
        rng.GetBytes(salt);
        return Convert.ToBase64String(salt);

    }

    public static string ComputeHash(string password, string salt, string entropy)
    {
        SHA512Managed hashAlg = new SHA512Managed();
        byte[] hash = hashAlg.ComputeHash(Encoding.UTF8.GetBytes(password + salt + entropy));
        return Convert.ToBase64String(hash);
    }
  
    private void SendActivationEmail()
    {
    
            using (MailMessage mm = new MailMessage("temp5052@gmail.com", TxtBxEmail.Text.Trim()))
            {
                mm.Subject = "Account Activated";
                string body = "Hello " + AppUsername + ",";
                body += "<br /><br />Please Save This Information.";
                body += "<br />Full Name : " + AppUserfname + " " + AppUserlname;
                body += "<br />User Name : " + AppUsername;
                body += "<br />Email : " + TxtBxEmail.Text.Trim();
                body += "<br />Password : " + password;
                body += "<br />Fathername : " + fathername;
                body += "<br />DOB : " + dob;
                body += "<br />Pincode : " + pincode;
                body += "<br />Workphone : " + workphone;
                body += "<br />Office Address : " + officeaddress + " , " + city + " , " + province;
                body += "<br />Your Party Name : " + partyname;
                body += "<br />The Constituency in which you represent Your Party : " + constituency;
                body += "<br /><br />Thanks <br /> Electoral Information System";
                mm.Body = body;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential("temp5052@gmail.com", "abcabc");
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            
        }


    }

}
Posted
Updated 26-Apr-14 0:55am
v3

1 solution

you can put username and password in session or view state.

http://msdn.microsoft.com/en-us/library/vstudio/75x4ha6s(v=vs.100).aspx[^]
Session
set
C#
Session["userName"]=userName;

get
C#
String userName= Session["userName"] as String;

ViewState
set
C#
ViewState["userName"]=userName;

get
C#
string userName=ViewState["userName"];
   
v3

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