hello.. i am making website on "online electronics products"
in asp.net in the product table i have
product id
product name
product price
product imageurl
i have completed the user part that is ... add items to cart, edit cart,shopping etc...
now i want to do the admin part to add new items in the product table and edit existing..
i can add/edit the product code, price and name... bt how to add image using FileUpload? (the field imageurl is string)
and also i want to save that image in my project's (items) folder...
Thanx...!
Loading


Kirtan PatelRank #292 · 8.6K reputationPosted Oct 26, 2009, 7:50 AM EDT
Here is your Solution
take one FileUpload Control and One Button Control and Code Like Below
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == true)
{
//Get Just FileName not Full path of File
string FileName = FileUpload1.FileName;
//Get Actual Path of Images Folder on Server
string FolderPathToSave = Server.MapPath("images");
//Save Image in Images Folder
FileUpload1.SaveAs(FolderPathToSave+@"\"+FileName);
string PathToStoreInDatabase = @"~\images\" + FileName;
// Insert PathToStoreDatabase in Database to Retrive Image Path Again When you need From Database
//Like If you want to Show Uploaded Image in Picture Control Then Code below
Image1.ImageUrl = PathToStoreInDatabase;
}
}
All the Best :)
Mahesh ChandRank #1 · 393.3K reputationPosted Oct 26, 2009, 7:56 AM EDT
Dipa AhujaRank #1,168 · 1.5K reputationPosted Oct 26, 2009, 9:27 AM EDT
To Kirtan ..
hi...i tried tht way as u have suggested..
bt its working bt not fully..
in updateproducts.aspx i have
1 datalist control and 1 detailsview control....
DETEAILSVIEW & SQLDATASOURCE
-- Sqldatasource for DetailsView --%> asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="" SelectCommand="SELECT * FROM [product] WHERE ([pcode] = @pcode)" DeleteCommand="DELETE FROM [product] WHERE [pcode] = @pcode" InsertCommand="Insert into [product] ([pcode],[pname], [purl], [price],[disc]) VALUES (@pcode, @pname, @purl, @price, @disc)" UpdateCommand="UPDATE [product] SET [pname] = @pname, [disc] = @disc, [price] = @price, [purl] = @purl WHERE [pcode] = @pcode" OnInserted="SqlDataSource2_Inserted" OnUpdated="SqlDataSource2_Updated" OnDeleted="SqlDataSource2_Deleted"> DeleteParameters> asp:Parameter Name="pcode" Type="string" /> DeleteParameters> SelectParameters> asp:ControlParameter Name="pcode" Type="String" ControlID="DataList1" PropertyName="SelectedValue">asp:ControlParameter> SelectParameters> UpdateParameters> asp:Parameter Name="pname" Type="string" /> asp:Parameter Name="disc" Type="String" /> asp:Parameter Name="price" Type="double" /> asp:Parameter Name="purl" Type="string" /> asp:Parameter Name="pcode" Type="string" /> UpdateParameters> InsertParameters> asp:Parameter Name="pcode" Type="string"/> asp:Parameter Name="pname" Type="string" /> asp:Parameter Name="purl" Type="string" /> asp:Parameter Name="price" Type="double" /> asp:Parameter Name="disc" Type="string" /> InsertParameters> asp:SqlDataSource> -- Details file --%> -- Details View Which will display selected product and also add new and edit existing--%> asp:DetailsView ID="DetailsView1" BorderColor="white" datasourceid="SqlDataSource2" BorderWidth="1px" runat="server" Height="50px" Width="600px" autogeneraterows="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" DataKeyNames="pcode"> HeaderStyle ForeColor="White" /> FooterTemplate> asp:ValidationSummary Font-Bold="true" ForeColor="white" id="ValidationSummary1" runat="server">asp:ValidationSummary> FooterTemplate> Fields> asp:TemplateField SortExpression="pcode" HeaderText="Code"> ItemTemplate> asp:Label runat="server" Text='' id="Label1">asp:Label> ItemTemplate> InsertItemTemplate> asp:TextBox runat="server" Text='' id="pcod"/>br /> InsertItemTemplate> HeaderStyle Font-Bold="True" ForeColor="White" /> asp:TemplateField> asp:BoundField DataField="pname" HeaderText="Product Name" SortExpression="pname" > HeaderStyle Font-Bold="True" ForeColor="White" /> ItemStyle ForeColor="White" /> asp:BoundField> asp:BoundField DataField="price" HeaderText="Price" SortExpression="price" > HeaderStyle Font-Bold="True" ForeColor="White" /> ItemStyle ForeColor="White" /> asp:BoundField> asp:TemplateField SortExpression="purl" HeaderText="PictureURL"> EditItemTemplate> asp:TextBox runat="server" Text='' id="txtPictureURL"/> br /> asp:FileUpload ID="FileUpload1" Runat="server" /> asp:Button ID="btnUpload" Runat="server" Text="Upload Picture" OnClick="btnUpload_Click" Width="127px" Height="21px" /> asp:Label ID="Status" Runat="server">asp:Label> EditItemTemplate> ItemTemplate> asp:Label runat="server" Text='' id="lblurl">asp:Label> ItemTemplate> InsertItemTemplate> asp:TextBox runat="server" Text='' id="txtPictureURL"> asp:TextBox>br /> asp:FileUpload ID="FileUpload1" Runat="server" /> asp:Button ID="btnUpload" Runat="server" Text="Upload Picture" OnClick="btnUpload_Click" Width="127px" Height="21px" /> asp:Label ID="Status" Runat="server">asp:Label> InsertItemTemplate> asp:TemplateField> asp:TemplateField SortExpression="disc" HeaderText="Description"> EditItemTemplate> asp:TextBox runat="server" Text='' id="TextBox2" Columns="50" TextMode="MultiLine" Rows="3">asp:TextBox> asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" errormessage="You must enter a description" controltovalidate="TextBox2">*asp:requiredfieldvalidator> EditItemTemplate> ItemTemplate> asp:Label runat="server" Text='' id="Label2">asp:Label> ItemTemplate> InsertItemTemplate> asp:TextBox runat="server" Text='' id="TextBox2" Columns="50" TextMode="MultiLine" Rows="3">asp:TextBox> asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" errormessage="You must enter a description" controltovalidate="TextBox2">*asp:requiredfieldvalidator> InsertItemTemplate> HeaderStyle Font-Bold="True" ForeColor="White" /> asp:TemplateField> Fields> asp:DetailsView>DATA LIST SQLDATA SOURCE:
-- Sqldatasource for datalist--%> asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="" SelectCommand="SELECT * FROM [product]" DeleteCommand="DELETE FROM [product] WHERE [pcode] = @pcode" InsertCommand="Insert into [product] ([pcode], [pname], [purl], [price],[disc]) VALUES (@pcode, @pname, @purl, @price, @disc)" UpdateCommand="UPDATE [products] SET [pname] = @pname, [disc] = @disc, [price] = @price, [purl] = @purl WHERE [pcode] = @pcode"> DeleteParameters> asp:Parameter Name="pcode" Type="string" /> DeleteParameters> UpdateParameters> asp:Parameter Name="pname" Type="string" /> asp:Parameter Name="disc" Type="String" /> asp:Parameter Name="price" Type="double" /> asp:Parameter Name="purl" Type="string" /> asp:Parameter Name="pcode" Type="string" /> UpdateParameters> InsertParameters> asp:Parameter Name="pcode" Type="string"/> asp:Parameter Name="pname" Type="string" /> asp:Parameter Name="purl" Type="string" /> asp:Parameter Name="price" Type="double" /> asp:Parameter Name="disc" Type="string" /> InsertParameters> asp:SqlDataSource> -- datalist--%> asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatDirection="Horizontal" BackColor="Transparent" BorderColor="White" BorderStyle="None" BorderWidth="2px" CellPadding="0" GridLines="Both" ShowFooter="False" ShowHeader="False" Width="80%" Height="400" CellSpacing="0" ForeColor="Black" DataKeyField="pcode"> ItemTemplate> asp:Image ImageUrl='' runat="server" id="PictureURLImage" Height="80" Width="80" BorderWidth="2px" />br />br /> asp:Label Text='' runat="server" id="NameLabel" style="font-weight:bold"/>br /> asp:Label Text='' runat="server" id="PriceLabel"/> Rs.br /> asp:LinkButton id="LinkButton1" runat="server" CommandName="Select">Selectasp:LinkButton> ItemTemplate> SelectedItemStyle BorderColor="#ff9900" BorderStyle="dashed" BorderWidth="1px" /> ItemStyle VerticalAlign="top" HorizontalAlign="left" /> asp:DataList>and at the top:
asp:linkbutton id="btnNewItem" runat="server" commandname="New" OnClick="btnNewItem_Click">Add New Itemasp:linkbutton> br />br /> .cs protected void btnNewItem_Click(object sender, EventArgs e) { DetailsView1.ChangeMode(DetailsViewMode.Insert); }so by ur code i wrote:
protected void btnUpload_Click(object sender, System.EventArgs e) { FileUpload fp = (FileUpload)DetailsView1.FindControl("FileUpload1"); if (fp.HasFile == true) { string FileName = fp.FileName; //Get Actual Path of Images Folder on Server string FolderPathToSave = Server.MapPath("~/Products/items"); //Save Image in Images Folder fp.SaveAs(FolderPathToSave + @"\" + FileName); string PathToStoreInDatabase = @"~/Products/items" + FileName; }it stores image in the folder, bt not the imageurl in the database...!
where is the problem?
thanx!
Dipa AhujaRank #1,168 · 1.5K reputationPosted Oct 26, 2009, 9:30 AM EDT
and those images i want to store is from root : "products/items"
Dipa AhujaRank #1,168 · 1.5K reputationPosted Oct 26, 2009, 9:52 AM EDT
i 4got to write this:
bt 1 problem when i take Ajax's updatepanel.. in my updateproduct.aspx, its not uploading the image.. y its happening so?
Dipa AhujaRank #1,168 · 1.5K reputationPosted Oct 27, 2009, 8:05 AM EDT
hi would u plz see y the fileupload not working when i use ajax updatepanel...
http://www.mediafire.com/file/tlzn5jzgmjw/admin.rar
Dipa AhujaRank #1,168 · 1.5K reputationPosted Oct 31, 2009, 8:40 AM EDT
div> table cellpadding="4" cellspacing="4" width="90%" align="center"> tr> cc1:UpdatePanel ID="UpdatePanel1" runat="server"> ContentTemplate> asp:Label ID="Label1" runat="server" Text="Select A File To Upload:" ForeColor="Black">asp:Label> asp:FileUpload ID="FileUpload1" runat="server" /> asp:Button ID="ButtonUpload" runat="server" Height="25px" OnClick="ButtonUpload_Click" Text="Upload" Width="128px" /> br /> asp:Label ID="Label2" runat="server">asp:Label> br /> ContentTemplate> Triggers> cc1:PostBackTrigger ControlID="ButtonUpload" /> Triggers> cc1:UpdatePanel> tr> table> div>here file upload button is simple bt in my code it is inside the details view...!
so in the code how to write the controlId Property of detailsView 's btnUpload :