2013-02-22 110 views
0

在我的應用程序中,我使用了使用Web服務的3層體系結構。 我想要在數據庫中插入數據。當插入方法是從該web服務調用它給出了一個錯誤爲「有生成的XML文檔Webservice方法返回錯誤:「生成XML文檔時出錯」

.CS下次點擊事件

protected void btnSave_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      //assinging values to the Object(objEmp) 
      string Result = objEmp.Insert(); 
      if (Result == "1") 
      { 

       Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowMessage", string.Format("<script type='text/javascript'>alert('{0}')</script>", "Record Saved")); 
       Response.Redirect("ViewEmployeeDetails.aspx"); 

      } 
      else 
      { 
       Page.ClientScript.RegisterStartupScript(this.GetType(), "Error1", string.Format("<script type='text/javascript'>alert('{0}')</script>", Result.Replace("'", "\\'"))); 
      } 

     } 
     catch (Exception ex) 
     { 
      Page.ClientScript.RegisterStartupScript(this.GetType(), "Error", string.Format("<script type='text/javascript'>alert('{0}')</script>", ex.Message.ToString())); 
     } 
    } 

類(Emp.cs)

錯誤
public String Insert() 
    { 
     Service.SqlParameter[] param = new Service.SqlParameter[13]; 

     param[0] = new Service.SqlParameter(); 
     param[0].ParameterName = "@Dta_Users"; 
     param[0].Value = Dta_Users; 
     param[0].SqlDbType = Service.SqlDbType.NVarChar; 

     param[1] = new Service.SqlParameter(); 
     param[1].ParameterName = "@FK_EmployeeTypeID"; 
     param[1].Value = FK_EmployeeTypeID; 
     param[1].SqlDbType = Service.SqlDbType.Decimal; 

     param[2] = new Service.SqlParameter(); 
     param[2].ParameterName = "@EmployeeName"; 
     param[2].Value = EmployeeName; 
     param[2].SqlDbType = Service.SqlDbType.NVarChar; 

     param[3] = new Service.SqlParameter(); 
     param[3].ParameterName = "@CellNo"; 
     param[3].Value = CellNo; 
     param[3].SqlDbType = Service.SqlDbType.NVarChar; 

     param[4] = new Service.SqlParameter(); 
     param[4].ParameterName = "@PhNo"; 
     param[4].Value = PhNo; 
     param[4].SqlDbType = Service.SqlDbType.NVarChar; 

     param[5] = new Service.SqlParameter(); 
     param[5].ParameterName = "@Address"; 
     param[5].Value = Address; 
     param[5].SqlDbType = Service.SqlDbType.NVarChar; 

     param[6] = new Service.SqlParameter(); 
     param[6].ParameterName = "@Email"; 
     param[6].Value = Email; 
     param[6].SqlDbType = Service.SqlDbType.NVarChar; 

     param[7] = new Service.SqlParameter(); 
     param[7].ParameterName = "@DOB"; 
     if (DOB == null) 
     { 
      param[7].Value = DBNull.Value; 
     } 
     else 
     { 
      param[7].Value = DOB; 
     } 
     param[7].SqlDbType = Service.SqlDbType.DateTime; 

     param[8] = new Service.SqlParameter(); 
     param[8].ParameterName = "@DOJ"; 
     param[8].Value = DOJ; 
     param[8].SqlDbType = Service.SqlDbType.DateTime; 

     param[9] = new Service.SqlParameter(); 
     param[9].ParameterName = "@DOR"; 
     param[9].Value = DOR; 
     param[9].SqlDbType = Service.SqlDbType.NVarChar; 

     param[10] = new Service.SqlParameter(); 
     param[10].ParameterName = "@Status"; 
     param[10].Value = Status; 
     param[10].SqlDbType = Service.SqlDbType.NVarChar; 

     param[11] = new Service.SqlParameter(); 
     param[11].ParameterName = "@Remarks"; 
     param[11].Value = Remarks; 
     param[11].SqlDbType = Service.SqlDbType.NVarChar; 

     param[12] = new Service.SqlParameter(); 
     param[12].ParameterName = "@Dta_User"; 
     param[12].Value = Dta_User; 
     param[12].SqlDbType = Service.SqlDbType.NVarChar; 

     return s.InsUpdDel(Conn, "Employee_Insert", param); 


    } 

web服務方法 [的WebMethod]

public string InsUpdDel(string Conn, string ProcName, SqlParameter[] p) 
{ 
    try 
    { 
     using (SqlConnection cn = new SqlConnection(Conn)) 
     { 
      if (cn.State == ConnectionState.Open || cn.State == ConnectionState.Broken || cn.State == ConnectionState.Connecting || cn.State == ConnectionState.Executing || cn.State == ConnectionState.Fetching) 
       cn.Close(); 
      cn.Open(); 
      SqlCommand cmd = new SqlCommand(ProcName, cn); 

      cmd.CommandType = CommandType.StoredProcedure; 

      foreach (SqlParameter param in p) 
      { 
       cmd.Parameters.Add(param); 
      } 
      SqlDataReader dr = cmd.ExecuteReader(); 
      dr.Read(); 
      if (dr[0].ToString() == "1") 
      { 
       return "1"; 
      } 
      else 
      { 
       return dr[0].ToString(); 
      } 
      //dr.Close(); 
      cn.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     return "Error : " + ex.Message.ToString(); 
    } 


} 

當我調試save_click事件遊標去catch語句....因爲錯誤是從Web服務。如果我只需添加InsUpdDel()方法Emp.cs和插入調用此方法()方法則誤差不來

如果我寫了下面的代碼,然後它給錯誤

 param[7] = new Service.SqlParameter(); 
     param[7].ParameterName = "@DOB"; 
     if (DOB == null) 
     { 
      param[7].Value = DBNull.Value; 
     } 
     else 
     { 
      param[7].Value = DOB; 
     } 
     param[7].SqlDbType = Service.SqlDbType.DateTime; 

如果我下面寫代碼,然後它不給錯誤

param[7] = new Service.SqlParameter(); 
    param[7].ParameterName = "@DOB"; 
    param[7].Value = DOB; 
    param[7].SqlDbType = Service.SqlDbType.DateTime; 

回答

0

改變這一行:

param[7].Value = DBNull.Value; 

param[7].Value = null; 

,然後在WS方:

foreach (SqlParameter param in p) 
{ 
    if (param.Value == null) 
    { 
      param.Value = DBNull.Value; 
    } 
    cmd.Parameters.Add(param); 
} 
+0

及其與其他應用程序和方法 – Pritesh 2013-02-22 09:23:51

+0

請任何人建議有關錯誤 – Pritesh 2013-02-22 09:53:21

+0

請檢查我的編輯工作,它應該解決您的問題: ) – gzaxx 2013-02-22 10:04:16