Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果文本框为空但数据仍要记录在数据库中,则自定义验证_C#_Sql Server - Fatal编程技术网

C# 如果文本框为空但数据仍要记录在数据库中,则自定义验证

C# 如果文本框为空但数据仍要记录在数据库中,则自定义验证,c#,sql-server,C#,Sql Server,这是我的aspx,我想当我插入数据是不记录在数据库 <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-c

这是我的aspx,我想当我插入数据是不记录在数据库

<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>
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;
       }
}
此过程将插入

        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();

在OnSubmit函数或按钮中单击事件(表单提交的位置)添加


根据您更新的问题,在这行下面:

newitem.desc_hzrMain = ((TextBox)_f.Controls[0].FindControl("TextBox3")).Text; // this insert to database  
尝试添加以下代码:

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

您显示的代码中没有任何内容与数据库有关。你能显示你插入数据的代码吗(以及你应该在插入之前首先检查数据是否有效的代码)?好的,先生,已经添加了。只需将整个“数据库保存”代码封装在if(Page.IsValid)中,如果只对一个字段有效,但如果需要更多验证,则会非常有用,建议@HannanGreat这是我想要的
if(string.isNullOrEmpty(newitem.desc_hzrMain)){
   //show alert or something that data fail to insert
   return;
}