8.2 启动与执行业务流程
8.2.2 TaskInstance.aspx.cs文件代码:
......(续前页)
//保存可编辑字段的值
bool Savefields(string relatedtable,string identityfield,string fieldvalue,
string processinstanceid)
{
string strSql;
Base basecode=new Base();
string datatype;
int datalenth;
//如果任务实例包含可编辑的业务表字段
if(wfields != null)
{
strSql="update "+relatedtable+" set "
+wfields.Tables[0].Rows[0]["FieldName"].ToString().Trim()
+"=@"+wfields.Tables[0].Rows[0]["FieldName"].ToString().Trim();
for(int i=1;i<wfields.Tables[0].Rows.Count;i++)
{
strSql=strSql+","+wfields.Tables[0].Rows[i]["FieldName"].ToString().Trim()
+"=@"+wfields.Tables[0].Rows[i]["FieldName"].ToString().Trim();
}
strSql=strSql+" where "+identityfield+"="+fieldvalue;
try
{
SqlCommand cmd=new SqlCommand(strSql);
for(int i=0;i<wfields.Tables[0].Rows.Count;i++)
{
//根据字段的数据类型设置参数并验证
datatype=wfields.Tables[0].Rows[i]["DataType"].ToString();
if(datatype == "int")
{
cmd.Parameters.Add(new SqlParameter("@"
+wfields.Tables[0].Rows[i]["FieldName"].ToString(),SqlDbType.Int));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=int.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为整数!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "smallint")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.SmallInt));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=Int16.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为整数!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "bigint")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.BigInt));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=Int64.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为整数!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "datetime")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.DateTime));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DateTime.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为日期,请输入类似1900-1-1的日期格式!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "smalldatetime")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.SmallDateTime));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DateTime.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为日期,请输入类似1900-1-1的日期格式!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "decimal")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.Decimal));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=Decimal.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为小数!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "float")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.Float));
if(((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=float.Parse(((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim());
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"应该为浮点数!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=DBNull.Value;
}
else if(datatype == "char" || datatype == "nchar" || datatype == "ntext"
|| datatype == "nvarchar" || datatype == "varchar" || datatype == "text")
{
cmd.Parameters.Add(new SqlParameter("@"+wfields.Tables[0]
.Rows[i]["FieldName"].ToString(),SqlDbType.VarChar,
int.Parse(wfields.Tables[0].Rows[i]["DataLenth"].ToString())));
if( ((TextBox)this.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() != "")
{
byte[] s = System.Text.Encoding.Default.GetBytes( ((TextBox)this
.FindControl(wfields.Tables[0].Rows[i]["FieldName"]
.ToString())).Text.Trim() );
datalenth=s.Length;
if(datalenth > int.Parse(wfields.Tables[0].Rows[i]["DataLenth"]
.ToString()))
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"字符长度超过限制!";
return false;
}
try
{
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value=((TextBox)this.FindControl(wfields.Tables[0]
.Rows[i]["FieldName"].ToString())).Text.Trim();
}
catch(System.FormatException ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"字符长度超过限制!";
return false;
}
catch(System.Exception ex)
{
Label2.Text=wfields.Tables[0].Rows[i]["FieldAlias"].ToString()
+"数据输入错误!";
return false;
}
}
else
cmd.Parameters["@"+wfields.Tables[0].Rows[i]["FieldName"]
.ToString()].Value="";
}
else//其它数据类型
{
Label2.Text="系统不支持"+wfields.Tables[0].Rows[i]["FieldAlias"]
.ToString()+"的数据类型,请与管理员联系!";
return false;
}
}//for
if(! basecode.SQLExeNonQuery_proc(cmd))
{
Label2.Text="7:"+basecode.BaseSqlErrDes;
return false;
}
}
catch(System.FormatException exp)
{
Label2.Text="8:"+exp.Message;
return false;
}
}
return true;
}