Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I want to upload image in database.
I have created 1 table name img and taken 1 column pic whose datatype is image
The code is here but showing error that file1 is not a member of webapplication1.image

can anyone help me how to rectify that file1 error in the below line
UpLoadImageFile(Me.File1.Value.Trim())

VB
Imports System.Data.SqlClient
Imports System.IO
Imports System.Data

Public Class Image
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim imageInfo As FileInfo = New FileInfo(Me.File1.value.Trim())
        If imageInfo.Exists() = False Then
            Me.RegisterClientScriptBlock("alertMsg", "<script>alert('please select one image file.');</script>")
        Else
            Select Case (imageInfo.Extension.ToUpper())
                Case ".JPG" : UpLoadImageFile(Me.File1.Value.Trim())
                Case ".GIF" : UpLoadImageFile(Me.File1.Value.Trim())
                Case ".BMP" : UpLoadImageFile(Me.File1.Value.Trim())
                    'default: RegisterClientScriptBlock("alertMsg", "<script>alert('file type error.');</script>")
            End Select
        End If
    End Sub

    Private Sub UpLoadImageFile(ByVal info As String)
        Dim objConn As SqlConnection
        Dim objCom As SqlCommand
        Try
            Dim imagestream As FileStream = New FileStream(info, FileMode.Open)
            Dim data() As Byte
            ReDim data(imagestream.Length - 1)
            imagestream.Read(data, 0, imagestream.Length)

            imagestream.Close()
            objConn = New SqlConnection("loginConnectionString")
            objCom = New SqlCommand("insert into img(pic)values(@Pic)", objConn)

            Dim pictureParameter As SqlParameter = New SqlParameter("@Pic", SqlDbType.Image)
            pictureParameter.Value = data
            objCom.Parameters.Add(pictureParameter)
            objConn.Open()
            objCom.ExecuteNonQuery()
            objConn.Close()
        Catch ex As Exception
            Throw New Exception(ex.Message)
            objConn.Close()
        End Try
    End Sub
End Class
Posted
v2
Comments
Dholakiya Ankit 26-Sep-13 2:22am    
try UpLoadImageFile(Me.File1.Value.ToString().Trim())
ChienVH 26-Sep-13 2:24am    
According to me, shouldn't store image into the db, instead just storing the image path.
sudeshna from bangkok 26-Sep-13 3:31am    
Ya i meant the same
member 8888995 1-Oct-13 1:28am    
Hey Tadit Dash
I was helping to remove errors from code. Although as per your comments , I deleted solutions but a single comment was enough to told this.Why you pasted same thing everywhere? If you have full fledged solution , working code then please paste that as this will be more helpful instead of such suggestions , or if you don't have then write code , test and then post as 'Solution'.
This is what my suggestion to you.

Regards

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