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:
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:
- Literal txtid = (Literal)gvsubcat.FooterRow.FindControl("TBF_footer");
- 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:
- <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">
- <FooterStyle BackColor="White" ForeColor="#000066" />
- <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
- <RowStyle ForeColor="#000066" />
- <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#F1F1F1" />
- <SortedAscendingHeaderStyle BackColor="#007DBB" />
- <SortedDescendingCellStyle BackColor="#CAC9C9" />
- <SortedDescendingHeaderStyle BackColor="#00547E" />
- <Columns>
- <asp:BoundField DataField="C_Id" HeaderText="C_Id" SortExpression="C_Id"/>--%>
- <asp:TemplateField HeaderText="Id">
- <ItemTemplate>
- <asp:Label ID="lb_Id" Text='<%#Eval("C_Id") %>' runat="server">asp:Label>
- ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="lbE_Id" runat="server" Text='<%#Eval("C_Id") %>'>asp:TextBox>
- EditItemTemplate>
- <FooterTemplate>
- <asp:Literal ID="TBF_footer" runat="server">asp:Literal>
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Name">
- <ItemTemplate>
- <asp:Label ID="lb_Na" Text='<%#Eval("Su_Name") %>' runat="server">asp:Label>
- ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="Name_Edit" runat="server" Text='<%#Eval("Su_Name") %>'>asp:TextBox>
- EditItemTemplate>
- <FooterTemplate>
- <asp:TextBox ID="Name_footer" runat="server">asp:TextBox>
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Ingredints">
- <ItemTemplate>
- <asp:Label ID="Lb_Ing" Text='<%#Eval("Ingredients") %>' runat="server">asp:Label>
- ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="Ing_Edit" runat="server" Text='<%#Eval("Ingredients") %>'>asp:TextBox>
- EditItemTemplate>
- <FooterTemplate>
- <asp:TextBox ID="Ing_footer" runat="server">asp:TextBox>
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="price">
- <ItemTemplate>
- <asp:Label ID="Lb_pr" Text='<%#Eval("Price") %>' runat="server">asp:Label>
- ItemTemplate>
- <EditItemTemplate>
- <asp:TextBox ID="Pri_Edit" runat="server" Text='<%#Eval("Price") %>'>asp:TextBox>
- EditItemTemplate>
- <FooterTemplate>
- <asp:TextBox ID="Pri_footer" runat="server">asp:TextBox>
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Image">
- <ItemTemplate>
- <asp:Image ID="Img_item" runat="server" Height="80px" Width="80px" ImageUrl='<%#Eval("Image") %>' />
- ItemTemplate>
- <EditItemTemplate>
- <asp:FileUpload ID="FU_Edit" runat="server" Visible="false"/>
- EditItemTemplate>
- <FooterTemplate>
- <asp:FileUpload ID="FU_footer" runat="server" />
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField HeaderText="Enable">
- <ItemTemplate>
- <asp:CheckBox ID="Ch_SUcat" runat="server" Enabled='<%#Eval("Visible") %>' />
- ItemTemplate>
- <EditItemTemplate>
- <asp:CheckBox ID="Ch_SUcat_E" runat="server" Enabled='<%#Eval("Visible") %>' />
- EditItemTemplate>
- <FooterTemplate>
- <asp:CheckBox ID="Ch_SUcat_Footer" runat="server" />
- FooterTemplate>
- asp:TemplateField>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:ImageButton ID="imgb1" runat="server" ImageUrl="~/images/png/pencil.png" CommandName="Edit" Width="20px" Height="20px" />
- <asp:ImageButton ID="imgb2" runat="server" ImageUrl="~/images/png/delete.png" CommandName="Delete" Width="20px" Height="20px" />
- ItemTemplate>
- <EditItemTemplate>
- <asp:ImageButton ID="imgb3" runat="server" ImageUrl="~/images/png/saveblue.png" CommandName="Update" Width="20px" Height="20px" />
- <asp:ImageButton ID="imgb4" runat="server" ImageUrl="~/images/png/cancel.png" CommandName="Cancel" Width="20px" Height="20px" />
- EditItemTemplate>
- <FooterTemplate>
- <asp:ImageButton ID="imgb5" runat="server" ImageUrl="~/images/png/addblue.png" CommandName="AddNew" Width="20px" Height="20px" />
- FooterTemplate>
- asp:TemplateField>
- Columns>
- asp:GridView>
- <asp:ValidationSummary ID="VSummry" runat="server" ForeColor="Red" />
- <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)">
- <DeleteParameters>
- <asp:Parameter Name="C_Id" />
- DeleteParameters>
- <InsertParameters>
- <asp:Parameter Name="Su_Name" Type="String" />
- <asp:Parameter Name="Ingredients" Type="String" />
- <asp:Parameter Name="Price" Type="Decimal" />
- <asp:Parameter Name="Image" Type="String" />
- <asp:Parameter Name="Visible" Type="Boolean" />
- <asp:Parameter Name="C_Id" Type="Int32" />
- InsertParameters>
- <SelectParameters>
- <asp:ControlParameter ControlID="RadioButtonList1" Name="C_Id" PropertyName="SelectedValue" Type="Int32" />
- SelectParameters>
- <UpdateParameters>
- <asp:Parameter Name="Su_Name" Type="String" />
- <asp:Parameter Name="Ingredients" Type="String" />
- <asp:Parameter Name="Price" Type="Decimal" />
- <asp:Parameter Name="Image" Type="String" />
- <asp:Parameter Name="Visible" Type="Boolean" />
- <asp:Parameter Name="C_Id" Type="Int32" />
- UpdateParameters>
- asp:SqlDataSource>
- <br />
- <asp:Label ID="lblsuccessmessage" Text="" runat="server" ForeColor="Green">asp:Label><br />
- <asp:Label ID="lblerrormessage" Text="" runat="server" ForeColor="Red">asp:Label>
- public partial class sub_category : System.Web.UI.Page
- {
- string connectionString = @"Data Source=(local)\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbCh.mdf;Integrated Security=True";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- gvsubcat.DataBind();
- }
- }
- protected void gvsubcat_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- try
- {
- if (e.CommandName.Equals("AddNew"))
- {
- using (SqlConnection sqlcon = new SqlConnection(connectionString))
- {
- FileUpload fu = (FileUpload)gvsubcat.FooterRow.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)
- {
- lblerrormessage.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 Sub_categories(Su_Name,Ingredients,Price,Image,Visible,C_Id) VALUES(@Su_Name,@Ingredients,@Price,@Image,@Visible,@C_Id)";
- SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
- sqlcmd.Parameters.AddWithValue("@C_Id", (gvsubcat.FooterRow.FindControl("TBF_footer") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@Su_Name", (gvsubcat.FooterRow.FindControl("Name_footer") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@Ingredients", (gvsubcat.FooterRow.FindControl("Ing_footer") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@Price", (gvsubcat.FooterRow.FindControl("Pri_footer") as TextBox).Text.Trim());
- sqlcmd.Parameters.AddWithValue("@Image", fileName);
- //sqlcmd.Parameters.AddWithValue("@Visible", Convert.ToBoolean((gvsubcat.FooterRow.FindControl("Ch_SUcat_Footer") as CheckBox)));
- sqlcmd.Parameters.AddWithValue("@Visible", (gvsubcat.FooterRow.FindControl("Ch_SUcat_Footer.text") as CheckBox));
- sqlcmd.ExecuteNonQuery();
- gvsubcat.DataBind();
- lblsuccessmessage.Text = "New Record Added";
- lblerrormessage.Text = "";
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- lblsuccessmessage.Text = "";
- lblerrormessage.Text = ex.Message;
- }
- }
- protected void gvsubcat_RowEditing(object sender, GridViewEditEventArgs e)
- {
- gvsubcat.EditIndex = e.NewEditIndex;
- gvsubcat.DataBind();
- }
- protected void gvsubcat_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- gvsubcat.EditIndex = -1;
- gvsubcat.DataBind();
- }
- protected void gvsubcat_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- try
- {
- using (SqlConnection sqlcon = new SqlConnection(connectionString))
- {
- sqlcon.Open();
- string query = "DELETE FROM Categories where Sub_categories.Id =@Id";
- SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
- sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(gvsubcat.DataKeys[e.RowIndex].Value.ToString()));
- sqlcmd.ExecuteNonQuery();
- gvsubcat.DataBind();
- lblsuccessmessage.Text = "Selected Record has been Deleted.";
- lblerrormessage.Text = "";
- }
- }
- catch (Exception ex)
- {
- lblsuccessmessage.Text = "";
- lblerrormessage.Text = ex.Message;
- }
- }
- protected void gvsubcat_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- try
- {
- using (SqlConnection sqlcon = new SqlConnection(connectionString))
- {
- sqlcon.Open();
- string query = "UPDATE Sub_categories SET Su_Name=@Su_Name, Ingredients=@Ingredients, Price=@Price, Image=@Image, Visible=@Visible where Sub_categories.Id=@Id";
- SqlCommand sqlcmd = new SqlCommand(query, sqlcon);
- sqlcmd.Parameters.AddWithValue("@Su_Name", (gvsubcat.FooterRow.FindControl("Name_Edit") as TextBox));
- sqlcmd.Parameters.AddWithValue("@Ingredients", (gvsubcat.FooterRow.FindControl("Ing_Edit") as TextBox));
- sqlcmd.Parameters.AddWithValue("@Price", (gvsubcat.FooterRow.FindControl("Pri_Edit") as TextBox));
- sqlcmd.Parameters.AddWithValue("@Image", (gvsubcat.FooterRow.FindControl("FU_Edit")as FileUpload));
- sqlcmd.Parameters.AddWithValue("@Visible", Convert.ToBoolean((gvsubcat.FooterRow.FindControl("Ch_SUcat_E") as CheckBox).Checked));
- FileUpload fu = (FileUpload)gvsubcat.FindControl("FU_Edit");
- if (fu.HasFile)
- {
- string file = System.IO.Path.Combine(Server.MapPath("~/images/categories/"), fu.FileName);
- fu.SaveAs(file);
- }
- sqlcmd.Parameters.AddWithValue("@Id", Convert.ToInt32(gvsubcat.DataKeys[e.RowIndex].Value.ToString()));
- sqlcmd.ExecuteNonQuery();
- gvsubcat.EditIndex = -1;
- gvsubcat.DataBind();
- lblsuccessmessage.Text = "Selected Record has been Updated.";
- lblerrormessage.Text = "";
- }
- }
- catch (Exception ex)
- {
- lblsuccessmessage.Text = "";
- lblerrormessage.Text = ex.Message;
- }
- }
- protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- Literal txtid = (Literal)gvsubcat.FooterRow.FindControl("TBF_footer");
- txtid = RadioButtonList1.SelectedItem.Text;
- }
- }

Rajanikant HawaldarPosted Oct 10, 2019, 9:52 PM
Aktham MahmoudPosted Oct 12, 2019, 12:13 PM