When using Modal Dialogs (esclusive focus windows) in Asp.Net with DataBase retrieved data, there are one small problem: data are loaded from db only the first time you open the window. After that, data is going to be loaded from the cache.
To solve this problem, we have to put into page load even onf the modal dialog page the following lines of code:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetExpires(DateTime.Now - New TimeSpan(1, 0, 0))
Response.Cache.SetLastModified(DateTime.Now)
Response.Cache.SetAllowResponseInBrowserHistory(False)
To open the window as modal dialog, use this javascript method:
hlkNewWindow.Attributes.Add("onclick", "javascript: window.showModalDialog('PageToOpen.aspx', null, 'status:no; dialogWidth:500px; dialogHeight:400px; dialogHide:true; help:no; scroll:no;')"
This example opens a modal dialog of 400x500 pixel, without status bar, buttons and scrolls with a click on the hlkNewWidow HyperLink.
Wednesday, February 25, 2009
Wednesday, February 18, 2009
Load an image from Database with VB.Net
Dim count As Integer = 1
Dim Album As New Collection
Dim Query As String
Dim Archive As SqlConnection
Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Archive = New SqlConnection
Archive.ConnectionString = "connection string"
Query = "SELECT Photo FROM ALBUM"
Dim cmd As New SqlCommand(Query, Archive)
Archive.Open()
Dim I As Integer = 0
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim Temp As Byte ()
While reader.Read
Temp = reader(0)
Album.Add(Temp)
End While
If Album.Count > 0 Then
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Dim Temp As Byte ()
If count > 1 Then
count = count - 1
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim Temp As Byte ()
If count Album.Count Then
count = count + 1
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Dim Album As New Collection
Dim Query As String
Dim Archive As SqlConnection
Private Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
Archive = New SqlConnection
Archive.ConnectionString = "connection string"
Query = "SELECT Photo FROM ALBUM"
Dim cmd As New SqlCommand(Query, Archive)
Archive.Open()
Dim I As Integer = 0
Dim reader As SqlDataReader = cmd.ExecuteReader()
Dim Temp As Byte ()
While reader.Read
Temp = reader(0)
Album.Add(Temp)
End While
If Album.Count > 0 Then
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Dim Temp As Byte ()
If count > 1 Then
count = count - 1
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim Temp As Byte ()
If count Album.Count Then
count = count + 1
Temp = CType (Album.Item(count), Byte())
Dim Img As MemoryStream = New MemoryStream(Temp)
PictureBox1.Image = Image.FromStream(Img)
End If
End Sub
Load image into Database with VB.Net
Dim Archive As SqlConnection
Archive = New SqlConnection
Archive.ConnectionString = "connection string"
Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
OpenFileDialog1.Filter = "BMP - Bitmap Windows | *.bmp | " _
& "GIF - CompuServe Graphics Interchange | *.gif | JPG - " _
& "Compatibile JFIF | *.jpg"
OpenFileDialog1.Title = "Select image..."
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
TextBox1.Text = OpenFileDialog1.FileName.ToString
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName.ToString)
End If
End Sub
Private Function ImageToStream()As Byte ()
Dim Image As New Bitmap(TextBox1.Text)
Dim stream As MemoryStream = New MemoryStream()
Image.Save(stream, _
System.Drawing.Imaging.ImageFormat.Bmp)
Return stream.ToArray()
End Function
Private Sub Load()
If File.Exists(TextBox1.Text) Then
Dim Query As String
Query = "INSERT INTO Album (Foto) VALUES (@Img)"
Dim mycommand As New SqlCommand(Query, Archive)
Archive.Open()
Dim param As SqlParameter = New SqlParameter("@Img", SqlDbType.Binary)
param.Value = ImageToStream()
mycommand.Parameters.Add(param)
mycommand.ExecuteNonQuery()
mycommand = Nothing
Archive.Close()
PictureBox1.Image = Nothing
TextBox1.Text = ""
Else
MessageBox.Show("File not found!!" , ""Test DB Image", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly)
End If
End Sub
Archive = New SqlConnection
Archive.ConnectionString = "connection string"
Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
OpenFileDialog1.Filter = "BMP - Bitmap Windows | *.bmp | " _
& "GIF - CompuServe Graphics Interchange | *.gif | JPG - " _
& "Compatibile JFIF | *.jpg"
OpenFileDialog1.Title = "Select image..."
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> "" Then
TextBox1.Text = OpenFileDialog1.FileName.ToString
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName.ToString)
End If
End Sub
Private Function ImageToStream()As Byte ()
Dim Image As New Bitmap(TextBox1.Text)
Dim stream As MemoryStream = New MemoryStream()
Image.Save(stream, _
System.Drawing.Imaging.ImageFormat.Bmp)
Return stream.ToArray()
End Function
Private Sub Load()
If File.Exists(TextBox1.Text) Then
Dim Query As String
Query = "INSERT INTO Album (Foto) VALUES (@Img)"
Dim mycommand As New SqlCommand(Query, Archive)
Archive.Open()
Dim param As SqlParameter = New SqlParameter("@Img", SqlDbType.Binary)
param.Value = ImageToStream()
mycommand.Parameters.Add(param)
mycommand.ExecuteNonQuery()
mycommand = Nothing
Archive.Close()
PictureBox1.Image = Nothing
TextBox1.Text = ""
Else
MessageBox.Show("File not found!!" , ""Test DB Image", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation, _
MessageBoxDefaultButton.Button1, _
MessageBoxOptions.DefaultDesktopOnly)
End If
End Sub
Monday, January 19, 2009
Read a single value from a query with VB.Net
Sometimes it's necessary to read single value from a table (or a query). Here's the code:
Imports System.Data.Oledb
Dim ConnString As String = "connection string"
' open connection
Dim Conn As New Data.OleDb.OleDbConnection(ConnString)
Cn.Open()
' request execution
Dim SQL As String = "SELECT Data FROM Table WHERE ID = 'aaaa';"
Dim command As New Data.OleDb.OleDbCommand(Sql, Conn)
Dim ID As Integer = command.ExecuteScalar()
'close connection
Cn.Close()
Sunday, January 18, 2009
Image to byte array conversion
It's very easy to convert an image into a byte array, with VB.NET; using MemoryStream object:
Private Function ConvertImageToByteArray(ByVal inputImage As Image) As Byte()
Dim ms As New System.IO.MemoryStream
inputImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Return ms.ToArray
End Function
To re-convert the byte array into image, we can use ImageConverter class:
Private Function ConvertByteArrayToBitmap(ByVal array As Byte()) As Bitmap
Dim ic As New ImageConverter
Dim img As Image = CType(ic.ConvertFrom(array), Image)
Dim bmp As New Bitmap(img)
Return bmp
End Function
Subscribe to:
Posts (Atom)