Greeting
I faced this problem
No mapping exists from object type System.Web.UI.WebControls.Label to a known managed provider native type
here that code:
- protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
- {
- try
- {
- using (SqlConnection sqlcon = new SqlConnection(connectionString))
- {
- if (e.CommandName.Equals("AddNew"))
- {
- Label llbc = (Label)DetailsView1.FindControl("Lb_cat");
- FileUpload fu = (FileUpload)DetailsView1.FindControl("FU_footer");
- if (fu.HasFile)
- {
- string fileName = fu.FileName;
- string exten = Path.GetExtension(fileName);
- //here we have to restrict file type
- exten = exten.ToLower();
- string[] acceptedFileTypes = new string[4];
- acceptedFileTypes[0] = ".jpg";
- acceptedFileTypes[1] = ".jpeg";
- acceptedFileTypes[2] = ".gif";
- acceptedFileTypes[3] = ".png";
- bool acceptFile = false;
- for (int i = 0; i <= 3; i++)
- {
- if (exten == acceptedFileTypes[i])
- {
- acceptFile = true;
- }
- }
- if (!acceptFile)
- {
- lberrormsg.Text = "The file you are trying to upload is not a permitted file type!";
- }
- else
- {
- //upload the file onto the server
- fu.SaveAs(Server.MapPath("~/images/categories/" + fileName));
- //Open Connection with D.B
- sqlcon.Open();
- string query = "INSERT INTO Products(P_Name,Ingredients,P_Price,P_photo) VALUES(@P_Name,@Ingredients,@P_Price,@P_photo)";
- SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
- sqlcmd.Parameters.AddWithValue("@P_Name", (DetailsView1.FindControl("TB_Name") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@Ingredients", (DetailsView1.FindControl("TB_Ing") as TextBox).Text.Trim());
- //sqlcmd.Parameters.AddWithValue("@P_Enable", Convert.ToBoolean (DetailsView1.FindControl("Ch_enabled") as CheckBox));
- sqlcmd.Parameters.AddWithValue("@P_Price", (DetailsView1.FindControl("TB_Price") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@P_photo", fileName);
- sqlcmd.Parameters.AddWithValue("@C_Id", (DetailsView1.FindControl("Lb_cat") as Label));
- sqlcmd.ExecuteNonQuery();
- DetailsView1.DataBind();
- lbsuccessmsg.Text = "New Record Added";
- lberrormsg.Text = "";
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- lbsuccessmsg.Text = "";
- lberrormsg.Text = ex.Message;
- }
- }
- protected void DetailsView1_DataBound1(object sender, EventArgs e)
- {
- DropDownList dr = (DropDownList)DetailsView1.FindControl("DDLCategory");
- Label lb1 = (Label)DetailsView1.FindControl("Lb_cat");
- lb1.Text = dr.SelectedItem.Value.ToString();
- }
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Rajeesh MenothPosted Nov 10, 2019, 3:16 AM