I am trying to add an image to the database, with the code below but it does not insert to the database, can't see why this is the case, any help?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim imageInfo As FileInfo = New FileInfo(Me.File1.Value.Trim())
If imageInfo.Exists() = False Then
' Me.ClientScript.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(strconn)
objCom = New SqlCommand("insert into tblImgvec(imageName,vecImg, vecId)values(@imageName,@vecImg,@vecId)", objConn)
Dim categorynameParameter As SqlParameter = New SqlParameter("@imageName", SqlDbType.NVarChar)
If Me.txtFileName.Text.Trim().Equals("") Then
categorynameParameter.Value = "Default"
Else
categorynameParameter.Value = Me.txtFileName.Text.Trim()
End If
objCom.Parameters.Add(categorynameParameter)
Dim pictureParameter As SqlParameter = New SqlParameter("@vecImg", 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
I am programming in vb but thanks, I am working through a tutorial and have come across the following error
'dt is not declared, it may be not be accesible.
Dim fselect As String
Dim adp As New SqlDataAdapter
Dim ds As New DataSet
fselect = "select fileid,ImageName from upload"
Dim con As New SqlConnection()
con.ConnectionString = ConfigurationManager.ConnectionStrings("MyString").ConnectionString
Dim cmd As New SqlCommand()
cmd = New SqlCommand(fselect, con)
con.Open()
adp = New SqlDataAdapter(cmd)
ds = New DataSet()
adp.Fill(ds)
dt = ds.Tables(0)
Session("dataimage") = dt
imggrid.DataSource = dt
imggrid.DataBind()
jabaarali786
Member
61 Points
46 Posts
Inserting image to database
Feb 27, 2012 02:03 PM|LINK
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim imageInfo As FileInfo = New FileInfo(Me.File1.Value.Trim()) If imageInfo.Exists() = False Then ' Me.ClientScript.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(strconn) objCom = New SqlCommand("insert into tblImgvec(imageName,vecImg, vecId)values(@imageName,@vecImg,@vecId)", objConn) Dim categorynameParameter As SqlParameter = New SqlParameter("@imageName", SqlDbType.NVarChar) If Me.txtFileName.Text.Trim().Equals("") Then categorynameParameter.Value = "Default" Else categorynameParameter.Value = Me.txtFileName.Text.Trim() End If objCom.Parameters.Add(categorynameParameter) Dim pictureParameter As SqlParameter = New SqlParameter("@vecImg", 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 Subvijay_myl
Contributor
5070 Points
1068 Posts
Re: Inserting image to database
Feb 27, 2012 02:08 PM|LINK
hi..
TRy the below link....
http://www.dotnetcode.in/2011/02/upload-image-or-file-into-database-in.html
http://www.dotnetcode.in/2011/06/how-to-retrieve-uploaded-images-from.html
My .NET blog
Submit Article
kedarrkulkar...
All-Star
34411 Points
5534 Posts
Re: Inserting image to database
Feb 27, 2012 02:08 PM|LINK
what is the Me.File1.Value here?
is it a hidden field? if yes, does it have a path of file on server? or client? I cant see image being uploaded from client to server.....
what is u r aspx code
KK
Please mark as Answer if post helps in resolving your issue
My Site
jabaarali786
Member
61 Points
46 Posts
Re: Inserting image to database
Feb 27, 2012 03:33 PM|LINK
l
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mystring%>" SelectCommand="SELECT * FROM [tblSub]" InsertCommand="insert into [tblSub]([subjectName], [subjectDescription]) values(@name, @desc)"> </asp:SqlDataSource> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="subjectId" DataSourceID="SqlDataSource1" ForeColor="Black" Height="50px" Width="380px"> <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <Fields> <asp:TemplateField HeaderText="subjectName" SortExpression="subjectName"> <InsertItemTemplate> <asp:TextBox ID="txtSubjectName" runat="server" Text='<%# Bind("subjectName") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="subreq" runat="server" ErrorMessage="Enter Subject Name" ControlToValidate="txtSubjectName"> </asp:RequiredFieldValidator> </InsertItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtSubjectName" runat="server"></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("subjectName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="subjectDescription" SortExpression="subjectDescription"> <InsertItemTemplate> <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("subjectDescription") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Description" ControlToValidate="txtDescription"> </asp:RequiredFieldValidator> </InsertItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtDescription" runat="server" Text='<%# Bind("subjectDescription") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("subjectDescription") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Image"> <InsertItemTemplate> <asp:FileUpload ID="FileUpload1" runat="server"> </asp:FileUpload> <asp:RequiredFieldValidator ID="imgreq" runat="server" ErrorMessage="You must add image" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> </Fields> <FooterStyle BackColor="Red" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="Red" ForeColor="Black" HorizontalAlign="Left" /> <RowStyle BackColor="White" /> </asp:DetailsView> <asp:Button Text="Insert" runat="server" ID="btnInsert" /> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>jabaarali786
Member
61 Points
46 Posts
Re: Inserting image to database
Feb 27, 2012 03:38 PM|LINK
Thanks Il take a look at them
Primillo
Star
8731 Points
1681 Posts
Re: Inserting image to database
Feb 27, 2012 04:54 PM|LINK
Hi
Check this tutorial:
http://v4.dbtutorials.com/c/uploading-files-to-a-database-in-c-2/
Primillo
http://www.facebook.com/programandopuntonet
jabaarali786
Member
61 Points
46 Posts
Re: Inserting image to database
Feb 27, 2012 05:15 PM|LINK
I am programming in vb but thanks, I am working through a tutorial and have come across the following error 'dt is not declared, it may be not be accesible.
Dim fselect As String Dim adp As New SqlDataAdapter Dim ds As New DataSet fselect = "select fileid,ImageName from upload" Dim con As New SqlConnection() con.ConnectionString = ConfigurationManager.ConnectionStrings("MyString").ConnectionString Dim cmd As New SqlCommand() cmd = New SqlCommand(fselect, con) con.Open() adp = New SqlDataAdapter(cmd) ds = New DataSet() adp.Fill(ds) dt = ds.Tables(0) Session("dataimage") = dt imggrid.DataSource = dt imggrid.DataBind()jabaarali786
Member
61 Points
46 Posts
Re: Inserting image to database
Feb 27, 2012 06:52 PM|LINK
I have gone through the following tutorial <http://www.dotnetcode.in/2011/06/how-to-retrieve-uploaded-images-from.html>, completed it and the images are not showing, the grid shows but the image seem to not be retrieved. Any suggestion on what I should look for??
AsPxFrK
Member
232 Points
93 Posts
Re: Inserting image to database
Feb 27, 2012 07:07 PM|LINK
Is the image going into the database okay? You're just having trouble getting the image to display?
This should help: http://forums.asp.net/t/1773760.aspx/1?Select+Image+works+in+C+but+not+in+VB
jabaarali786
Member
61 Points
46 Posts
Re: Inserting image to database
Feb 27, 2012 07:32 PM|LINK
Yes the image is going into the database, but it is not being displayed. Development is in vb.net also