Search This Blog

Friday, July 30, 2010

Fetching image from database in ASP.NET

In asp database the images are stored in form of binary array.So it is never easy to fetch the image and display it in a image control.For doing so we take a round about method where we print the image in a page and ten we direct the image url in the image control to that page.

Next we would follow the steps to do that :

Now we shall add a page named image.aspx and in the image.aspx.vb we shall write the code given below


Partial Class image
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim s As String = Request.QueryString("val")
Try
con.Open()
com.CommandText = "select * from items where pid ='" & s & "'"
com.Connection = con
dr = com.ExecuteReader
dr.Read()
Dim bytImage As Byte() = New Byte() {}
bytImage = dr.Item(7)
Response.ContentType = "image/jpeg"
Response.Expires = 0

Response.Buffer = True

Response.Clear()
Response.BinaryWrite(bytImage)
Response.End()

Catch ex As Exception

MsgBox(ex.Message)

Finally
con.Close()
com.Cancel()
dr.Close()

End Try
End Sub

End Class



In this page we will get the image from the database an a binary form and will binarywrite the image in this page.We shall get the pid from the querystring which we shall pass from the page where the image control is. In that page we will direct the image url as below :

Image1.ImageUrl = "~/image1.aspx?vall=" & DropDownList1.SelectedValue

where dropdownlist1.selected value returns a element of the pid coloum in the items table
I have already shown how to connect the database using the module in my earlier blog

Wednesday, July 21, 2010

Adding image to database in ASP.NET

First we have taken a sql database table (items) with a image type field in it.Next we have to create a connection using a module..

The module should look like this

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient


Public Module connection

Public con As New SqlConnection("Data Source=.\SQLEXPRESS;_AttachDbFilename='J:\shopping\App_Data\Database.mdf';Integrated_ Security=True;User Instance=True")
Public com As New SqlCommand
Public dr As SqlDataReader

End Module


Now to add the image follow the following steps :


Try

Dim imgByte As Byte() = Nothing
If FileUpload1.PostedFile IsNot Nothing AndAlso FileUpload1.PostedFile.FileName <> "" Then


Dim File As HttpPostedFile = FileUpload1.PostedFile
imgByte = New Byte(File.ContentLength - 1) {}
File.InputStream.Read(imgByte, 0, File.ContentLength)
End If

Dim b As Boolean

con.Open()
com.CommandText = "insert into items values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox7.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "',@eimg)"
com.Parameters.AddWithValue("@eimg", imgByte)
com.Connection = con
com.ExecuteNonQuery()


MsgBox("inserted")

Catch ex As Exception
MsgBox(ex.Message)

Finally
con.Close()
com.Cancel()
com.Parameters.Clear()
dr.Close()

End Try


First we shall upload the file using a fileupload control then we shall convert it into a byte stream and would store it into a byte array.Next we would store this byte array into the items database using parametarised query.Hence the image gets stored into the image field

Thursday, July 8, 2010

JAVA Threads

Multithreading refers to two or more tasks executing concurrently within a single program. A thread is an independent path of execution within a program. Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.

Multithreading has several advantages over Multiprocessing such as;

  • Threads are lightweight compared to processes
  • Threads share the same address space and therefore can share both data and code
  • Context switching between threads is usually less expensive than between processes
  • Cost of thread intercommunication is relatively low that that of process intercommunication
  • Threads allow different tasks to be performed concurrently.

Thread Creation

There are two ways to create thread in java;

  • Implement the Runnable interface (java.lang.Runnable)
  • By Extending the Thread class (java.lang.Thread)
Implementing the Runnable Interface

public interface Runnable {

void run();

}

One way to create a thread in java is to implement the Runnable Interface and then instantiate an object of the class. We need to override the run() method into our class which is the only method that needs to be implemented. The run() method contains the logic of the thread.

The procedure for creating threads based on the Runnable interface is as follows:

1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object.

2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method.

3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned.

4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception

Extending Thread Class

The procedure for creating threads based on extending the Thread is as follows:

1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.

2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.

3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.


When creating threads, there are two reasons why implementing the Runnable interface may be preferable to extending the Thread class:

  • Extending the Thread class means that the subclass cannot extend any other class, whereas a class implementing the Runnable interface
    has this option.
  • A class might only be interested in being runnable, and therefore, inheriting the full overhead of the Thread class would be excessive.