【问题标题】:No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type不存在从对象类型 System.Web.UI.WebControls.Label 到已知托管提供程序本机类型的映射
【发布时间】:2016-08-19 05:54:41
【问题描述】:

我试图将 gridview 标签作为参数以根据该标签在 sql 中更新,但它显示错误,因为从对象类型 System.Web.UI.WebControls.Label 到已知托管提供程序本机类型不存在映射。请帮助我提前谢谢下面是我的代码

protected void btnSave_Click(object sender, EventArgs e)
{

    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStrCCUKYC"].ToString()))
    {

        foreach (GridViewRow row in ShowGrid.Rows)
        {


            var Clientid = row.Cells[5].FindControl("lblClientid");

           // var Clientid = row.Cells[5].Text;  Clientid is coming Null 

            //Label Clientid = ((Label)row.FindControl("lblClientid")); No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type.

            CheckBox ckb = (CheckBox)row.FindControl("chkSelect");
            //ckb.Checked = ((CheckBox)sender).Checked;

            if (ckb.Checked==true)
            {

                var check = true;

                SqlCommand cmd = new SqlCommand("sp_InsertRecieved_HOSignaturecard ", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Check", check);
                cmd.Parameters.AddWithValue("@Clientid", Clientid);
                con.Open();
                var res = cmd.ExecuteNonQuery();
                con.Close();
                if (res > 0)
                {

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmsg", "alertify.alert('Data Saved successfully..!')", true);
                }

                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmsg", "alertify.alert('Data NotSaved..!')", true);
                }


            }

            else
            {

                var check = false;

                SqlCommand cmd = new SqlCommand("sp_InsertRecieved_HOSignaturecard ", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Check", check);
                cmd.Parameters.AddWithValue("@Clientid", Clientid);
                con.Open();
                var res = cmd.ExecuteNonQuery();
                con.Close();
                if (res > 0)
                {

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmsg", "alertify.alert('Data Saved successfully..!')", true);
                }

                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmsg", "alertify.alert('Data NotSaved..!')", true);
                }   

            }

        }

    }


}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:
    var lblClientid = row.cells[5].Controls[0].FindControl("lblClientid") as Label;
    if (lblClientid != null)
    {
        // do something if found the control...
        // e.g. assign the value to a variable
        var ClientId = lblClientid.Text;
    }
    

    以上代码示例假定您的 ASPX 标记已正确完成。如果您仍然无法获得正确的值,请在您的问题中提供您的 ASPX 代码。

    更新

    对于 UPDATE、INSERT 和 DELETE 语句,返回值为 受命令影响的行数。当触发器存在于 正在插入或更新的表,返回值包括数字 受插入或更新操作和数量影响的行数 受触发器或触发器影响的行数。对于所有其他类型的 语句,返回值为-1。如果发生回滚,则返回 值也是-1。

    如果您的cmd.ExecuteNonQuery() 返回-1,您需要检查您的存储过程并查看选项SET NOCOUNT 是否打开/关闭。

    更多详情请查看https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

    【讨论】:

    • 添加上述代码后仍然无法正常工作,出现同样的错误:'(
    • 您能提供您的 aspx 标记代码吗?需要知道标签控件的实际放置位置。
    • 还要确保cmd.Parameters.AddWithValue("@Clientid", Clientid); 这一行中的变量ClientId 仍然没有引用标签控件。您需要将客户端 ID 值作为字符串传递。
    • 如果你在if (lblClientid != null)这一行设置一个断点,你在这个变量lblClientId中看到了什么?它是空的吗?也尝试改变你的硬编码索引,5,也许它没有使用正确的列?
    【解决方案2】:

    您必须在参数中使用ClientId.Text,而不是ClientId

    【讨论】:

    • 我使用了 'ClientId.Text' 但错误显示为 'res -1' 但它是 (If (res>0)
    【解决方案3】:

    我得到了答案,他们在读取网格值时没有错误,但错误就在附近

    var check = true;

    应该是 var check = "true";

    感谢woodykiddy的回答。

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多