在我的应用程序中,我使用了使用 Web 服务的 3 层架构。我想在数据库中插入数据。当从 web 服务调用 insert 方法时,它会给出错误“生成 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);
}
网络服务方法 [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 服务。如果我只是在 Emp.cs 中添加 InsUpdDel() 方法并在 Insert() 方法中调用此方法,则不会出现错误
如果我写下面的代码,那么它给出的错误
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;