Anusha Reddy Anusha Reddy - 3 months ago 20
ASP.NET (C#) Question

No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type

iam trying to take gridview label as parameter to update in sql as per that label but it is showing error as No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type. please help me thanks in advance below is my code

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);
}

}

}

}


}

Answer
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;
}

Above code sample assumes that your ASPX markup is done correctly. If you still can't get the correct value, please provide your ASPX code in your question.

Update

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

If your cmd.ExecuteNonQuery() returns -1, you need to check your stored procedure and see whether or not the option SET NOCOUNT is on/off.

For more details, please check https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

Comments