更新gridview时如何防止异常? [英] How to prevent exception when updating the gridview?

查看:72
本文介绍了更新gridview时如何防止异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,并且从存储过程中调用gridview内的内容数据,但是每当我要更新gridview行内容时,就会出现一个异常.更新代码如下:

I have a gridview and the content data inside gridview is called from a stored procedure,but whenever i want to update the gridview row content an Exception appears. The update code is as below:

protected void Grid_Updating(object sender, GridViewUpdateEventArgs e) 
    { 
        SqlCommand cmd = DataProvider.GenerateCommand("Update_HowzeEducation_SP", CommandType.StoredProcedure); 
        cmd.Connection.Open(); 
 
        cmd.Parameters.Add("@FieldName", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; 
        cmd.Parameters.Add("@DegreeId", SqlDbType.Int).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
        cmd.Parameters.Add("@SchoolName", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
        cmd.Parameters.Add("@StudyCityDescribtion", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
        cmd.Parameters.Add("@Average", SqlDbType.Decimal).Value = decimal.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text, CultureInfo.InvariantCulture); 
        cmd.Parameters.Add("@FinishLevelDate", SqlDbType.Date).Value = Convert.ToDateTime(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim()); 
        cmd.Parameters.Add("@ThesisTitle", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text; 
        cmd.Parameters.Add("@Describtion", SqlDbType.NVarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[8].Controls[0]).Text; 
        cmd.Parameters.Add("@HowzeEducationId", SqlDbType.Int).Value = GlobalVariables.HowzeEducationId; 
 
        cmd.ExecuteNonQuery(); 
 
    } 


唯一的例外是:
无法将参数值从字符串转换为Int32.
该消息出现在代码的最后一行.

感谢您的帮助.


And the exception is :
Failed to convert parameter value from a String to a Int32.
This message comes up in the last line of the code.

I will be thankfull for any usefull help.

推荐答案

cmd.Parameters.Add("@HowzeEducationId", SqlDbType.Int).Value = GlobalVariables.HowzeEducationId; 



应该是你的问题.您确定GlobalVariables.HowzeEducationId是整数吗?



There should be your problem. Are you sure that GlobalVariables.HowzeEducationId is an integer?


这篇关于更新gridview时如何防止异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆