Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends ..i got aone problem in asp.net please help me..

i am develop a signup page .in that when i am submit the data ..data was inserted into database even null values also ..but i am set the null values are not allowed in the database .and also i am adding the validations also but these are not worked properly ..please help me..

my code is:
C#
protected void btn_r_submit_Click(object sender, EventArgs e)
    {

               con.Open();
        string strid = "select count(*) from tbl_user_register";
        SqlCommand cmdid = new SqlCommand(strid, con);
        int count = Convert.ToInt16(cmdid.ExecuteScalar()) + 1;
        string struserid = count.ToString();
        

        string strquery = "insert into tbl_user_register values('"+struserid+"','" + txt_fname.Text + "','" + txt_dob.Text + "','" + txt_r_loginid.Text + "','" + txt_r_pwd.Text + "','" + txt_r_confirmpwd + "','" + txt_r_email.Text + "','" + txt_r_mobile.Text + "')";

        SqlCommand cmdregister = new SqlCommand(strquery, con);
        if (cmdregister.ExecuteNonQuery()>0)
        {

            Response.Write("<Script>alert('Successfully Registered in GVK Academy..')</Script>");         
      
            con.Close();
        }

        else
        {
            try
            {

                con.Open();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                //return false;

                con.Close();
            }
        }



my design page code is:
ASP.NET
<div id="div_signup">
                      <div id="div_sigbg">
                          <table class="auto-style1">
                              <tr>
                                  <td colspan="2">&nbsp;</td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="background-color: #006699; color: #FFFFFF; text-align: center; font-weight: bold; height:30px; font-size: 25px; font-family: 'Courier New', Courier, monospace;">Student Registration</td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Full Name :</td>
                                  <td class="auto-style4">
                                      <asp:TextBox ID="txt_fname" CssClass="txt_register" runat="server"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" SetFocusOnError="true" ControlToValidate="txt_fname" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Date Of Birth :</td>
                                  <td>
                                      <asp:TextBox ID="txt_dob" CssClass="txt_register" TextMode="Date" runat="server"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" SetFocusOnError="true" ControlToValidate="txt_dob" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Login ID :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_loginid" runat="server" CssClass="txt_register" ></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator3"  SetFocusOnError="true" ControlToValidate="txt_r_loginid" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Password :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_pwd" runat="server"  TextMode="Password" CssClass="txt_register"></asp:TextBox>

                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator4"  SetFocusOnError="true" ControlToValidate="txt_r_pwd" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>

                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Confirm Passsword :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_confirmpwd" runat="server"  TextMode="Password" CssClass="txt_register"></asp:TextBox>

                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator9"  SetFocusOnError="true" ControlToValidate="txt_r_confirmpwd" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_r_pwd" ControlToValidate="txt_r_confirmpwd" ForeColor="#ff0000" ErrorMessage="Password not matched"></asp:CompareValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist" >E-Mail Id :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_email" runat="server" CssClass="txt_register"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator5"  SetFocusOnError="true" ControlToValidate="txt_r_email" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;&nbsp;&nbsp;
                                      <asp:RegularExpressionValidator ID="RegularExpressionValidator1"  SetFocusOnError="true" runat="server" ForeColor="#ff0000" ControlToValidate="txt_r_email" ErrorMessage="Invaild email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Mobile No :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_mobile" runat="server" CssClass="txt_register"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator6"  SetFocusOnError="true" ControlToValidate="txt_r_mobile" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;&nbsp;&nbsp;
                                      <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"  SetFocusOnError="true"  ControlToValidate="txt_r_mobile" ErrorMessage="Invalid Mobi no" ForeColor="Red" ValidationExpression="[789][0-9]{9}" ValidationGroup="register"></asp:RegularExpressionValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td>&nbsp;</td>
                                  <td>
                                      <asp:Label ID="lbl_registermessage" Visible="false" runat="server" ForeColor="#006600" Text="Successfully Registration Complete..."></asp:Label>
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="text-align:center">
                                      <asp:Button ID="btn_r_submit"  CssClass="btn_register" runat="server"   ValidationGroup="register" Text="Submit" OnClick="btn_r_submit_Click" ValidateRequestMode="Enabled" />
                                      &nbsp;
                                      <asp:Button ID="btn_r_clear"   CssClass="btn_register"  runat="server"  ValidationGroup="register" Text="Clear" OnClick="btn_r_clear_Click" />
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="text-align:center">
                                      &nbsp;</td>
                              </tr>
                          </table>
                      </div>
                  </div>
Posted
Updated 7-Dec-15 18:51pm
v2
Comments
[no name] 8-Dec-15 0:54am    
What is not actually validating. Whether it is displaying message or not validating at all even if you are not entering any value to the textbox. Please go through below link:

http://www.c-sharpcorner.com/UploadFile/cd7c2e/enabling-unobtrusive-validation-mode-in-Asp-Net-4-5/

Two things, the first should fix your problem.

1) You need to add ValidationGroup to your web controls, such as: <asp:TextBox ID="txt_r_confirmpwd" runat="server" ValidationGroup="register" TextMode="Password" CssClass="txt_register">

2) It is best practice to use the Page.IsValid method when submitting a form, because it will force the validation controls to run on the server side too.

Page.Validate();
        if (!Page.IsValid)
        {
            Literal1.Text = "* Form is incomplete";
        }
        else
        {
            //insert data
        }
 
Share this answer
 
v2
Comments
Member 10575434 8-Dec-15 1:07am    
k sir, but the above method where will i inserted in my codeing page..? in button event or page load method..
please tel me
jgakenhe 8-Dec-15 1:19am    
The button click event.
jgakenhe 8-Dec-15 1:23am    
protected void btn_r_submit_Click(object sender, EventArgs e)
{
Page.Validate();
if (!Page.IsValid)
{
lbl_registermessage.Text = "* Form is incomplete";
}
else
{
con.Open();
string strid = "select count(*) from tbl_user_register";
SqlCommand cmdid = new SqlCommand(strid, con);
int count = Convert.ToInt16(cmdid.ExecuteScalar()) + 1;
string struserid = count.ToString();


string strquery = "insert into tbl_user_register values('"+struserid+"','" + txt_fname.Text + "','" + txt_dob.Text + "','" + txt_r_loginid.Text + "','" + txt_r_pwd.Text + "','" + txt_r_confirmpwd + "','" + txt_r_email.Text + "','" + txt_r_mobile.Text + "')";

SqlCommand cmdregister = new SqlCommand(strquery, con);
if (cmdregister.ExecuteNonQuery()>0)
{

Response.Write("<Script>alert('Successfully Registered in GVK Academy..')</Script>");

con.Close();
}
else
{
try
{
con.Open();
}
catch (Exception ex)
{
Response.Write(ex.Message);
//return false;
con.Close();
}
}
}
}
There are couple of possibility of your problem
1. You should check Page.IsValid before code execution on button click.
2. if you are using 'asp:Button' and .CS file for code behind then you should fire Serverclick event
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900