Hi y@1998,
Refer below code.
HTML
<div>
<asp:TextBox ID="admin" runat="server" ReadOnly="true" Text="admin" Visible="false"></asp:TextBox>
<div class="container">
<div class="row" style="background-color: aliceblue">
<div class="col-md-8 col-md-offset-2 m-auto">
<div class="contact-form">
<h1>
</h1>
<h1 style="font-family: 'Adobe Caslon Pro'">
<b>Admin Registration</b></h1>
<form action="/examples/actions/confirmation.php" method="post">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label for="inputFirstName">
Full Name</label>
<asp:RadioButtonList ID="role" runat="server" Width="500px" RepeatDirection="Horizontal">
<asp:ListItem>Placement Coordinator</asp:ListItem>
<asp:ListItem>Faculty</asp:ListItem>
</asp:RadioButtonList>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="inputFirstName">
Full Name</label>
<asp:TextBox ID="fnm" runat="server" class="form-control" Placeholder="Enter Your Name"
onKeyPress="return ValidateAlpha(event)"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name Required!"
ForeColor="Red" ControlToValidate="fnm"></asp:RequiredFieldValidator>
</div>
</div>
<script type="text/javascript">
function ValidateAlpha(evt) {
var keyCode = (evt.which) ? evt.which : evt.keyCode
if ((keyCode < 65 || keyCode > 90) && (keyCode < 97 || keyCode > 123) && keyCode != 32 && keyCode != 8 && keyCode != 11)
return false;
return true;
}
</script>
<div class="col-sm-6">
<div class="form-group">
<label for="inputFirstName">
Email</label>
<asp:TextBox ID="mail" runat="server" class="form-control" Placeholder="Enter Your Email"
AutoPostBack="true"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Email Required!"
ForeColor="Red" ControlToValidate="mail"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Your Email is not proper format"
ControlToValidate="mail" Display="Dynamic" ValidationExpression="\w+(-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ForeColor="Red"></asp:RegularExpressionValidator>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputFirstName">
Contact</label>
<asp:TextBox ID="cn" runat="server" class="form-control" MaxLength="10" Placeholder="Enter Contact Number"
AutoPostBack="true"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="" ForeColor="Red"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Contact Required!"
ForeColor="Red" ControlToValidate="cn"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" ErrorMessage="Please repeat your number"
ControlToValidate="cn" ForeColor="Red" Display="Dynamic" ValidationExpression="^[6-9]\d{9}$"></asp:RegularExpressionValidator>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="inputFirstName">
Gender</label>
<asp:RadioButtonList ID="gender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Gender Required!"
ForeColor="Red" ControlToValidate="gender"></asp:RequiredFieldValidator>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputFirstName">
Password</label>
<asp:TextBox ID="password" runat="server" class="form-control" Type="password" Placeholder="************"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator16" runat="server" ErrorMessage="Required"
ControlToValidate="password" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ErrorMessage="ex: Aa@123 "
ControlToValidate="password" ForeColor="Red" ValidationExpression="((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})"
runat="server" />
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="inputFirstName">
Confirm Password</label>
<asp:TextBox ID="cpassword" runat="server" class="form-control" Type="password" Placeholder="************"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ErrorMessage="Required"
ControlToValidate="cpassword" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="comparePasswords" runat="server" ControlToCompare="password"
ControlToValidate="cpassword" ErrorMessage="Your passwords do not match up!"
Display="Dynamic" />
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
Already have account?<a href="slogin.aspx" style="color: dodgerblue">Sign In</a>.</p></td>
<asp:Button CssClass="btn btn-success" ID="Button1" runat="server" Text="Sign Up"
OnClick="Button1_Click" CausesValidation="true" />
<asp:Button ID="cancle" runat="server" Text="Cancle" CssClass="btn btn-danger" CausesValidation="false" />
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
Namespaces
using System.Data.SqlClient;
using System.Configuration;
Code
protected void Button1_Click(object sender, EventArgs e)
{
string sql = null;
string st = role.SelectedItem.Value;
sql = "Select type from register_29 where type=@type";
cnn.Open();
using (SqlCommand md = new SqlCommand(sql, cnn))
{
md.Parameters.AddWithValue("@type", st.ToString());
string coordinator = Convert.ToString(md.ExecuteScalar());
if (!string.IsNullOrEmpty(coordinator))
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('You must select Faculty')", true);
}
else
{
cnn.Close();
cnn.Open();
var qry = "insert into register_29(fullname,email,contact,gender,pwd,type,d_t)values(@fullname,@email,@contact,@gender,@pwd,@type,@d_t)";
using (SqlCommand cmd = new SqlCommand(qry, cnn))
{
cmd.Parameters.AddWithValue("@fullname", fnm.Text);
cmd.Parameters.AddWithValue("@email", mail.Text);
cmd.Parameters.AddWithValue("@contact", cn.Text);
cmd.Parameters.AddWithValue("@gender", gender.Text);
cmd.Parameters.AddWithValue("@pwd", password.Text);
cmd.Parameters.AddWithValue("@type", role.Text);
cmd.Parameters.AddWithValue("@d_t", DateTime.Now);
if (cmd.ExecuteNonQuery() > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Add Successfully');window.location ='login.aspx';", true);
}
}
cnn.Close();
}
}
}
y@1998 says:
ScriptManager.RegisterStartupScript(
this
,
this
.GetType(),
"alert"
,
"alert('You must select Faculty');window.location ='reg_admin.aspx';"
,
true
);
Your code is also working only you need to remove above code using below line of code.
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('You must select Faculty')", true);