求分析,这块代码出有关问题了,点击按钮没反应
求分析,这块代码出问题了,点击按钮没反应
protected void btn_save_Click(object sender, EventArgs e)
{
SqlConnection updateCon = BaseDate.returnSqlCon();
//SqlCommand upCom = new SqlCommand();
//SqlTransaction trans = null;
if (repeatTable.Rows.Count <= 0)
{ return; }
try
{
updateCon.Open();
//trans = updateCon.BeginTransaction();
for (int i = 0; i < repeatTable.Rows.Count; i++)
{
int pid = int.Parse(repeatTable.Rows[i]["pID"].ToString().Trim());//序号
string pName = repeatTable.Rows[i]["pName"].ToString().Trim();//姓名
int gbleid = int.Parse(repeatTable.Rows[i]["jzgwid"].ToString().Trim());//
string gbleName = repeatTable.Rows[i]["jzgwlb"].ToString().Trim();
float xld_szdfc = float.Parse(repeatTable.Rows[i]["xld_szdf"].ToString().Trim());//述职答辩
float xld_mzcpc = float.Parse(repeatTable.Rows[i]["xld_mzcp"].ToString().Trim());//*测评
int xld_typsc = int.Parse(repeatTable.Rows[i]["xld_typs"].ToString().Trim());//同意票数
int xld_btypsc = int.Parse(repeatTable.Rows[i]["xld_btyps"].ToString().Trim());//不同意票数
float cj_szdfc = float.Parse(repeatTable.Rows[i]["cj_szdf"].ToString().Trim());
float cj_mzcpc = float.Parse(repeatTable.Rows[i]["cj_mzcp"].ToString().Trim());
int cj_typsc = int.Parse(repeatTable.Rows[i]["cj_typs"].ToString().Trim());
int cj_btypsc = int.Parse(repeatTable.Rows[i]["cj_btyps"].ToString().Trim());
float js_szdfc = float.Parse(repeatTable.Rows[i]["js_szdf"].ToString().Trim());
float js_mzcpc = float.Parse(repeatTable.Rows[i]["js_mzcp"].ToString().Trim());
int js_typsc = int.Parse(repeatTable.Rows[i]["js_typs"].ToString().Trim());
int js_btypsc = int.Parse(repeatTable.Rows[i]["js_btyps"].ToString().Trim());
float hj_szdfc = float.Parse(repeatTable.Rows[i]["hj_szdf"].ToString().Trim());
float hj_mzcpc = float.Parse(repeatTable.Rows[i]["hj_mzcp"].ToString().Trim());
int hj_typsc = int.Parse(repeatTable.Rows[i]["hj_typs"].ToString().Trim());
int hj_btypsc = int.Parse(repeatTable.Rows[i]["hj_btyps"].ToString().Trim());
float zhdfc = float.Parse(repeatTable.Rows[i]["zhdf"].ToString().Trim());
DateTime zr_date = DateTime.Parse(repeatTable.Rows[i]["pdate"].ToString().Trim());
string InsertString = "INSERT INTO [T_zongheResult] ([pID], [pName], [jzgwid], [jzgwlb], [xld_szdf], [xld_mzcp], [xld_typs], [xld_btyps], [cj_szdf], [cj_mzcp], [cj_typs], [cj_btyps], [js_szdf], [js_mzcp], [js_typs], [js_btyps], [hj_szdf], [hj_mzcp], [hj_typs], [hj_btyps], [zhdf],[zr_date]) VALUES (" +
pid + ",'" + pName + "'," + gbleid + ",'" + gbleName + "'," + xld_szdfc + "," + xld_mzcpc + "," + xld_typsc + "," + xld_btypsc + ","
+ cj_szdfc + "," + cj_mzcpc + "," + cj_typsc + "," + cj_btypsc + "," + js_szdfc + "," + js_mzcpc + "," + js_typsc + "," + js_btypsc + ","
+ hj_szdfc + "," + hj_mzcpc + "," + hj_typsc + "," + hj_btypsc + "," + zhdfc + ","+ zr_date+")";
SqlCommand mycom = new SqlCommand(InsertString, updateCon);
mycom.ExecuteNonQuery();
//upCom.Connection = updateCon;
//upCom.CommandText = InsertString;
////upCom.Transaction = trans;
//upCom.ExecuteNonQuery();
}
//trans.Commit();
btn_save.Enabled = false;
Response.Write("<script language = 'javascript'> alert('恭喜--提交数据成功!') </script>");
}
catch(SqlException ex)
{
Response.Write("<script language = 'javascript'> alert('出错啦!错误的原因是:"+ ex.Message.ToString()+"') </script>");
}
}
我想实现的功能是:通过遍历取表的数据,然后将数据保存到数据库。但是点击按钮没任何反应,也不报错。
------解决思路----------------------
你在插入语句设个断点,看看数据取得是不是完整,
写入插入函数:
public void insertdata(string s1, string s2, string s3)//插入数据库
{
SqlConnection con = new SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False");
con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con);
if ((int)cmd.ExecuteScalar() > 0)
{
listBox1.Items.Add(s1 + " 数据已经存在");
}
else
{
string sql = "insert into newtable(a,b,c) values('" + s1 + "','" + s2 + "','" + s3 +"')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
listBox1.Items.Add(s1 + " 成功添加");
}
cmd.Dispose();
con.Close();
}
------解决思路----------------------
有错误信息吗?还是调试一下吧
------解决思路----------------------
个人建议,
1.try...catch范围过大,而且例外没有捕获全。建议把代码再整理下。
2.int.parse如果有问题,会抛ArgumentNullException,FormatException,OverflowException等等。
建议换成int.TryParse。
------解决思路----------------------
单步跟踪一下,应该比较容易找到问题的吧。
------解决思路----------------------
我猜想问题出在你的一堆parse里面
------解决思路----------------------
断点调试一下呗,看看你的sql语句对不对~
------解决思路----------------------
把你的catch变成catch(Exception ex),
你现在
------解决思路----------------------
单步一下啊,应该很快找出问题了
protected void btn_save_Click(object sender, EventArgs e)
{
SqlConnection updateCon = BaseDate.returnSqlCon();
//SqlCommand upCom = new SqlCommand();
//SqlTransaction trans = null;
if (repeatTable.Rows.Count <= 0)
{ return; }
try
{
updateCon.Open();
//trans = updateCon.BeginTransaction();
for (int i = 0; i < repeatTable.Rows.Count; i++)
{
int pid = int.Parse(repeatTable.Rows[i]["pID"].ToString().Trim());//序号
string pName = repeatTable.Rows[i]["pName"].ToString().Trim();//姓名
int gbleid = int.Parse(repeatTable.Rows[i]["jzgwid"].ToString().Trim());//
string gbleName = repeatTable.Rows[i]["jzgwlb"].ToString().Trim();
float xld_szdfc = float.Parse(repeatTable.Rows[i]["xld_szdf"].ToString().Trim());//述职答辩
float xld_mzcpc = float.Parse(repeatTable.Rows[i]["xld_mzcp"].ToString().Trim());//*测评
int xld_typsc = int.Parse(repeatTable.Rows[i]["xld_typs"].ToString().Trim());//同意票数
int xld_btypsc = int.Parse(repeatTable.Rows[i]["xld_btyps"].ToString().Trim());//不同意票数
float cj_szdfc = float.Parse(repeatTable.Rows[i]["cj_szdf"].ToString().Trim());
float cj_mzcpc = float.Parse(repeatTable.Rows[i]["cj_mzcp"].ToString().Trim());
int cj_typsc = int.Parse(repeatTable.Rows[i]["cj_typs"].ToString().Trim());
int cj_btypsc = int.Parse(repeatTable.Rows[i]["cj_btyps"].ToString().Trim());
float js_szdfc = float.Parse(repeatTable.Rows[i]["js_szdf"].ToString().Trim());
float js_mzcpc = float.Parse(repeatTable.Rows[i]["js_mzcp"].ToString().Trim());
int js_typsc = int.Parse(repeatTable.Rows[i]["js_typs"].ToString().Trim());
int js_btypsc = int.Parse(repeatTable.Rows[i]["js_btyps"].ToString().Trim());
float hj_szdfc = float.Parse(repeatTable.Rows[i]["hj_szdf"].ToString().Trim());
float hj_mzcpc = float.Parse(repeatTable.Rows[i]["hj_mzcp"].ToString().Trim());
int hj_typsc = int.Parse(repeatTable.Rows[i]["hj_typs"].ToString().Trim());
int hj_btypsc = int.Parse(repeatTable.Rows[i]["hj_btyps"].ToString().Trim());
float zhdfc = float.Parse(repeatTable.Rows[i]["zhdf"].ToString().Trim());
DateTime zr_date = DateTime.Parse(repeatTable.Rows[i]["pdate"].ToString().Trim());
string InsertString = "INSERT INTO [T_zongheResult] ([pID], [pName], [jzgwid], [jzgwlb], [xld_szdf], [xld_mzcp], [xld_typs], [xld_btyps], [cj_szdf], [cj_mzcp], [cj_typs], [cj_btyps], [js_szdf], [js_mzcp], [js_typs], [js_btyps], [hj_szdf], [hj_mzcp], [hj_typs], [hj_btyps], [zhdf],[zr_date]) VALUES (" +
pid + ",'" + pName + "'," + gbleid + ",'" + gbleName + "'," + xld_szdfc + "," + xld_mzcpc + "," + xld_typsc + "," + xld_btypsc + ","
+ cj_szdfc + "," + cj_mzcpc + "," + cj_typsc + "," + cj_btypsc + "," + js_szdfc + "," + js_mzcpc + "," + js_typsc + "," + js_btypsc + ","
+ hj_szdfc + "," + hj_mzcpc + "," + hj_typsc + "," + hj_btypsc + "," + zhdfc + ","+ zr_date+")";
SqlCommand mycom = new SqlCommand(InsertString, updateCon);
mycom.ExecuteNonQuery();
//upCom.Connection = updateCon;
//upCom.CommandText = InsertString;
////upCom.Transaction = trans;
//upCom.ExecuteNonQuery();
}
//trans.Commit();
btn_save.Enabled = false;
Response.Write("<script language = 'javascript'> alert('恭喜--提交数据成功!') </script>");
}
catch(SqlException ex)
{
Response.Write("<script language = 'javascript'> alert('出错啦!错误的原因是:"+ ex.Message.ToString()+"') </script>");
}
}
我想实现的功能是:通过遍历取表的数据,然后将数据保存到数据库。但是点击按钮没任何反应,也不报错。
------解决思路----------------------
你在插入语句设个断点,看看数据取得是不是完整,
写入插入函数:
public void insertdata(string s1, string s2, string s3)//插入数据库
{
SqlConnection con = new SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False");
con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con);
if ((int)cmd.ExecuteScalar() > 0)
{
listBox1.Items.Add(s1 + " 数据已经存在");
}
else
{
string sql = "insert into newtable(a,b,c) values('" + s1 + "','" + s2 + "','" + s3 +"')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
listBox1.Items.Add(s1 + " 成功添加");
}
cmd.Dispose();
con.Close();
}
------解决思路----------------------
有错误信息吗?还是调试一下吧
------解决思路----------------------
个人建议,
1.try...catch范围过大,而且例外没有捕获全。建议把代码再整理下。
2.int.parse如果有问题,会抛ArgumentNullException,FormatException,OverflowException等等。
建议换成int.TryParse。
------解决思路----------------------
单步跟踪一下,应该比较容易找到问题的吧。
------解决思路----------------------
我猜想问题出在你的一堆parse里面
------解决思路----------------------
断点调试一下呗,看看你的sql语句对不对~
------解决思路----------------------
把你的catch变成catch(Exception ex),
你现在
catch(SqlException ex),只是捕捉sqlException
------解决思路----------------------
单步一下啊,应该很快找出问题了