Thursday, March 05, 2015

registration with validations

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class registration : System.Web.UI.Page
{
   SqlConnection con = new SqlConnection("Data Source=AAPTI-1506A06CD\\AAPTI;Initial Catalog=vidya;Integrated Security=True");

   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
          
           DropDownList1.Items.Add("--select--");
           try
           {
               SqlCommand cmd = new SqlCommand("select CountryName from country", con);
               con.Open();
               SqlDataReader dr;
               dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   DropDownList1.Items.Add(dr[0].ToString());
               }
               con.Close();
           }
           catch (Exception ex)
           {
               Response.Write(ex);
           }
       }
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {



       if (DropDownList1.SelectedIndex > 0)
       {
           try
           {
               SqlCommand cmd = new SqlCommand("select Countrycode from country where CountryName='" + DropDownList1.SelectedValue.ToString() + "' ", con);
               SqlDataReader dr;
               con.Open();
               dr = cmd.ExecuteReader();
               while (dr.Read())
               {
                   txtcode.Text = dr.GetValue(0).ToString();
               }

               con.Close();
           }
           catch (Exception ex)
           {
               Response.Write(ex);
           }
       }
       else
           txtcode.Text = "";
   }
   protected void btnSubmit_Click(object sender, EventArgs e)
   {
       if (DropDownList1.SelectedIndex > 0)
       {
           string mobile = txtcode.Text + txtphno.Text;

           SqlConnection con = new SqlConnection("Data Source=AAPTI-1506A06CD\\AAPTI;Initial Catalog=vidya;Integrated Security=True");
           try
           {
               SqlCommand cmd = new SqlCommand("insert into Registration values('" + txtName.Text + "','" + txtEmail.Text + "','" + txtPW.Text + "','" + txtCPW.Text + "','" + DropDownList1.SelectedItem.Text + "','" + mobile.ToString() + "')", con);
               con.Open();
               int i = cmd.ExecuteNonQuery();



               if (i > 0)
               {
                   ShowAlertMessage(i + " Row has been Inserted Successfully");
               }
               con.Close();

               ClearTextboxes(this.Controls);

           }
           catch (Exception ex)
           {
               Response.Write(ex);
           }
       }

       else
           ShowAlertMessage("Please select a Country");
   }

   public void ShowAlertMessage(string error)
   {
       Page page = HttpContext.Current.Handler as Page;
       if (page != null)
       {
           error = error.Replace("'", "\'");
           ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);

       }
   }



   public void ClearTextboxes(ControlCollection ctrl)
   {
       foreach (Control c in ctrl)
       {

           //Clearing text in the TextBox
           if (c is TextBox)
               ((TextBox)c).Text = string.Empty;

           //Clearing Selected RadioButton
           if (c is RadioButtonList)
               ((RadioButtonList)c).ClearSelection();

           //Clearing selected ListBox
           if (c is ListBox)
               ((ListBox)c).ClearSelection();

           //Clearing selected CheckBox
           if (c is CheckBox)
               ((CheckBox)c).Checked = false;

           //Clearing selected index of DropDown
           if (c is DropDownList)
               //usually dorpdownlists 0 index is used for "- Select -", "- Apply -" etc.  
               ((DropDownList)c).SelectedIndex = 0;
           //(myControl as DropDownList).SelectedIndex = 0;

           ClearTextboxes(c.Controls);
       }
   }
}

No comments:

Post a Comment