Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 37.7k

Unable to cast a dropdownlist in GridView and Convertible

Jan 30 2020 5:21 AM
Greeting all
I have a gridView as photo below
 
A position dropdownlist connect alone with (SQLDATASOURC) to get Brunch name and Id and implement them to Dropdownlist......Anyway..when I select any item from dropdownlist showing that mistake
 
Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.IConvertible'.
as photo beow:
 
 I used many many many solutions to resolve that no way..
Have anyone a perfect way to resolve that mistake..
Take a view a bout code below:
  1. protected void gvjobsshow_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.                 using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  6.                 {  
  7.                       
  8.                     sqlcon.Open();  
  9.                       string query = "UPDATE Jobs SET Job_title = @Job_title, Job_desc = @Job_desc,P_date = @P_date, C_date = @C_date,Branch_Id = @Branch_Id, JType_Id = @JType_Id, JEnable = @JEnable WHERE (Jobs.Id = @Id)";  
  10.   
  11.                     string Pday = ((TextBox)gvjobsshow.Rows[e.RowIndex].FindControl("TB_PDate")).Text;  
  12.                     string Cday = ((TextBox)gvjobsshow.Rows[e.RowIndex].FindControl("TBCdate_E")).Text;  
  13.                     //====That solution to get DropDownlist value and imlement it is Variable, and then implement a variable in SQL Upadate=====  
  14.                     DropDownList DDLBrID1 = (DropDownList)gvjobsshow.Rows[e.RowIndex].FindControl("DDl_Po_E");  
  15.                     string DDLBrID2 = DDLBrID1.SelectedItem.Value.ToString();  
  16.                       
  17.                     SqlCommand sqlcmd = new SqlCommand(query, sqlcon);  
  18.                     sqlcmd.Parameters.AddWithValue("@Job_title", (gvjobsshow.Rows[e.RowIndex].FindControl("TB_Tit_E"as TextBox).Text.Trim());  
  19.                     sqlcmd.Parameters.AddWithValue("@Job_desc", (gvjobsshow.Rows[e.RowIndex].FindControl("TbDes_E"as TextBox).Text.Trim());  
  20.                     sqlcmd.Parameters.AddWithValue("@P_date", Pday);  
  21.                     sqlcmd.Parameters.AddWithValue("@C_date", Cday);  
  22.                     sqlcmd.Parameters.AddWithValue("@Branch_Id", DDLBrID2);  
  23.                      
  24.                     //sqlcmd.Parameters.Add("@Branch_Id", SqlDbType.Int).Value = ((DropDownList)gvjobsshow.Rows[e.RowIndex].FindControl("DDl_Po_E"));   
  25.                       
  26.                     //==== I used that solution to access to dropdownlist value and return it with [[Convert.ToInt32]] but no way...  
  27.                     //sqlcmd.Parameters.AddWithValue("@Branch_Id", Convert.ToInt32((gvjobsshow.Rows[e.RowIndex].FindControl("DDl_Po_E"))));  
  28.                     sqlcmd.Parameters.AddWithValue("@JType_Id", Convert.ToInt32((gvjobsshow.Rows[e.RowIndex].FindControl("DDl_Miss_E"))));  
  29.                     sqlcmd.Parameters.AddWithValue("@JEnable", Convert.ToBoolean((gvjobsshow.Rows[e.RowIndex].FindControl("Ch_E"as CheckBox).Checked));  
  30.                     sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(gvjobsshow.DataKeys[e.RowIndex].Value.ToString()));  
  31.                     sqlcmd.ExecuteNonQuery();  
  32.                     gvjobsshow.EditIndex = -1;  
  33.                     GVShowJobs();  
  34.                     JobSuUp.Text = "Selected Record has been Updated.";  
  35.   
  36.                 }  
  37.             }  
  38.             catch (Exception ex)  
  39.             {  
  40.   
  41.                 JobFaUp.Text = ex.Message;  
  42.             }  
  43.         }  
 
 

Answers (6)