Click here to Skip to main content
15,885,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
im working on a simple program that will retrieve data from my .mdb and display in datagrid on my form all is showing well except from image can anybody tell me what is the right format to store image in access database 2007 OLE object and insert the file and use link cause when i do that i dont see my image in the db table it show package by the way the image i've put is .bmp which suppose to work.also in vb in my data grid it set my column as DataGridViewImageColumn when i run the app i get system.AgumentExeption:parameter is not valid plus other stuff.

but if i set the colum as DataGridViewTextBoxColumn i dont get any error but insted of a picture in that column i get (Byte[] Array)

hope someone can help
Posted

thanks for the replay but what you showing me is for vb.net/asp right you showing the image on a webpage or something?

my datagridview is on windows from. i already have a table in my .mdb which contain the image and also a combo box that has data binding which will query my db and display result in the datagrid.

so i just need to know what format to set the column in my table and and how to make the image populate in the image column in my datagrid on my windows form.im using visual basic 2008 express edition.
 
Share this answer
 
You create database in access 2007 then save in word2003 format.

My table structure is:-
Id AutoNumber
Title Text
SaveImage OleObject
Description Memo
Date Datetime



In above table name is Image1
For image save i create a Default.aspx page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td>
                <table width="100%">
                    <tr>
            <td style="width: 30%">
                <asp:HyperLink ID="HyperLink19" runat="server" NavigateUrl="~/crtUser.aspx">Create User</asp:HyperLink></td>
            <td style="width: 30%">
            </td>
            <td style="width: 30%">
                <asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="log">Logout</asp:LinkButton></td>
        </tr>
                </table>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Panel ID="Panel1" runat="server" Width="100%">
                        <table>
                            <tr>
                                <td colspan="3">
                                Add Image
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Title:</td>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server" MaxLength="100"></asp:TextBox></td>
                                <td>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                                        ErrorMessage="Enter the Image Title"></asp:RequiredFieldValidator></td>
                            </tr>
                            <tr>
                                <td>
                                    Image Upload:&nbsp;</td>
                                <td>
                                    <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                                <td>
                                    </td>
                            </tr>
                            <tr>
                                <td>
                                    Description:</td>
                                <td>
                                    <asp:TextBox ID="TextBox2" runat="server" MaxLength="100" TextMode="MultiLine" Width="100%"></asp:TextBox></td>
                                <td>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="3">
                                    <asp:Button ID="Button1" runat="server" Font-Bold="True" Text="Save" Width="121px" />
                                    &nbsp;&nbsp;
                                    <asp:Button ID="Button2" runat="server" Text="Delete" ValidationGroup="Del" Width="79px" /></td>
                            </tr>
                        </table>
                    </asp:Panel>
                 </td>
              </tr>
              <tr>
                <td>
                <asp:Label ID="Label1" runat="server" Text="Some problem in this page."></asp:Label>
                </td>
              </tr>
              <tr>
                <td>
                    &nbsp; &nbsp; <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Image.aspx">Go to Image Gallery</asp:HyperLink>
                </td>
              </tr>
           </table>
    </div>
    </form>
</body>
</html>


Now Default.aspx.vb page write code
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim con As OleDbConnection
    Dim ds As DataSet1
    Dim adapter1 As OleDbDataAdapter

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If FileUpload1.HasFile = True Then
                Dim fileData As Byte() = New Byte(FileUpload1.PostedFile.InputStream.Length) {}
                FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length)

                adapter1 = New OleDbDataAdapter("Select * from ImageSave1", con)
                Dim cmdbuilder As New OleDbCommandBuilder(adapter1)
                ds = New DataSet1()
                adapter1.Fill(ds.Tables(0))

                Dim row As DataRow = ds.Tables(0).Rows.Find(Request.QueryString("ID"))
                row.BeginEdit()
                row("Title") = TextBox1.Text.Trim()
                row("Description") = TextBox2.Text.Trim()
                row("date1") = Date.Now()
                row("saveImage") = fileData
                row.EndEdit()
                adapter1.Update(ds.Tables(0))
                Response.Write("Update successfully")
                TextBox1.Text = ""
                TextBox2.Text = ""
            Else
                adapter1 = New OleDbDataAdapter("Select * from ImageSave1", con)
                Dim cmdbuilder As New OleDbCommandBuilder(adapter1)
                ds = New DataSet1()
                adapter1.Fill(ds.Tables(0))

                Dim row As DataRow = ds.Tables(0).Rows.Find(Request.QueryString("ID"))
                row.BeginEdit()
                row("Title") = TextBox1.Text.Trim()
                row("Description") = TextBox2.Text.Trim()
                row("date1") = Date.Now()
                row.EndEdit()
                adapter1.Update(ds.Tables(0))
                Response.Write("Update successfully")
                TextBox1.Text = ""
                TextBox2.Text = ""
            End If
        Catch
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        con = New OleDbConnection(ConfigurationManager.ConnectionStrings("ImgGallery").ToString())
        If Not Page.IsPostBack Then
            If Request.QueryString("ID") Is Nothing Then
                Panel1.Visible = False
                Label1.Visible = True
            Else
                Panel1.Visible = True
                Label1.Visible = False
                adapter1 = New OleDbDataAdapter("Select * from ImageSave where ID=" & Request.QueryString("ID"), con)
                Dim cmdbuilder As New OleDbCommandBuilder(adapter1)
                ds = New DataSet1()
                adapter1.Fill(ds.Tables(0))
                TextBox1.Text = ds.Tables(0).Rows(0)("Title").ToString()
                TextBox2.Text = ds.Tables(0).Rows(0)("Description").ToString()
            End If
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        adapter1 = New OleDbDataAdapter("Select * from ImageSave", con)
        Dim cmdbuilder As New OleDbCommandBuilder(adapter1)
        ds = New DataSet1()
        adapter1.Fill(ds.Tables(0))

        Dim row As DataRow = ds.Tables(0).Rows.Find(Request.QueryString("ID"))
        row.BeginEdit()
        row("Title") = "Image" + Request.QueryString("ID")
        row("Description") = Nothing
        row("date1") = Date.Now()
        row("saveImage") = Nothing
        row.EndEdit()
        adapter1.Update(ds.Tables(0))
        Response.Write("Delete successfully")
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If Session("user") = "" Or Session("user") Is Nothing Then
            HyperLink19.Visible = False
            LinkButton1.Visible = False
            Session("user") = ""
            Response.Redirect("login.aspx")
        End If
    End Sub

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Session.Abandon()
        Session("user") = ""
        Response.Redirect("~/login.aspx")
    End Sub
End Class



Now For Display Image i create the user control name is Image.ascx

XML
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Image.ascx.vb" Inherits="Image" %>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal">
            <HeaderTemplate>
                 <table cols="4" cellspacing="2" width="100%" style="text-align:center;">
                 <tr>
            </HeaderTemplate>
            <ItemTemplate>
                        <td valign="top" width="25%" Height="92px" >
                            <asp:Image ID="Image1" runat="server" Width="108px" Height="91px"  ImageUrl= '<%# "~/DrawImg.aspx?id="+ DataBinder.Eval(Container.DataItem,"ID").ToString() %>' />
                        <br />
                           <%#DataBinder.Eval(Container.DataItem, "Description")%>
                           <br /> <a style="color:#0000FF;" href='<%# "VwFull.aspx?vID="+ DataBinder.Eval(Container.DataItem,"ID").Tostring() %>' shape="rect">View full size</a>
                           <hr />
            </ItemTemplate>
            <SeparatorTemplate>
                </td>
            </SeparatorTemplate>
            <FooterTemplate>
             </table>
            </FooterTemplate>
        </asp:DataList>
        <br />
    <asp:LinkButton ForeColor="#0000FF" ID="LinkButton1" runat="server"></asp:LinkButton>


Now Image.ascx.vb is

<pre lang="vb">Partial Class Image
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Public Sub Display(ByVal source As PagedDataSource)
        source.AllowPaging = True
        source.PageSize = 16
        Dim current As Integer = 1
        If Not Request.QueryString("page") = Nothing Then
            current = Integer.Parse(Request.QueryString("page"))
        Else
            current = 1
        End If
        source.CurrentPageIndex = current - 1
        DataList1.DataSource = source
        DataList1.DataBind()
        If source.PageCount > 1 Then
            For i As Integer = 1 To source.PageCount
                LinkButton1.Text += " " & "<a style='color: black;' href=Image.aspx?page=" & i.ToString() & ">" & i.ToString() & "</a> "
            Next i
        End If
    End Sub
End Class






Now in above control is drag and drop in Image.aspx page:-
XML
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"  AutoEventWireup="false" CodeFile="Image.aspx.vb" Inherits="image_data_" Title="Image Gallary" ViewStateEncryptionMode="Auto" %>
<%@ Register Src="~/Control/Image.ascx"TagName="Image" TagPrefix="uc1" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" ID="Content1" runat="server">
    <uc1:Image ID="Image1" runat="server" />
</asp:Content>



Now Image.aspx.vb is
<pre lang="vb">Partial Class image_data_
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Page As New PagedDataSource()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ImgGallery").ToString())
        Dim adapter As New SqlDataAdapter("Select * from ImageSave1", con)
        Dim ds As New DataSet()
        adapter.Fill(ds)

        Page.DataSource = ds.Tables(0).DefaultView
        Image1.Display(Page)
    End Sub
End Class




Now i am add DrawImg.aspx where image is drawing:-
XML
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DrawImg.aspx.vb" Inherits="DrawImg" Title="View Image" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>


Now DrawImg.aspx.vb page code is:-

Imports System.IO
Partial Class DrawImg
    Inherits System.Web.UI.Page

    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ImgGallery").ToString())

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("id") <> Nothing Then
            Try
                'Dim myFile As File = Nothing
                Dim a As Byte()

                Dim myCommand As SqlCommand = New SqlCommand("Select * from ImageSave1 where ID=" + Request.QueryString("id"), con)
                con.Open()
                Using myReader As SqlDataReader = myCommand.ExecuteReader()
                    If myReader.Read() Then
                        a = DirectCast(myReader(myReader.GetOrdinal("saveThumb")), Byte())
                    End If
                    myReader.Close()
                End Using
                Response.ContentType = "Image/jpeg"
                Response.BinaryWrite(a)
            Catch
            End Try
        End If
    End Sub
End Class



I hope work successfully.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900