Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 35k

problem return RadioList value to control in GridView

Oct 2 2019 11:18 AM
Hello
 I designed a form to add sub-category depends on user select from Main categroy (dropdownlist)
 A gridview designed depends on that video GRIDVIEW (CRUD):
https://www.youtube.com/watch?v=pH7E-GM8HjE
 
when I insert item a fiest error is:
Object reference not set to an instance of an object.
 I anlyising the suppoose error maybe from Id field because maybe be empty.
I designe a new column in gridview to pass value from dropdownlist depends on (Autopostpack) property like these:
 
  1. Literal txtid = (Literal)gvsubcat.FooterRow.FindControl("TBF_footer");    
  2.  txtid = RadioButtonList1.SelectedItem.Text;    
 the error is cannot implicity convert type string to system.web.UI.webcontrol.Literal
 
so there is a new way to solve that mistake. Iam searched long time to solve it, but nothing
here a source code:
  1.             <asp:GridView ID="gvsubcat" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="Id" DataSourceID="SDS_Subcategory" ShowFooter="True" OnRowCancelingEdit="gvsubcat_RowCancelingEdit" OnRowCommand="gvsubcat_RowCommand" OnRowDeleting="gvsubcat_RowDeleting" OnRowEditing="gvsubcat_RowEditing" OnRowUpdating="gvsubcat_RowUpdating">  
  2.                   
  3.                   
  4.                  <FooterStyle BackColor="White" ForeColor="#000066" />  
  5.                 <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />  
  6.                 <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />  
  7.                 <RowStyle ForeColor="#000066" />  
  8.                 <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />  
  9.                 <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  10.                 <SortedAscendingHeaderStyle BackColor="#007DBB" />  
  11.                 <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  12.                 <SortedDescendingHeaderStyle BackColor="#00547E" />  
  13.                 <Columns>  
  14.                        <asp:BoundField DataField="C_Id" HeaderText="C_Id" SortExpression="C_Id"/>--%>  
  15.   
  16.           <asp:TemplateField HeaderText="Id">  
  17.                 <ItemTemplate>  
  18.                      <asp:Label ID="lb_Id" Text='<%#Eval("C_Id") %>' runat="server">asp:Label>  
  19.                    ItemTemplate>  
  20.                    <EditItemTemplate>  
  21.                        <asp:TextBox ID="lbE_Id" runat="server" Text='<%#Eval("C_Id") %>'>asp:TextBox>  
  22.                    EditItemTemplate>  
  23.                    <FooterTemplate>  
  24.                  <asp:Literal ID="TBF_footer" runat="server">asp:Literal>  
  25.                 FooterTemplate>  
  26.                     asp:TemplateField>  
  27.   
  28.                     <asp:TemplateField HeaderText="Name">  
  29.                 <ItemTemplate>  
  30.                      <asp:Label ID="lb_Na" Text='<%#Eval("Su_Name") %>' runat="server">asp:Label>  
  31.                    ItemTemplate>  
  32.                    <EditItemTemplate>  
  33.                        <asp:TextBox ID="Name_Edit" runat="server" Text='<%#Eval("Su_Name") %>'>asp:TextBox>  
  34.                    EditItemTemplate>  
  35.                    <FooterTemplate>  
  36.                        <asp:TextBox ID="Name_footer" runat="server">asp:TextBox>  
  37.                FooterTemplate>  
  38.   
  39.                     asp:TemplateField>  
  40.                     <asp:TemplateField HeaderText="Ingredints">  
  41.                     <ItemTemplate>  
  42.                      <asp:Label ID="Lb_Ing" Text='<%#Eval("Ingredients") %>' runat="server">asp:Label>  
  43.                    ItemTemplate>  
  44.                    <EditItemTemplate>  
  45.                        <asp:TextBox ID="Ing_Edit" runat="server" Text='<%#Eval("Ingredients") %>'>asp:TextBox>  
  46.                    EditItemTemplate>  
  47.                    <FooterTemplate>  
  48.                        <asp:TextBox ID="Ing_footer" runat="server">asp:TextBox>  
  49.                    FooterTemplate>  
  50.   
  51.                     asp:TemplateField>  
  52.                     <asp:TemplateField HeaderText="price">  
  53.                    <ItemTemplate>  
  54.                      <asp:Label ID="Lb_pr" Text='<%#Eval("Price") %>' runat="server">asp:Label>  
  55.                    ItemTemplate>  
  56.                    <EditItemTemplate>  
  57.                        <asp:TextBox ID="Pri_Edit" runat="server" Text='<%#Eval("Price") %>'>asp:TextBox>  
  58.                    EditItemTemplate>  
  59.                    <FooterTemplate>  
  60.                        <asp:TextBox ID="Pri_footer" runat="server">asp:TextBox>  
  61.                    FooterTemplate>  
  62.                     asp:TemplateField>  
  63.   
  64.                     <asp:TemplateField HeaderText="Image">  
  65.                    <ItemTemplate>  
  66.                        <asp:Image ID="Img_item" runat="server" Height="80px" Width="80px" ImageUrl='<%#Eval("Image") %>' />  
  67.                    ItemTemplate>  
  68.                    <EditItemTemplate>  
  69.                        <asp:FileUpload ID="FU_Edit" runat="server" Visible="false"/>  
  70.                    EditItemTemplate>  
  71.                    <FooterTemplate>  
  72.                        <asp:FileUpload ID="FU_footer" runat="server" />  
  73.                    FooterTemplate>  
  74.   
  75.                     asp:TemplateField>  
  76.                     <asp:TemplateField HeaderText="Enable">  
  77.                   <ItemTemplate>  
  78.                 <asp:CheckBox ID="Ch_SUcat" runat="server" Enabled='<%#Eval("Visible") %>' />  
  79.             ItemTemplate>  
  80.             <EditItemTemplate>  
  81.                  <asp:CheckBox ID="Ch_SUcat_E" runat="server" Enabled='<%#Eval("Visible") %>' />  
  82.             EditItemTemplate>  
  83.             <FooterTemplate>  
  84.                  <asp:CheckBox ID="Ch_SUcat_Footer" runat="server" />  
  85.             FooterTemplate>  
  86.   
  87.                     asp:TemplateField>  
  88.                     <asp:TemplateField>  
  89.               <ItemTemplate>  
  90.               <asp:ImageButton ID="imgb1" runat="server" ImageUrl="~/images/png/pencil.png" CommandName="Edit" Width="20px" Height="20px" />  
  91.              <asp:ImageButton ID="imgb2"  runat="server" ImageUrl="~/images/png/delete.png" CommandName="Delete" Width="20px" Height="20px" />  
  92.              ItemTemplate>  
  93.             <EditItemTemplate>  
  94.               <asp:ImageButton ID="imgb3"  runat="server" ImageUrl="~/images/png/saveblue.png" CommandName="Update" Width="20px" Height="20px" />  
  95.              <asp:ImageButton ID="imgb4"   runat="server" ImageUrl="~/images/png/cancel.png" CommandName="Cancel" Width="20px" Height="20px" />  
  96.             EditItemTemplate>  
  97.             <FooterTemplate>  
  98.               <asp:ImageButton ID="imgb5" runat="server" ImageUrl="~/images/png/addblue.png" CommandName="AddNew" Width="20px" Height="20px" />  
  99.                 FooterTemplate>  
  100.   
  101.                     asp:TemplateField>  
  102.                 Columns>  
  103.        
  104.             asp:GridView>  
  105. <asp:ValidationSummary ID="VSummry" runat="server" ForeColor="Red" />  
  106.             <asp:SqlDataSource ID="SDS_Subcategory" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM Sub_categories WHERE (C_Id = @C_Id)" InsertCommand="INSERT INTO [Sub_categories] ([Su_Name], [Ingredients], [Price], [Image], [Visible], [C_Id]) VALUES (@Su_Name, @Ingredients, @Price, @Image, @Visible, @C_Id)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [Su_Name], [Ingredients], [Price], [Image], [Visible], [C_Id] FROM [Sub_categories] WHERE ([C_Id] = @C_Id)" UpdateCommand="UPDATE Sub_categories SET Su_Name = @Su_Name, Ingredients = @Ingredients, Price = @Price, Image = @Image, Visible = @Visible FROM Sub_categories INNER JOIN Categories ON Sub_categories.C_Id = Categories.C_Id WHERE (Sub_categories.C_Id = @C_Id)">  
  107.                 <DeleteParameters>  
  108.                     <asp:Parameter Name="C_Id" />  
  109.                 DeleteParameters>  
  110.                 <InsertParameters>  
  111.                     <asp:Parameter Name="Su_Name" Type="String" />  
  112.                     <asp:Parameter Name="Ingredients" Type="String" />  
  113.                     <asp:Parameter Name="Price" Type="Decimal" />  
  114.                     <asp:Parameter Name="Image" Type="String" />  
  115.                     <asp:Parameter Name="Visible" Type="Boolean" />  
  116.                     <asp:Parameter Name="C_Id" Type="Int32" />  
  117.                 InsertParameters>  
  118.                 <SelectParameters>  
  119.                     <asp:ControlParameter ControlID="RadioButtonList1" Name="C_Id" PropertyName="SelectedValue" Type="Int32" />  
  120.                 SelectParameters>  
  121.                 <UpdateParameters>  
  122.                     <asp:Parameter Name="Su_Name" Type="String" />  
  123.                     <asp:Parameter Name="Ingredients" Type="String" />  
  124.                     <asp:Parameter Name="Price" Type="Decimal" />  
  125.                     <asp:Parameter Name="Image" Type="String" />  
  126.                     <asp:Parameter Name="Visible" Type="Boolean" />  
  127.                     <asp:Parameter Name="C_Id" Type="Int32" />  
  128.                 UpdateParameters>  
  129.             asp:SqlDataSource>  
  130.       
  131. <br />  
  132. <asp:Label ID="lblsuccessmessage" Text="" runat="server" ForeColor="Green">asp:Label><br />  
  133. <asp:Label ID="lblerrormessage" Text="" runat="server" ForeColor="Red">asp:Label>  
Code Behind:
  1. public partial class sub_category : System.Web.UI.Page  
  2. {  
  3.     string connectionString = @"Data Source=(local)\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbCh.mdf;Integrated Security=True";  
  4.   
  5.     protected void Page_Load(object sender, EventArgs e)  
  6.     {  
  7.         if (!IsPostBack)  
  8.         {  
  9.             gvsubcat.DataBind();  
  10.         }  
  11.     }  
  12.   
  13.     protected void gvsubcat_RowCommand(object sender, GridViewCommandEventArgs e)  
  14.     {  
  15.         try  
  16.         {  
  17.               
  18.             if (e.CommandName.Equals("AddNew"))  
  19.             {  
  20.                 using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  21.                 {  
  22.                     FileUpload fu = (FileUpload)gvsubcat.FooterRow.FindControl("FU_footer");  
  23.                     if (fu.HasFile)  
  24.                     {  
  25.                         string fileName = fu.FileName;  
  26.                         string exten = Path.GetExtension(fileName);  
  27.                         //here we have to restrict file type              
  28.                         exten = exten.ToLower();  
  29.                         string[] acceptedFileTypes = new string[4];  
  30.                         acceptedFileTypes[0] = ".jpg";  
  31.                         acceptedFileTypes[1] = ".jpeg";  
  32.                         acceptedFileTypes[2] = ".gif";  
  33.                         acceptedFileTypes[3] = ".png";  
  34.                         bool acceptFile = false;  
  35.                         for (int i = 0; i <= 3; i++)  
  36.                         {  
  37.                             if (exten == acceptedFileTypes[i])  
  38.                             {  
  39.                                 acceptFile = true;  
  40.                             }  
  41.                         }  
  42.                         if (!acceptFile)  
  43.                         {  
  44.                             lblerrormessage.Text = "The file you are trying to upload is not a permitted file type!";  
  45.                         }  
  46.                         else  
  47.                         {  
  48.                             //upload the file onto the server                     
  49.                             fu.SaveAs(Server.MapPath("~/images/categories/" + fileName));  
  50.                             //Open Connection with D.B  
  51.                             sqlcon.Open();  
  52.                              string query = "INSERT INTO Sub_categories(Su_Name,Ingredients,Price,Image,Visible,C_Id) VALUES(@Su_Name,@Ingredients,@Price,@Image,@Visible,@C_Id)";  
  53.                             SqlCommand sqlcmd = new SqlCommand(query, sqlcon);  
  54.                             sqlcmd.Parameters.AddWithValue("@C_Id", (gvsubcat.FooterRow.FindControl("TBF_footer"as TextBox).Text.Trim());  
  55.                             sqlcmd.Parameters.AddWithValue("@Su_Name", (gvsubcat.FooterRow.FindControl("Name_footer"as TextBox).Text.Trim());  
  56.                             sqlcmd.Parameters.AddWithValue("@Ingredients", (gvsubcat.FooterRow.FindControl("Ing_footer"as TextBox).Text.Trim());  
  57.                             sqlcmd.Parameters.AddWithValue("@Price", (gvsubcat.FooterRow.FindControl("Pri_footer"as TextBox).Text.Trim());  
  58.                             sqlcmd.Parameters.AddWithValue("@Image", fileName);  
  59.                             //sqlcmd.Parameters.AddWithValue("@Visible", Convert.ToBoolean((gvsubcat.FooterRow.FindControl("Ch_SUcat_Footer") as CheckBox)));  
  60.                             sqlcmd.Parameters.AddWithValue("@Visible", (gvsubcat.FooterRow.FindControl("Ch_SUcat_Footer.text"as CheckBox));  
  61.   
  62.                             sqlcmd.ExecuteNonQuery();  
  63.                             gvsubcat.DataBind();  
  64.                             lblsuccessmessage.Text = "New Record Added";  
  65.                             lblerrormessage.Text = "";  
  66.   
  67.                         }  
  68.                     }  
  69.                 }  
  70.             }  
  71.         }  
  72.         catch (Exception ex)  
  73.         {  
  74.   
  75.             lblsuccessmessage.Text = "";  
  76.             lblerrormessage.Text = ex.Message;  
  77.         }  
  78.     }  
  79.   
  80.     protected void gvsubcat_RowEditing(object sender, GridViewEditEventArgs e)  
  81.     {  
  82.         gvsubcat.EditIndex = e.NewEditIndex;  
  83.         gvsubcat.DataBind();  
  84.     }  
  85.   
  86.     protected void gvsubcat_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  87.     {  
  88.         gvsubcat.EditIndex = -1;  
  89.         gvsubcat.DataBind();  
  90.     }  
  91.   
  92.     protected void gvsubcat_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  93.     {  
  94.         try  
  95.         {  
  96.             using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  97.             {  
  98.                 sqlcon.Open();  
  99.                 string query = "DELETE FROM Categories where Sub_categories.Id =@Id";  
  100.                 SqlCommand sqlcmd = new SqlCommand(query, sqlcon);  
  101.                 sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(gvsubcat.DataKeys[e.RowIndex].Value.ToString()));  
  102.                 sqlcmd.ExecuteNonQuery();  
  103.                 gvsubcat.DataBind();  
  104.                 lblsuccessmessage.Text = "Selected Record has been Deleted.";  
  105.                 lblerrormessage.Text = "";  
  106.             }  
  107.         }  
  108.         catch (Exception ex)  
  109.         {  
  110.             lblsuccessmessage.Text = "";  
  111.             lblerrormessage.Text = ex.Message;  
  112.         }  
  113.     }  
  114.   
  115.     protected void gvsubcat_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  116.     {  
  117.         try  
  118.         {  
  119.             using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  120.             {  
  121.                 sqlcon.Open();  
  122.                 string query = "UPDATE Sub_categories SET Su_Name=@Su_Name, Ingredients=@Ingredients, Price=@Price, Image=@Image, Visible=@Visible where Sub_categories.Id=@Id";  
  123.                 SqlCommand sqlcmd = new SqlCommand(query, sqlcon);  
  124.                 sqlcmd.Parameters.AddWithValue("@Su_Name", (gvsubcat.FooterRow.FindControl("Name_Edit"as TextBox));  
  125.                 sqlcmd.Parameters.AddWithValue("@Ingredients", (gvsubcat.FooterRow.FindControl("Ing_Edit"as TextBox));  
  126.                 sqlcmd.Parameters.AddWithValue("@Price", (gvsubcat.FooterRow.FindControl("Pri_Edit"as TextBox));  
  127.                 sqlcmd.Parameters.AddWithValue("@Image", (gvsubcat.FooterRow.FindControl("FU_Edit")as FileUpload));  
  128.                 sqlcmd.Parameters.AddWithValue("@Visible", Convert.ToBoolean((gvsubcat.FooterRow.FindControl("Ch_SUcat_E"as CheckBox).Checked));  
  129.                 FileUpload fu = (FileUpload)gvsubcat.FindControl("FU_Edit");  
  130.   
  131.                 if (fu.HasFile)  
  132.                 {  
  133.   
  134.                     string file = System.IO.Path.Combine(Server.MapPath("~/images/categories/"), fu.FileName);  
  135.                     fu.SaveAs(file);  
  136.   
  137.                 }  
  138.   
  139.                 sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(gvsubcat.DataKeys[e.RowIndex].Value.ToString()));  
  140.   
  141.                 sqlcmd.ExecuteNonQuery();  
  142.                 gvsubcat.EditIndex = -1;  
  143.                 gvsubcat.DataBind();  
  144.                 lblsuccessmessage.Text = "Selected Record has been Updated.";  
  145.                 lblerrormessage.Text = "";  
  146.             }  
  147.         }  
  148.         catch (Exception ex)  
  149.         {  
  150.   
  151.             lblsuccessmessage.Text = "";  
  152.             lblerrormessage.Text = ex.Message;  
  153.         }  
  154.   
  155.     }  
  156.   
  157.     protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)  
  158.     {  
  159.         Literal txtid = (Literal)gvsubcat.FooterRow.FindControl("TBF_footer");  
  160.         txtid = RadioButtonList1.SelectedItem.Text;  
  161.   
  162.     }  
  163. }  
 
 
 
 

Answers (2)