Hi dharmendr,
Refer the below code,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
using System.Globalization;
using System.IO;
using System.Data.OleDb;
using System.Web.Security;
using System.Net;
using System.Net.Mail;
using System.Text;
public partial class applicantsRegister : System.Web.UI.Page
{
DateTime now = DateTime.Now;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void cvRegister_ServerValidate(object Sender, ServerValidateEventArgs e)
{
if (string.IsNullOrEmpty(txtUsername.Text.Trim()))
{
Message("Username is Required!");
txtUsername.Focus();
}
else if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
{
Message("Password is Required!");
txtPassword.Focus();
}
else if (string.IsNullOrEmpty(txtcPassword.Text.Trim()))
{
Message("Password is Required!");
txtcPassword.Focus();
}
else if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
Message("Name is Required!");
txtName.Focus();
}
else if (string.IsNullOrEmpty(txtDOB.Text.Trim()))
{
Message("Date of Birth is Required!");
txtDOB.Focus();
}
else if (rblGender.SelectedIndex == -1)
{
Message("Please select gender");
}
else if (string.IsNullOrEmpty(txtICNumber.Text.Trim()))
{
Message("Identification Number is Required!");
txtICNumber.Focus();
}
else if (ddlReligion.SelectedIndex == 0)
{
Message("Please select religion");
}
else if (ddlRace.SelectedIndex == 0)
{
Message("Please select race");
}
else if (string.IsNullOrEmpty(txtAddress.Text.Trim()))
{
Message("Address is Required!");
txtAddress.Focus();
}
else if (string.IsNullOrEmpty(txtPostcode.Text.Trim()))
{
Message("Postcode is Required!");
txtPostcode.Focus();
}
else if (string.IsNullOrEmpty(txtMobile.Text.Trim()))
{
Message("Mobile No is Required!");
txtMobile.Focus();
}
else if (string.IsNullOrEmpty(txtEmail.Text.Trim()))
{
Message("Email is Required!");
txtEmail.Focus();
}
}
private void Message(string control)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Required", "alert('" + control + "')", true);
}
protected void btnApply_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string applicantIC = txtICNumber.Text;
con.Open();
String queryA = "SELECT ap_icno FROM tbl_applicantsdetails WHERE ap_icno = '" + applicantIC + "' ";
SqlCommand cmdA = new SqlCommand(queryA, con);
SqlDataReader drA = cmdA.ExecuteReader();
if (drA.Read())
{
string message = " Duplicate IC Number found.";
string url = "applicantsRegister.aspx";
string script = "window.onload = function(){ if(alert('";
script += message;
script += "'));";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
else
{
string query = "submitApplicants";
int ap_id;
if (declaration.Checked == false)
{
msg.Text = "Please tick on the declaration division if you agree";
return;
}
else
{
msg.Text = "";
SqlCommand cmdUser = new SqlCommand(query, con);
cmdUser.CommandType = CommandType.StoredProcedure;
cmdUser.Parameters.Add("@ap_id", SqlDbType.Int, 0, "ap_id");
cmdUser.Parameters["@ap_id"].Direction = ParameterDirection.Output;
cmdUser.Parameters.Add("@ap_username", SqlDbType.NVarChar).Value = txtUsername.Text;
cmdUser.Parameters.Add("@ap_password", SqlDbType.NVarChar).Value = txtPassword.Text;
cmdUser.Parameters.Add("@ap_name", SqlDbType.NVarChar).Value = txtName.Text;
{
int length = upImage.PostedFile.ContentLength;
byte[] pic = new byte[length];
upImage.PostedFile.InputStream.Read(pic, 0, length);
cmdUser.Parameters.AddWithValue("@ap_photo", pic);
}
cmdUser.Parameters.Add("@ap_icno", SqlDbType.NVarChar).Value = txtICNumber.Text;
cmdUser.Parameters.Add("@ap_dateofbirth", SqlDbType.Date).Value = txtDOB.Text;
cmdUser.Parameters.Add("@ap_gender", SqlDbType.NVarChar).Value = rblGender.Text;
cmdUser.Parameters.Add("@ap_religion", SqlDbType.NVarChar).Value = ddlReligion.Text;
cmdUser.Parameters.Add("@ap_race", SqlDbType.NVarChar).Value = ddlRace.Text;
cmdUser.Parameters.Add("@ap_nationality", SqlDbType.NVarChar).Value = txtNationality.Text;
cmdUser.Parameters.Add("@ap_maritalstatus", SqlDbType.NVarChar).Value = ddlMarital.Text;
cmdUser.Parameters.Add("@ap_address", SqlDbType.NVarChar).Value = txtAddress.Text;
cmdUser.Parameters.Add("@ap_city", SqlDbType.NVarChar).Value = txtCity.Text;
cmdUser.Parameters.Add("@ap_state", SqlDbType.NVarChar).Value = txtState.Text;
cmdUser.Parameters.Add("@ap_postcode", SqlDbType.Int).Value = Convert.ToInt32(txtPostcode.Text);
cmdUser.Parameters.Add("@ap_contacthome", SqlDbType.NVarChar).Value = txtHome.Text;
cmdUser.Parameters.Add("@ap_contactmobile", SqlDbType.NVarChar).Value = txtMobile.Text;
cmdUser.Parameters.Add("@ap_email", SqlDbType.NVarChar).Value = txtEmail.Text;
cmdUser.Parameters.Add("@ap_datecreated", SqlDbType.DateTime).Value = now;
if (upResume.HasFile == false)
{
msg.Text = "Please first select a file to upload";
}
else
{
string filePath = Server.MapPath("~/resume/" + upResume.FileName);
upResume.SaveAs(filePath);
}
cmdUser.Parameters.Add("@ap_attachresume", SqlDbType.NVarChar).Value = upResume.FileName;
cmdUser.ExecuteNonQuery();
ap_id = (int)cmdUser.Parameters["@ap_id"].Value;
}
string message = "Your details has been saved. ";
string url = "jobListing.aspx?ap_id=" + ap_id;
string script = "window.onload = function(){ if(alert('";
script += message;
script += "'));";
script += "window.location = '";
script += url;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
}
else
{
msg.Text = "Fill up the information required";
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
}
I put this code below at front page to call the process
<asp:CustomValidator id="cvApplicantRegister" ErrorMessage="" SetFocusOnError="true" runat="server" Display="Dynamic" OnServerValidate="cvRegister_ServerValidate" ValidationGroup="ValidateClick" ont-Bold="False" Font-Italic="True" Font-Strikeout="False" ForeColor="Red" ></asp:CustomValidator>
May I know why this code is not function? Can I separate the server validation and make it another class or I need to paste the code into "btnApply_Click"?