1 Replies - 6779 Views - Last Post: 27 February 2011 - 01:03 PM Rate Topic: -----

#1 creative-rose  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-January 11

how to insert image to database in formview?

Posted 29 January 2011 - 12:32 AM

Hello
I want to insert image to the database but i don't know how to do that in formview
his code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default5.aspx.vb" Inherits="Default5" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" AllowPaging="True" BackColor="#DEBA84"
        BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
        DataKeyNames="code" DataSourceID="SqlDataSource1" GridLines="Both" Width="324px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <EditItemTemplate>
            code:
            <asp:Label ID="codeLabel1" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><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>
            code:
            <asp:TextBox ID="codeTextBox" runat="server" Text='<%# Bind("code") %>'>
            </asp:TextBox><br />
            name:
            <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
            </asp:TextBox><br />
            nationality:
            <asp:TextBox ID="nationalityTextBox" runat="server" Text='<%# Bind("nationality") %>'>
            </asp:TextBox><br />
            sex:
            <asp:TextBox ID="sexTextBox" runat="server" Text='<%# Bind("sex") %>'>
            </asp:TextBox><br />
            status:
            <asp:TextBox ID="statusTextBox" runat="server" Text='<%# Bind("status") %>'>
            </asp:TextBox><br />
            degree:
            <asp:TextBox ID="degreeTextBox" runat="server" Text='<%# Bind("degree") %>'>
            </asp:TextBox><br />
            job:
            <asp:TextBox ID="jobTextBox" runat="server" Text='<%# Bind("job") %>'>
            </asp:TextBox><br />
            address:
            <asp:TextBox ID="addressTextBox" runat="server" Text='<%# Bind("address") %>'>
            </asp:TextBox><br />
            phone:
            <asp:TextBox ID="phoneTextBox" runat="server" Text='<%# Bind("phone") %>'>
            </asp:TextBox><br />
            total_salary:
            <asp:TextBox ID="total_salaryTextBox" runat="server" Text='<%# Bind("total_salary") %>'>
            </asp:TextBox><br />
            appoint_date:
            <asp:TextBox ID="appoint_dateTextBox" runat="server" Text='<%# Bind("appoint_date") %>'>
            </asp:TextBox><br />
            disconnrct:
            <asp:TextBox ID="disconnrctTextBox" runat="server" Text='<%# Bind("disconnrct") %>'>
            </asp:TextBox><br />
            personal_image:
            <asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
            </asp:TextBox><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>
            code:
            <asp:Label ID="codeLabel" runat="server" Text='<%# Eval("code") %>'></asp:Label><br />
            name:
            <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label><br />
            nationality:
            <asp:Label ID="nationalityLabel" runat="server" Text='<%# Bind("nationality") %>'></asp:Label><br />
            sex:
            <asp:Label ID="sexLabel" runat="server" Text='<%# Bind("sex") %>'></asp:Label><br />
            status:
            <asp:Label ID="statusLabel" runat="server" Text='<%# Bind("status") %>'></asp:Label><br />
            degree:
            <asp:Label ID="degreeLabel" runat="server" Text='<%# Bind("degree") %>'></asp:Label><br />
            job:
            <asp:Label ID="jobLabel" runat="server" Text='<%# Bind("job") %>'></asp:Label><br />
            address:
            <asp:Label ID="addressLabel" runat="server" Text='<%# Bind("address") %>'></asp:Label><br />
            phone:
            <asp:Label ID="phoneLabel" runat="server" Text='<%# Bind("phone") %>'></asp:Label><br />
            total_salary:
            <asp:Label ID="total_salaryLabel" runat="server" Text='<%# Bind("total_salary") %>'></asp:Label><br />
            appoint_date:
            <asp:Label ID="appoint_dateLabel" runat="server" Text='<%# Bind("appoint_date") %>'></asp:Label><br />
            disconnrct:
            <asp:Label ID="disconnrctLabel" runat="server" Text='<%# Bind("disconnrct") %>'></asp:Label><br />
            personal_image:
            <asp:Label ID="personal_imageLabel" runat="server" Text='<%# Bind("personal_image") %>'></asp:Label><br />
            <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>
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:employeedataConnectionString %>"
        DeleteCommand="DELETE FROM [emp] WHERE [code] = @code" InsertCommand="INSERT INTO [emp] ([code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image]) VALUES (@code, @name, @nationality, @sex, @status, @degree, @job, @address, @phone, @total_salary, @appoint_date, @disconnrct, @personal_image)"
        SelectCommand="SELECT [code], [name], [nationality], [sex], [status], [degree], [job], [address], [phone], [total_salary], [appoint_date], [disconnrct], [personal_image] FROM [emp]"
        UpdateCommand="UPDATE [emp] SET [name] = @name, [nationality] = @nationality, [sex] = @sex, [status] = @status, [degree] = @degree, [job] = @job, [address] = @address, [phone] = @phone, [total_salary] = @total_salary, [appoint_date] = @appoint_date, [disconnrct] = @disconnrct, [personal_image] = @personal_image WHERE [code] = @code">
        <DeleteParameters>
            <asp:Parameter Name="code" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
            <asp:Parameter Name="code" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="code" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="nationality" Type="String" />
            <asp:Parameter Name="sex" Type="String" />
            <asp:Parameter Name="status" Type="String" />
            <asp:Parameter Name="degree" Type="String" />
            <asp:Parameter Name="job" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="phone" Type="String" />
            <asp:Parameter Name="total_salary" Type="Decimal" />
            <asp:Parameter Name="appoint_date" Type="String" />
            <asp:Parameter Name="disconnrct" Type="String" />
            <asp:Parameter Name="personal_image" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
</asp:Content>


I found the solution how to insert image to database but i don't know how to do that in formview?
Please help me?
thank you

Is This A Good Question/Topic? 0
  • +

Replies To: how to insert image to database in formview?

#2 Ninjato  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 29
  • Joined: 24-February 11

Re: how to insert image to database in formview?

Posted 27 February 2011 - 01:03 PM

View Postcreative-rose, on 29 January 2011 - 12:32 AM, said:

<EditItemTemplate>
personal_image:
<asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
</asp:TextBox><br />
</EditItemTemplate>


Well, why are you using a textbox for image in Edit Template, use a file upload control to load image

Quote

<InsertItemTemplate>
personal_image:
<asp:TextBox ID="personal_imageTextBox" runat="server" Text='<%# Bind("personal_image") %>'>
</asp:TextBox><br />

Now, here you should have used an File Upload Control instead of a textbox for inserting images and also use Image Control in Item Template. Go to FormView Tasks through it's smart tag there you should see below Edit Template Section. In InsertItem Template use FileUpload control for images instead of textbox.

So, now for inserting in the database you need ItemInserting Event see this snippet
  protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        try
        {
            string path = ConfigurationManager.AppSettings["path"].ToString();
            var fileupload = (FileUpload)FormView1.FindControl("FileUpload1");
            if (fileupload.HasFile)
            {
                fileupload.SaveAs(path + fileupload.FileName);
                SqlDataSource1.InsertParameters["img"].DefaultValue = "~/Images/" + fileupload.FileName;
                Response.Write("Done");
            }
            else
            {
                e.Cancel = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }


First, take the path where image will get store, here I've saved my path in webconfig and getting it in a string path. Now FindControl i.e. your File Upload Control, check for file existence by bool HasFile. Then you can set the value of your parameter to the path + file name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1