Aktham Mahmoud

Aktham Mahmoud

  • 1.6k
  • 282
  • 1.3k

Implement Upload file control with DetailsView

Sep 16 2019 10:53 AM
Good times All
I've an application to mange Chocolate shope,
at admin side there is many pages to mange a site, my problem with a form to insert data in data base with uploaded image by File upload control implemented in details view
you can see a front end code here: 
 
 
  1. <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SDS_Subcategory" DefaultMode="Insert" Height="50px" Width="125px" OnItemInserting="DetailsView1_ItemInserting">  
  2.             <Fields>  
  3.                 <asp:TemplateField HeaderText="Main Category:">  
  4.                     <InsertItemTemplate>  
  5.                         <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="C_Name" DataValueField="C_Id" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">  
  6.                         </asp:DropDownList>  
  7.                         <asp:Literal ID="Lit_cat" runat="server" Text='<%#Bind("C_Id") %>'></asp:Literal>  
  8.                         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [C_Id], [C_Name] FROM [Categories]"></asp:SqlDataSource>  
  9.                         <asp:Label ID="Label1" runat="server"></asp:Label>  
  10.                     </InsertItemTemplate>  
  11.                 </asp:TemplateField>  
  12.                 <asp:TemplateField HeaderText="Name:">  
  13.                     <InsertItemTemplate>  
  14.                         <asp:TextBox ID="TXT_Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>  
  15.                     </InsertItemTemplate>  
  16.                 </asp:TemplateField>  
  17.                 <asp:TemplateField HeaderText="Image">  
  18.                     <InsertItemTemplate>  
  19.                         <asp:FileUpload ID="FU_Image" runat="server" Enabled='<%# Bind("Image") %>' />  
  20.                     </InsertItemTemplate>  
  21.                 </asp:TemplateField>  
  22.                 <asp:TemplateField HeaderText="Ingredients">  
  23.                     <InsertItemTemplate>  
  24.                         <asp:TextBox ID="TXT_Description" runat="server" Text='<%# Bind("Ingredients") %>' TextMode="MultiLine"></asp:TextBox>  
  25.                     </InsertItemTemplate>  
  26.                 </asp:TemplateField>  
  27.                 <asp:CommandField ShowInsertButton="True" />  
  28.                 <asp:TemplateField>  
  29.                     <InsertItemTemplate>  
  30.                         <asp:Label ID="LBMSG" runat="server"></asp:Label>  
  31.                     </InsertItemTemplate>  
  32.                 </asp:TemplateField>  
  33.             </Fields>  
  34.         </asp:DetailsView>  
  35.     </div>  
 
 and a code behind here:
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Configuration;  
  10. using System.IO;  
  11.   
  12.   
  13. namespace WebApplication1.admin  
  14. {  
  15.     public partial class sub_category : System.Web.UI.Page  
  16.     {  
  17.         protected void Page_Load(object sender, EventArgs e)  
  18.         {  
  19.             Label lbmessage = (Label)DetailsView1.FindControl("LBMSG");  
  20.         }  
  21.   
  22.         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  23.         {  
  24.             string function = ((DropDownList)DetailsView1.FindControl("DropDownList1")).SelectedValue.ToString();  
  25.             Literal lit_txt = (Literal)DetailsView1.FindControl("lit_cat");  
  26.             lit_txt.Text = function;  
  27.             Label lbl_txt = (Label)DetailsView1.FindControl("Label1");  
  28.         }  
  29.   
  30.         protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)  
  31.         {  
  32.             FileUpload fu = (FileUpload)DetailsView1.FindControl("FU_Image");  
  33.             TextBox txtimg= (TextBox)DetailsView1.FindControl("TXT_Name");  
  34.             Literal lit_txt = (Literal)DetailsView1.FindControl("lit_cat");  
  35.             TextBox txtdesc =(TextBox)DetailsView1.FindControl("TXT_Description");  
  36.             if (fu.PostedFile != null)  
  37.             {  
  38.                 string imgfile = Path.GetFileName(fu.PostedFile.FileName);  
  39.                 //fu.SaveAs("../images/categories" + imgfile);  
  40.                 fu.SaveAs(Server.MapPath("../images/categories") + imgfile);  
  41.                 string mainconnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  42.                 SqlConnection sqlcon = new SqlConnection(mainconnection);  
  43.                 sqlcon.Open();  
  44.                 string sqlquery = "insert into [dbo].[Sub_categories]([Name],[Image],[Ingredients],[C_Id]) values (@Name,@Image,@Ingredients,@C_Id)";  
  45.                 SqlCommand sqlcom = new SqlCommand(sqlquery, sqlcon);  
  46.                 sqlcom.Parameters.AddWithValue("@Name", txtimg);  
  47.                 sqlcom.Parameters.AddWithValue("@Image""../images/categories" + imgfile);  
  48.                 sqlcom.Parameters.AddWithValue("@Ingredients", txtdesc);  
  49.                 sqlcom.Parameters.AddWithValue("@C_Id", lit_txt);  
  50.                 sqlcom.ExecuteNonQuery();  
  51.                 Label lbmessage = (Label)DetailsView1.FindControl("LBMSG");  
  52.                 lbmessage.Text = "Image and record has beed saved Succesfully";  
  53.                 sqlcon.Close();  
  54.             }  
  55.             else {  
  56.                 Label lbmessage = (Label)DetailsView1.FindControl("LBMSG");  
  57.                 lbmessage.Text = "Image and record not saved Succesfully";  
  58.             }  
  59.         }  
  60.   
  61.     }  
  62. }  
 
 so there is a way to solve a prolem 
 

Answers (1)