Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:无法在调试中将字符串转换为int32_C#_Asp.net_Sql - Fatal编程技术网

C# 错误:无法在调试中将字符串转换为int32

C# 错误:无法在调试中将字符串转换为int32,c#,asp.net,sql,C#,Asp.net,Sql,我用编辑模板设置了一个测试gridview,以便我的用户能够编辑/更新/添加多个记录。以下是测试结果: 当我点击“更新编辑”时,更改一个值,然后单击“更新”“值不更新”。在update语句上放置断点时,它指向datatable并读取: 无法将字符串转换为int32 该列表定义如下: private int InpatientMeasures = 1; private int OutpatientMeasures = 2; private int HeartAttack = 1; privat

我用编辑模板设置了一个测试gridview,以便我的用户能够编辑/更新/添加多个记录。以下是测试结果:

当我点击“更新编辑”时,更改一个值,然后单击“更新”“值不更新”。在update语句上放置断点时,它指向datatable并读取:
无法将字符串转换为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;
以下是数据表:

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 SiteID=@SiteID, ServiceLineID=@ServiceLineID, Measure=@Measure, MyCompAvg=@MyCompAvg, KYStateAvg=@KYStateAvg, NationalAvg=@NationalAvg where " +
    "num=@num;" + 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
,该变量将数据表设置回更新后图像中显示的select

已编辑-这是请求的堆栈跟踪

位于System.Data.SqlClient.SqlParameter.ImproveValue(对象值、元类型destinationType、Boolean&ImprovedToDataFeed、Boolean&typeChanged、Boolean allowStreaming)
位于System.Data.SqlClient.SqlParameter.GetImpressedValue()处
位于System.Data.SqlClient.SqlParameter.Validate(Int32索引,布尔值isCommandProc)
位于System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser解析器、SqlParameterCollection参数)
位于System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior行为、字符串commandText、SqlParameterCollection参数、_SqlRPC&rpc)
位于System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite)
位于System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法、TaskCompletionSource`1 completion、Int32超时、Task&Task、Boolean asyncWrite)
位于System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法)
位于System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior,String方法)
位于System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior)
位于System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior)
位于System.Data.Common.DbDataAdapter.FillInternal(数据集数据集,数据表[]数据表,Int32 StartRecords,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior)
位于System.Data.Common.DbDataAdapter.Fill(DataTable[]dataTables,Int32 StartRecords,Int32 maxRecords,IDbCommand命令,CommandBehavior)
位于System.Data.Common.DbDataAdapter.Fill(DataTable)
在c:\Users\20002143\Documents\Visual Studio 2012\Projects\QualityOutcomeGraphGen\u test\QualityOutcomeGraphGen\u test\Admin.GetData(SqlCommand cmd)中的QualityOutcomeGraphGen\u test\QualityOutcomeGraphGen\u test\Admin.aspx.cs:第57行
在c:\Users\20002143\Documents\Visual Studio 2012\Projects\QualityOutcomeGraphGen\u test\QualityOutcomeGraphGen\u test\QualityOutcomeGraphGen\u test\Admin.aspx.cs中的QualityOutcomeGraphGen\u test.Admin.updateequalitymeasures(对象发送方,GridViewUpdateEventArgs e)中:第241行
在System.Web.UI.WebControl.GridView.OnRowUpdate(GridViewUpdateEventArgs e)中
位于System.Web.UI.WebControl.GridView.HandleUpdate(GridViewRow行、Int32行索引、布尔原因验证)
位于System.Web.UI.WebControl.GridView.HandleEvent(EventArgs e、Boolean causesValidation、String validationGroup)
位于System.Web.UI.WebControl.GridView.OnBubbleEvent(对象源,EventArgs e)
位于System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs-args)
位于System.Web.UI.WebControl.GridViewRow.OnBubbleEvent(对象源,EventArgs e)
位于System.Web.UI.Control.RaiseBubbleEvent(对象源,EventArgs-args)
在System.Web.UI.WebControl.LinkButton.OnCommand(CommandEventArgs e)中
位于System.Web.UI.WebControl.LinkButton.RaisePostBackEvent(String eventArgument)
位于System.Web.UI.WebControl.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串事件参数)
位于System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)
位于System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)

您的
NewID
变量类型似乎与数据库中的类型不匹配。请尝试将其解析为
整数

cmd.Parameters.Add("@num", SqlDbType.Int).Value = int.Parse(NewID);
您还应该检查其他参数类型,确保它们与相应的
NVarChar
类型匹配,
NText
string
。因此,您的其他参数类型(
SiteID
ServiceLineID
等)应该是
string
尝试以下操作:

cmd.Parameters.Add(new SqlParameter() { DbType = DbType.Int32, ParameterName = "@num", Value = Convert.ToInt32(NewID) });
我不确定ServiceLineID=HeartAttack.ToString()中的心脏病发作是什么即类型或变量。在我看来更像类型。如果我是你,我会将HeartAttackId绑定到下拉列表中的值。例如:

new ListItem() { Text = "Heart Attack", Value = "1" }; // here 1 is HeartAttackId
然后重新整理它就像

string ServiceLineID = ((DropDownList)gvMainView.FooterRow.FindControl("ddlServiceLine")).Value;

我需要找到那一排。所以我改变了

string Mynum = ((Label)gvMainView.FooterRow.FindControl("lblNum")).Text;


现在它工作了

请将您的错误粘贴到堆栈跟踪中。您的错误在gvMainView.DataSource=GetData(cmd)@Alok Stack posted:)@Akrem这在DataTable中有解释,如果按原样复制并粘贴到数据库中,查询是否运行?不要使用@num,试着自己键入一个数字,看看它是否有效。它表示解析包含无效的参数。我编辑了原始帖子。这将使完美,因为如果它还没有定义…这是什么造成我的困惑。它不应该有更新问题
string Mynum = ((Label)gvMainView.Rows[e.RowIndex].FindControl("lblNum")).Text;