Saving image in ms sql database

sumitmehta

New Member
Messages
215
Reaction score
1
Points
0
[FONT=&quot]Saving image in ms sql database using asp.net in vb.net code[/FONT]

[FONT=&quot]Hye Guys. I hope this is useful for someone.[/FONT]

[FONT=&quot]Imports[/FONT][FONT=&quot] System.Data.OleDb[/FONT]
[FONT=&quot]Imports[/FONT][FONT=&quot] System.Drawing[/FONT]
[FONT=&quot]Imports[/FONT][FONT=&quot] System.Drawing.Imaging[/FONT]
[FONT=&quot]Partial[/FONT][FONT=&quot] Class RSell[/FONT][FONT=&quot] Inherits System.Web.UI.Page[/FONT]

[FONT=&quot] Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click[/FONT]
[FONT=&quot] Dim len As Integer[/FONT]
[FONT=&quot] Dim len1 As Integer[/FONT]
[FONT=&quot] len = img.PostedFile.ContentLength[/FONT]
[FONT=&quot] len1 = img1.PostedFile.ContentLength[/FONT]
[FONT=&quot] Dim pic(len) As Byte[/FONT]
[FONT=&quot] Dim pic1(len1) As Byte[/FONT]
[FONT=&quot] img.PostedFile.InputStream.Read(pic, 0, len)[/FONT]
[FONT=&quot] img1.PostedFile.InputStream.Read(pic1, 0, len1)[/FONT]
[FONT=&quot] Dim LoginUser As String[/FONT]
[FONT=&quot] Dim UserMid As Integer[/FONT]
[FONT=&quot] Dim midstr As String[/FONT]
[FONT=&quot] Dim sellstr As String[/FONT]
[FONT=&quot] Dim disposal, fireplace, furnished, gasrange, microwave, washer, balcony, water, carparking As String[/FONT]
[FONT=&quot] Dim sellcon As OleDbConnection[/FONT]
[FONT=&quot] Dim sellcmd As OleDbCommand[/FONT]
[FONT=&quot] Dim midcmd As OleDbCommand[/FONT]
[FONT=&quot] Dim midread As OleDbDataReader[/FONT]
[FONT=&quot] LoginUser = Session("Username")[/FONT]
[FONT=&quot] If chkDesp.Checked = True Then[/FONT]
[FONT=&quot] disposal = "yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] disposal = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkFirePlace.Checked = True Then[/FONT]
[FONT=&quot] fireplace = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] fireplace = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkFurnished.Checked = True Then[/FONT]
[FONT=&quot] furnished = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] furnished = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkGasRange.Checked = True Then[/FONT]
[FONT=&quot] gasrange = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] gasrange = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkMicrowave.Checked = True Then[/FONT]
[FONT=&quot] microwave = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] microwave = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkWasher.Checked = True Then[/FONT]
[FONT=&quot] washer = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] washer = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkBalcony.Checked = True Then[/FONT]
[FONT=&quot] balcony = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] balcony = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkWater.Checked = True Then[/FONT]
[FONT=&quot] water = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] water = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] If chkParking.Checked = True Then[/FONT]
[FONT=&quot] carparking = "Yes"[/FONT]
[FONT=&quot] Else[/FONT]
[FONT=&quot] carparking = "No"[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] sellcon = New OleDbConnection("Provider=SQLOLEDB;Data Source=pml;Initial Catalog=property;Integrated Security=SSPI")[/FONT]
[FONT=&quot] midstr = "select mid from master where username='" & LoginUser & "'"[/FONT]
[FONT=&quot] midcmd = New OleDbCommand(midstr, sellcon)[/FONT]
[FONT=&quot] sellcon.Open()[/FONT]
[FONT=&quot] midread = midcmd.ExecuteReader[/FONT]
[FONT=&quot] While (midread.Read)[/FONT]
[FONT=&quot] UserMid = Integer.Parse(midread("mid"))[/FONT]
[FONT=&quot] End While[/FONT]
[FONT=&quot] Session("Usermid") = UserMid[/FONT]
[FONT=&quot] sellstr = "insert into sell(mid,categories,type,country,state,city,area,pincode,bedroom,bathroom,psize,tsize,price,description,hospital,airport,railway,school,tvphone,paints,ac,motot,lift,rainwater,disposal,fireplace,furnished,gasrange,microwave,washer,facing,balcony,water,pcar,img,img1) values (" & UserMid & ",'" & cboCate.SelectedItem.Text & "','" & cboType.SelectedItem.Text & "','" & cboCountry.SelectedItem.Text & "','" & cboState.SelectedItem.Text & "','" & cboCity.SelectedItem.Text & "','" & txtArea.Text & "','" & txtPincode.Text & "','" & cboBedroom.SelectedItem.Text & "','" & cboBathroom.SelectedItem.Text & "','" & txtSize.Text & "','" & cboSize.SelectedItem.Text & "','" & cboPrice.SelectedItem.Text & "','" & txtDescription.Text & "','" & txtHospital.Text & "','" & txtAirPort.Text & "','" & txtRailway.Text & "','" & txtSchool.Text & "','" & txtTv.Text & "','" & txtPaints.Text & "','" & txtAc.Text & "','" & txtMotor.Text & "','" & txtLift.Text & "','" & txtRainwater.Text & "','" & disposal & "','" & fireplace & "','" & furnished & "','" & gasrange & "','" & microwave & "','" & washer & "','" & cboFacing.SelectedItem.Text & "','" & balcony & "','" & water & "','" & carparking & "',?,? )"[/FONT]
[FONT=&quot] sellcmd = New OleDbCommand(sellstr, sellcon)[/FONT]
[FONT=&quot] sellcmd.Parameters.Add("@im", pic)[/FONT]
[FONT=&quot] sellcmd.Parameters.Add("@im1", pic1)[/FONT]
[FONT=&quot] sellcmd.ExecuteNonQuery()[/FONT]
[FONT=&quot] sellcon.Close()[/FONT]
[FONT=&quot] Response.Redirect("SSSell.aspx")[/FONT]
[FONT=&quot] End Sub[/FONT]
[FONT=&quot]End[/FONT][FONT=&quot] Class

I will post SSell.aspx code in another thread. The thread would be named as 'Uploading image in ms sql database'
[/FONT]
 

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
Nice tutorial man. I suppose you took it from some web page. Any way nice to have this tutorial over here.
 

sumitmehta

New Member
Messages
215
Reaction score
1
Points
0
Well yes. I am always in search of articles which can be useful for me as well as others. Whenever I get time out of my busy schedule, I just switch on Google.
Edit:
OK now next part as follows

(Upload in asp.net page )
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Partial Class SSell
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Dim LoginUser As String
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim s As String
Dim read1 As OleDbDataReader
Dim intmid As Integer
Dim stram As New MemoryStream
Dim image() As Byte
Dim bit As Bitmap
intmid = Session("Usermid")
con = New OleDbConnection("Provider=SQLOLEDB;Data Source=pml;Initial Catalog=property;Integrated Security=SSPI")
con.Open()
cmd = New OleDbCommand("select img from sell where mid =?", con)
cmd.Parameters.Add("@mid", intmid)
'image = (Byte)cmd.ExecuteScalar
read1 = cmd.ExecuteReader
While (read1.Read)
Image = read1("img")
End While
stram.Write(Image, 0, Image.Length())
bit = New Bitmap(stram)
Response.ContentType = "image/gif"
bit.Save(Response.OutputStream, ImageFormat.Gif)
Response.Write("This image")
con.Close()
End Sub
End Class


Call this SSell.aspx page in SSSell.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SSSell.aspx.vb" Inherits="SSSell" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;<img src="SSell.aspx" />
<asp:Label ID="Label1" runat="server" Text="This is my first image"></asp:Label>
</form>
</body>
</html>
 
Last edited:
Top