+ Reply to Thread
Results 1 to 3 of 3

Thread: Saving image in ms sql database

  1. #1
    sumitmehta is offline x10 Sophmore sumitmehta is an unknown quantity at this point
    Join Date
    Oct 2005
    Posts
    106

    Saving image in ms sql database

    Saving image in ms sql database using asp.net in vb.net code

    Hye Guys. I hope this is useful for someone.

    Imports System.Data.OleDb
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Partial Class RSell Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim len As Integer
    Dim len1 As Integer
    len = img.PostedFile.ContentLength
    len1 = img1.PostedFile.ContentLength
    Dim pic(len) As Byte
    Dim pic1(len1) As Byte
    img.PostedFile.InputStream.Read(pic, 0, len)
    img1.PostedFile.InputStream.Read(pic1, 0, len1)
    Dim LoginUser As String
    Dim UserMid As Integer
    Dim midstr As String
    Dim sellstr As String
    Dim disposal, fireplace, furnished, gasrange, microwave, washer, balcony, water, carparking As String
    Dim sellcon As OleDbConnection
    Dim sellcmd As OleDbCommand
    Dim midcmd As OleDbCommand
    Dim midread As OleDbDataReader
    LoginUser = Session("Username")
    If chkDesp.Checked = True Then
    disposal = "yes"
    Else
    disposal = "No"
    End If
    If chkFirePlace.Checked = True Then
    fireplace = "Yes"
    Else
    fireplace = "No"
    End If
    If chkFurnished.Checked = True Then
    furnished = "Yes"
    Else
    furnished = "No"
    End If
    If chkGasRange.Checked = True Then
    gasrange = "Yes"
    Else
    gasrange = "No"
    End If
    If chkMicrowave.Checked = True Then
    microwave = "Yes"
    Else
    microwave = "No"
    End If
    If chkWasher.Checked = True Then
    washer = "Yes"
    Else
    washer = "No"
    End If
    If chkBalcony.Checked = True Then
    balcony = "Yes"
    Else
    balcony = "No"
    End If
    If chkWater.Checked = True Then
    water = "Yes"
    Else
    water = "No"
    End If
    If chkParking.Checked = True Then
    carparking = "Yes"
    Else
    carparking = "No"
    End If
    sellcon = New OleDbConnection("Provider=SQLOLEDB;Data Source=pml;Initial Catalog=property;Integrated Security=SSPI")
    midstr = "select mid from master where username='" & LoginUser & "'"
    midcmd = New OleDbCommand(midstr, sellcon)
    sellcon.Open()
    midread = midcmd.ExecuteReader
    While (midread.Read)
    UserMid = Integer.Parse(midread("mid"))
    End While
    Session("Usermid") = UserMid
    sellstr = "insert into sell(mid,categories,type,country,state,city,area,p incode,bedroom,bathroom,psize,tsize,price,descript ion,hospital,airport,railway,school,tvphone,paints ,ac,motot,lift,rainwater,disposal,fireplace,furnis hed,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 & "',?,? )"
    sellcmd = New OleDbCommand(sellstr, sellcon)
    sellcmd.Parameters.Add("@im", pic)
    sellcmd.Parameters.Add("@im1", pic1)
    sellcmd.ExecuteNonQuery()
    sellcon.Close()
    Response.Redirect("SSSell.aspx")
    End Sub
    End Class

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

  2. #2
    sunils's Avatar
    sunils is offline x10 Spammer sunils is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Chennai ,India
    Posts
    2,264

    Re: Saving image in ms sql database

    Nice tutorial man. I suppose you took it from some web page. Any way nice to have this tutorial over here.
    [LEFT][B]Sunil Sankar
    -------------------------------------------------------------------------

  3. #3
    sumitmehta is offline x10 Sophmore sumitmehta is an unknown quantity at this point
    Join Date
    Oct 2005
    Posts
    106

    Re: Saving image in ms sql database

    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 by sumitmehta; 03-01-2008 at 01:21 AM. Reason: Automerged Doublepost

+ Reply to Thread

Similar Threads

  1. problem on creating sql database
    By joseph0829 in forum Free Hosting
    Replies: 1
    Last Post: 12-05-2007, 01:07 PM
  2. Cannot rename SQL database...?
    By 1337scape in forum Free Hosting
    Replies: 3
    Last Post: 11-12-2007, 03:49 PM
  3. Database links to back end on SQL
    By slinky in forum Free Hosting
    Replies: 1
    Last Post: 11-06-2007, 08:16 PM
  4. errors while attaching
    By mattspec in forum Feedback and Suggestions
    Replies: 0
    Last Post: 12-19-2005, 01:50 PM
  5. November Desktop
    By n4tec in forum Off Topic
    Replies: 12
    Last Post: 11-08-2005, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers