Wednesday, February 25, 2009

Modal Dialog (window.showModalDialog) and cache problem

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 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

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