1

This My aspx i want when i insert data is not to record in database...

<div class="form-group">
    <label for="description" class="control-label">Hazard Description : </label>
    <div class="col-xs-8">
      <asp:TextBox ID="TextBox3" runat="server" CssClass="form-control" TextMode="MultiLine" Columns="55" Rows="7"></asp:TextBox>
      <asp:CustomValidator ID="CustomValidatorHazardDescription" ValidateEmptyText="true" runat="server"  Display="Dynamic" ErrorMessage="Tidak Boleh Kosong" OnServerValidate="CustomValidatorHazardDescription_ServerValidate"></asp:CustomValidator>
    </div>
</div>

When I Clik insert message successfull but data still to record in database? This My code behind i want when i insert data is not to record in database...

protected void CustomValidatorHazardDescription_ServerValidate(object source, ServerValidateEventArgs args)
{
     TextBox txb = (TextBox)FormView1.FindControl("TextBox3");


     if(string.IsNullOrEmpty(txb.Text))
     {

         args.IsValid = false;                       
      }
         else
      {
         args.IsValid = true;
         return;
       }
}

This Process insert

        tbl_hzr_main newitem = new tbl_hzr_main();
        newitem.code_company = int.Parse(((CustomControls_DdlCompany)_f.Controls[0].FindControl("ddl_company1")).SelectedValue);


        newitem.code_empPIC = int.Parse(((HiddenField)_f.Controls[0].FindControl("hid_pic_id")).Value);
        newitem.code_hzrUser = id_spv;
        newitem.code_incdLocation = int.Parse(((CustomControls_DdlLocation)_f.Controls[0].FindControl("ddl_location1")).SelectedValue);
        newitem.code_main_contractor = int.Parse(((CustomControls_DdlCompany)_f.Controls[0].FindControl("ddl_company1")).SelectedMainConValue);
        newitem.code_section = code_section;
        newitem.code_usrEntry = int.Parse(uc.usrID);
        newitem.date_hzrMain = DateTime.Parse(((TextBox)_f.Controls[0].FindControl("TextBox1")).Text);
        newitem.desc_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox3")).Text; // this insert to database  


        newitem.dueDate_hzrMain = DateTime.Parse(((TextBox)_f.Controls[0].FindControl("TextBox2")).Text);
        newitem.entryDate_hzrMain = DateTime.Now;
        newitem.folup_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox4")).Text;
        newitem.locDetail_hzrMain = ((TextBox)_f.Controls[0].FindControl("txb_loc_detail")).Text;
        newitem.PICsign_status = byte.Parse(((RadioButtonList)_f.Controls[0].FindControl("rbl_sign_pic")).SelectedValue);
        newitem.stat_hzrMain = byte.Parse(((RadioButtonList)_f.Controls[0].FindControl("RadioButtonList1")).SelectedValue);

        dbcontext.tbl_hzr_main.Add(newitem);
        dbcontext.SaveChanges();
3
  • 1
    Nothing in the code you shows does anything with a database. Can you show the code where you insert the data (and where you supposedly check first if data is valid before inserting)? Commented Jul 24, 2017 at 5:48
  • ok sir, already to add. Commented Jul 24, 2017 at 5:57
  • simply enclose the whole "Database Save" code inside if(Page.IsValid) Commented Jul 24, 2017 at 6:07

2 Answers 2

1

In your OnSubmit function or Button_Click event (Where form Submits) add

Page.Validate();
 if (Page.IsValid == true){
//Add to Database
}else {
//Dont Add to Database
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great this is i want
1

from you updated question, below this line :

newitem.desc_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox3")).Text; // this insert to database  

try adding this code :

if(string.isNullOrEmpty(newitem.desc_hzrMain)){
   //show alert or something that data fail to insert
   return;
}

2 Comments

Good for only one field but if more validations are needed it would be exhaustive
Good advise @Hannan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.