ASP.Net Error: No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type

Last Reply 2 months ago By dharmendr

Posted 2 months ago

when i am trying to insert master table id into child table then this error is coming that 

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

on this line

_PID = Convert.ToInt32(cmd.ExecuteScalar());
here is inserting code into DB
        protected void Save_Click(object sender, EventArgs e)
        {
            int _PID = 0;
            using (SqlCommand cmd = new SqlCommand("Insert_Pack", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PID", IPID);
                cmd.Parameters.AddWithValue("@PName", txtName);

                con.Open();
                _PID = Convert.ToInt32(cmd.ExecuteScalar());


                DataTable dt = (DataTable)ViewState["dt"];
                int codeitem, qty;

                foreach (DataRow row in dt.Rows)
                {
                    qty = int.Parse(row["QTY"].ToString());
                    codeitem = int.Parse(row["Codeitem"].ToString());
             
                    this.InsertRows(codeitem, qty);
                }
            }
        }        
        private void InsertRows(int codeitem, int qty)
        {
            using (SqlCommand cmd = new SqlCommand("Insert_PackDetail", con))
            {
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PID", _PID );
                cmd.Parameters.AddWithValue("@CodeItem", codeitem);
                cmd.Parameters.AddWithValue("@QTY", qty);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
        public object _PID { get; set; }
    }

 

 

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago

Hi akhter,

You are passing txtName which is an object of TextBox, thus you are getting exception.

akhter says:
cmd.Parameters.AddWithValue("@PName", txtName);

Change above with the below code.

cmd.Parameters.AddWithValue("@PName", txtName.Text);