2014-01-29 37 views
-3

無法將NULL值插入到'AccountID'列中,表'e-BusPassManagementSystem.dbo.TBLAccount';列在這裏不允許空值。 INSERT失敗。 該聲明已被終止。無法將NULL值插入'AccountID'列中,表TBLAccount'列不允許爲空值。 INSERT失敗。該聲明已被終止

我的SQL語句是:

ALTER PROCEDURE SetApplicationForm 
@UserID int, 
@InstitueName varchar(50), 
@ResidentalAddress varchar(max), 
@Place varchar(max), 
@WorkingDays int, 
@Time int, 
@Photo varchar(50), 
@IDProof varchar(50), 
@AppliedDate datetime, 
@ValiedUpTo datetime, 
@CardType varchar(10), 
@Cardno varchar(50), 
@CardExpDate datetime 
AS 
begin 
insert into TBLApplicationForm(UserID, InstitueName, ResidentalAddress, 
            Place, WorkingDays, [Time], Photo, 
            IDProof, AppliedDate, ValidUpTo, Status) 
    values(@UserID, @InstitueName, @ResidentalAddress, 
      @Place, @WorkingDays, @Time, @Photo, 
      @IDProof, @AppliedDate, @ValiedUpTo, 'PENDING') 

declare @ApplicationID int 

set @ApplicationID = Scope_Identity() 

insert into TBLAccount(UserID, ApplicationID, CardType, CardNumber, Amount, CardExpireDate, Status) 
values(@UserID, @ApplicationID, @CardType, @Cardno, '200', @CardExpDate, 'PAID') 
end 

C#方法:

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     ApplicationFormEntity objAppFrm = new ApplicationFormEntity(); 
     objAppFrm.Institute = txtInstituteName.Text; 
     objAppFrm.UserID = Convert.ToInt32(Session["UserID"]); 
     // strName = Convert.ToInt32(txtName.Text).ToString(); 
     objAppFrm.ResidentalAddress = txtResAddr.Text; 
     objAppFrm.Place = txtPlace.Text; 
     objAppFrm.WorkingDays = Convert.ToInt32(txtNOD.Text); 
     objAppFrm.Time = Convert.ToInt32(txtTime.Text); 
     string strPhoto = Path.GetFileName(fpPhoto.PostedFile.FileName); 
     fpPhoto.SaveAs(Server.MapPath("Images/" + strPhoto)); 
     // fileuploadimages.SaveAs(Server.MapPath("~/" + filename)); 
     objAppFrm.ProofID = Path.GetFileName(fpIDProof.PostedFile.FileName); 
     fpPhoto.SaveAs(Server.MapPath("File/" + objAppFrm.ProofID)); 
     objAppFrm.AppliedDate = System.DateTime.Now; 
     objAppFrm.ValidUpTo = System.DateTime.Now; 
     objAppFrm.CardType = ddlCardType.Text; 
     objAppFrm.Cardno = txtCardNumber.Text; 
     objAppFrm.CardExpDate =DateTime.Parse(txtCardED.Text); 
     bool blResult = BusinessLayer.ApplicationFormBL.SetApplicationForm(objAppFrm,strPhoto); 
     if (blResult) 
     { 
      lblMsg.Text = "Application applied successfully"; 
     } 
     else 
     { 
      lblMsg.Text = "Not Applied"; 
      ClearApplicationForm(); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

public class ApplicationFormDB 
{ 
    public static bool SetApplicationForm(ApplicationFormEntity objAppFrm,string strPhoto) 
    { 
     try 
     { 
      SqlCommand cmd = new SqlCommand(); 
      cmd.CommandType = CommandType.StoredProcedure; 
      cmd.CommandText = "SetApplicationForm"; 
      cmd.Parameters.AddWithValue("@UserID",objAppFrm.UserID); 
      cmd.Parameters.AddWithValue("@InstitueName", objAppFrm.Institute); 
      cmd.Parameters.AddWithValue("@ResidentalAddress",objAppFrm.ResidentalAddress); 
      cmd.Parameters.AddWithValue("@Place",objAppFrm.Place); 
      cmd.Parameters.AddWithValue("@WorkingDays",objAppFrm.WorkingDays); 
      cmd.Parameters.AddWithValue("@Time",objAppFrm.Time); 
      cmd.Parameters.AddWithValue("@Photo",strPhoto); 
      cmd.Parameters.AddWithValue("@IDProof",objAppFrm.ProofID); 
      cmd.Parameters.AddWithValue("@AppliedDate",objAppFrm.AppliedDate); 
      cmd.Parameters.AddWithValue("@ValiedUpTo",objAppFrm.ValidUpTo); 
      cmd.Parameters.AddWithValue("@CardType", objAppFrm.CardType); 
      cmd.Parameters.AddWithValue("@Cardno", objAppFrm.Cardno); 
      cmd.Parameters.AddWithValue("@CardExpDate", objAppFrm.CardExpDate); 

      int intResullt=DBLayer.ExecuteNonQuery(cmd); 
      if(intResullt>0) 
       return true; 
      else 
       return false; 
     } 
     catch(Exception ex) 
     { 
      throw ex; 
     } 
    } 
+2

我找不到'AccountID'頁面,比錯誤以外的其他任何地方,這意味着你缺少的是在'INSERT'聲明。它是一個身份類型字段?自動號碼? – Habib

+2

錯誤和一些代碼不是問題。 – asawyer

回答

1

TBLAccount期待在AccountID列一個非空值。您存儲的proc和SetApplicationForm方法不處理這個問題。如果您希望它是一個自動增量ID字段,您可能已將該列設置爲錯誤。

1

我認爲您需要製作TBLAccount.AccountID作爲Identity列,如果它的數據類型爲intbigint。否則你需要在你的SP的第二個插入命令中爲它指定一個值。

如果您TBLAccount.AccountIDuiqueidentifier則默認值設置爲(newId())

0

檢查表dbo.TBLAccount模式定義,你應該有與NOT NULL定義的帳戶ID列,你要麼允許空不改變你當前的代碼或者爲它設置的值在當前的代碼:

enter image description here

相關問題