在我的应用程序中,我使用了Web服务的3层架构。 我想在数据库中插入数据。从webservice调用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;
答案 0 :(得分: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);
}