我有一个测试gridview设置与编辑模板为我的用户能够编辑/更新/添加几个记录。下面是测试的样子:错误:无法将字符串转换为int32在调试
当我打更新编辑,更改值,然后单击更新值不更新。当我把一个断点上的更新语句它指向的数据表中记载:
private int InpatientMeasures = 1;
private int OutpatientMeasures = 2;
private int HeartAttack = 1;
private int HeartFailure = 2;
private int Pneumonia = 3;
private int SIPrevention = 4;
private int Surgery = 5;
下面是数据表: failed to convert string to int32
名单如下定义
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(conn);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt); //This is where it breaks and gives the error in debug mode
return dt; //The Code will actually work until the line above but will not reach here
}
的调试将不会指定什么int
。我也将@num
变量值转换为一个int值,但它仍然不会更新。
protected void UpdateQualityMeasures(object sender, GridViewUpdateEventArgs e)
{
string Mynum = ((Label)gvMainView.FooterRow.FindControl("lblNum")).Text;
string SiteID = ((DropDownList)gvMainView.FooterRow.FindControl("ddlSite")).Text;
switch (SiteID)
{
case "Inpatient Measures":
SiteID = InpatientMeasures.ToString();
break;
case "Outpatient Measures":
SiteID = OutpatientMeasures.ToString();
break;
}
string ServiceLineID = ((DropDownList)gvMainView.FooterRow.FindControl("ddlServiceLine")).Text;
switch (ServiceLineID)
{
case "Heart Attack":
ServiceLineID = HeartAttack.ToString();
break;
case "Heart Failure":
ServiceLineID = HeartFailure.ToString();
break;
case "Pneumonia":
ServiceLineID = Pneumonia.ToString();
break;
case "Surgical Infection Prevention":
ServiceLineID = SIPrevention.ToString();
break;
case "Surgery":
ServiceLineID = Surgery.ToString();
break;
}
string Measure = ((TextBox)gvMainView.FooterRow.FindControl("txtMeasure")).Text;
string MyCompAvg = ((TextBox)gvMainView.FooterRow.FindControl("txtMyCompAvg")).Text;
string NationalAvg = ((TextBox)gvMainView.FooterRow.FindControl("txtNationalAvg")).Text;
string KYStateAvg = ((TextBox)gvMainView.FooterRow.FindControl("txtKYStateAvg")).Text;
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
string QueryPartB = Measures;
int NewID = Convert.ToInt32(Mynum);
cmd.CommandText = "update quality_Measures set [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] where " +
"[email protected];" + QueryPartB;
cmd.Parameters.Add("@num", SqlDbType.Int).Value = NewID;
cmd.Parameters.Add("@SiteID", SqlDbType.NVarChar).Value = SiteID;
cmd.Parameters.Add("@ServiceLineID", SqlDbType.NVarChar).Value = ServiceLineID;
cmd.Parameters.Add("@Measure", SqlDbType.NText).Value = Measure;
cmd.Parameters.Add("@MyCompAvg", SqlDbType.NVarChar).Value = MyCompAvg;
cmd.Parameters.Add("@KYStateAvg", SqlDbType.NVarChar).Value = KYStateAvg;
cmd.Parameters.Add("@NationalAvg", SqlDbType.NVarChar).Value = NationalAvg;
gvMainView.EditIndex = -1;
gvMainView.DataSource = GetData(cmd);
gvMainView.DataBind();
}
你会发现存在这样说QueryPartB
变量设置数据表回更新后的影像中选择更新的地方。
编辑 - 这里是请求堆栈跟踪
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
at System.Data.SqlClient.SqlParameter.GetCoercedValue()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)
at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
at QualityOutcomeGraphGen_test.Admin.GetData(SqlCommand cmd) in c:\Users\20002143\Documents\Visual Studio 2012\Projects\QualityOutcomeGraphGen_test\QualityOutcomeGraphGen_test\Admin.aspx.cs:line 57
at QualityOutcomeGraphGen_test.Admin.UpdateQualityMeasures(Object sender, GridViewUpdateEventArgs e) in c:\Users\20002143\Documents\Visual Studio 2012\Projects\QualityOutcomeGraphGen_test\QualityOutcomeGraphGen_test\Admin.aspx.cs:line 241
at System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e)
at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
您能否将您的错误粘贴到堆栈跟踪中? – Alok
你的错误在gvMainView行中。DataSource = GetData(cmd); ?? – Akrem
@Alok Stack发布:) – Skullomania