Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2007, 12:09 PM
Registered User
 
Join Date: Aug 2007
Location: Jaipur, , India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default fileupload control in formview insert item templat

hi Friends..

I am new to asp.net2.o world & trying my hands here & there,
when trying i find utility of using server controls in formview
very useful.now the problem is this that when i tried to use
fileupload control in formview insert item template, that is
not working, i am trying to upload a image file in my data folder
as well as in data table. my data type for images is varchar.
i have tried it lot in net but no success plz help me,if u
can give me full code or method of solving my problem
thank you friends

 
Old August 6th, 2007, 12:20 PM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

First of all, datatype of image cannot be varchar. I think it should be image. Or, you can store the path of image file in database table. For uploading the file in some folder on server, use following code:

FileUpload1.PostedFile.SaveAs("C:\\FolderName"); // you should preferably use relative path here
 
Old August 6th, 2007, 12:27 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

For a detailed description of both options that gaurav_jain2403 gave, take a look here:
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=414

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 6th, 2007, 12:43 PM
Registered User
 
Join Date: Aug 2007
Location: Jaipur, , India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi gaurav,
first of all thanks to reply.
i have made a database table in sql where with other string
items like name address etc, i have used a varchar datatype
for my images & store data in jpg format like abc.jpg
now when i am using a formview to see data in item template
i dragged a imageweb control in formview & binded it to
image(varchar) field of my database it is working fine
up to this point ,but now in inseritem template i am using
a file uploader control to upload file the only binding properties
it is showing is enable & visible. , thatswhy i am not able
to bind the file upload control to image field.i have not used
any code for file uploader
if you think that this method is wrong can you tell me another method
to do it i know that i am asking too much but i would greatfull
to you.if u tell me preferably in c#
thankyou

 
Old August 6th, 2007, 12:55 PM
Registered User
 
Join Date: Aug 2007
Location: Jaipur, , India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi imar
now the link which u have given is fine with fileuploader
outside the formview , but would it work in inserttemplate
of form also & if yes than how & what i should write at
my form page

 
Old August 6th, 2007, 01:06 PM
Registered User
 
Join Date: Aug 2007
Location: Jaipur, , India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

my code in html page is like this & no C# code


        <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="Code"
            DataSourceID="SqlDataSource1" Style="z-index: 100; left: 32px; position: absolute;
            top: 88px" Height="368px" Width="560px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" OnItemInserted="FormView1_ItemInserted" OnItemUpdated="FormView1_ItemUpdated" OnItemDeleted="FormView1_ItemDeleted">
            <EditItemTemplate>
                <h1>
                    <span style="font-size: 16pt">Edit Item.....</span></h1>
                Code: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;
                <asp:Label ID="CodeLabel1" runat="server" Text='<%# Eval("Code") %>'></asp:Label><br />
                <br />
                Name: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                </asp:TextBox><br />
                <br />
                Price: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'>
                </asp:TextBox><br />
                <br />
                Description: &nbsp; &nbsp; &nbsp; &nbsp;<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>
                </asp:TextBox><br />
                <br />
                FileThumb:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 120px; position: absolute;
                    top: 264px" Text='<%# Bind("FileThumb") %>'></asp:TextBox>
                <br />
                <br />
                FileZoom:&nbsp;
                <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 101; left: 120px; position: absolute;
                    top: 304px" Text='<%# Bind("FileZoom") %>'></asp:TextBox>
                &nbsp; &nbsp;&nbsp;
                <br />
                <asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("FileThumb", "Images/ChildrenImages/Thumbs/{0}") %>'
                    Style="z-index: 103; left: 416px; position: absolute; top: 136px" />
                <br />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                    Text="Update">
                </asp:LinkButton>
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </EditItemTemplate>
            <InsertItemTemplate>
                <h1>
                    <span style="font-size: 16pt">Insert Item....</span></h1>
                <br />
                Name: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                </asp:TextBox><br />
                <br />
                Price: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'>
                </asp:TextBox><br />
                <br />
                Description: &nbsp;&nbsp; &nbsp;<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'>
                </asp:TextBox><br />
                <br />
                FileThumb: &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;
                <asp:FileUpload ID="FileUpload1" runat="server" Style="z-index: 100; left: 104px;
                    position: absolute; top: 240px" Enabled="true" Visible='<%# Eval("FileThumb") %>' />
                &nbsp;
                <br />
                <br />
                FileZoom: &nbsp; &nbsp; &nbsp;&nbsp;
                <asp:TextBox ID="FileZoomTextBox" runat="server" Text='<%# Bind("FileZoom") %>'>
                </asp:TextBox><br />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                    Text="Insert">
                </asp:LinkButton>
                <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                    Text="Cancel">
                </asp:LinkButton>
            </InsertItemTemplate>
            <ItemTemplate>
                <h1>
                    <span style="font-size: 16pt">Product Description.......</span></h1>
                Code:
                <asp:Label ID="CodeLabel" runat="server" Text='<%# Eval("Code") %>'></asp:Label><br />
                <br />
                Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label><br />
                <br />
                Price:
                <asp:Label ID="PriceLabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label><br />
                <br />
                Description:
                <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>'>
                </asp:Label><br />
                <br />
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FileThumb", "Images/ChildrenImages/Thumbs/{0}") %>'
                    Style="z-index: 100; left: 352px; position: absolute; top: 112px" />
                &nbsp;
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit">
                </asp:LinkButton>
                <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete">
                </asp:LinkButton>
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
                    Text="New">
                </asp:LinkButton>
            </ItemTemplate>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:FormView>

 
Old August 6th, 2007, 01:23 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi rakuntal,

My article mainly helps you with writing the code for saving and retrieving files from the database.

To mix those concepts with controls like the FormView, you need to look into some basic Data Control Tutorials:

http://quickstarts.asp.net/QuickStar...a/default.aspx
http://msdn2.microsoft.com/en-us/lib...inserting.aspx

In particular, search Google for more information about handling the ItemInserting and ItemUpdating events. Inside these events, you can use FindControl to find the file upload control and work from there.

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old August 6th, 2007, 01:28 PM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

I have not worked with FormView. But can give you the idea. Add commandArgument=<%# Bind('DataTableID') %> to linkbutton "InsertButton". when this button is clicked and file for that corresponding row is browsed, write this code in FormView1_ItemInserting(not FormView1_ItemInserted):

string arg = e.CommandArgument.ToString();
// arg will contain the Id from the database for corresponding row

if (FileUpload1.PostedFile != null)
{
FileUpload1.Postedfile.saveAs("FilePath");
// or if you want to save the file in database, write code here
// insert the data in the row with Id equal to e.CommandArgument.ToString() in database
}
 
Old August 6th, 2007, 01:30 PM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

Imar, your previous article for saving and retrieving files from the database was great.
 
Old August 6th, 2007, 01:34 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Thank you..... ;)

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004




Similar Threads
Thread Thread Starter Forum Replies Last Post
Access denied error when using FileUpload Control mii2029 ASP.NET 3.5 Basics 0 October 25th, 2008 05:45 PM
Problem to implement CSSClas on fileupload control azizur123 ASP.NET 2.0 Professional 1 October 13th, 2008 09:50 AM
FileUpload control aliirfan84 ASP.NET 2.0 Professional 1 June 2nd, 2007 03:37 PM
FileUpload Control MunishBhatia ASP.NET 2.0 Basics 2 December 6th, 2006 04:03 AM
FileUpload control -- file size detection ekwong ASP.NET 2.0 Basics 3 November 7th, 2006 06:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.