convert name of image button to image button

G

Guest

Instead of drag and drop image button from Toolbox and name the image button id on the properties box, I would like to load image button ID from database. I created a database table that stores Image Button ID and Image (path name of the image for the image button). Example of the data will be: (ImgBtnLight, image\light.gif). The data type of the Image Button ID field in the database table is varchar whereas data type of image button in WebForm.aspx must be ImageButton.

For example, in database table: Image Button ID -> varchar -> 10 -> Not Null
In WebForm.aspx: Dim imgbtnid As ImageButton

Therefore I can't write the code as
Dim imgbtn As ImageButton = dr("Image Button ID"
imgbtn.ImageUrl = dr("Image"

The above codes will return error message
Exception Details: System.InvalidCastException: Specified cast is not valid

Can anyone please help me on this issue. The scenario is I must store the image button id in database and load it in WebForm.aspx. Thank you for your inputs

Thanks
Ester
 
C

Cor Ligthert

Hi Esther,

It is not simple see this sample which does not do the button, however when
that is a problem than maybe I can help you with that as well. It makes from
the images first thumbnails before it is set in the bitmap form.

I hope this helps?

Cor

\\\It needs 2 forms with a listbox, a picturebox and a label on form1
\\\webform1
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim conn As New SqlClient.SqlConnection _
("Server=localhost;" & "DataBase=Northwind;" & _
"Integrated Security=SSPI")
Dim da As New SqlClient.SqlDataAdapter _
("SELECT FileName, PictureID FROM Picture", conn)
Dim ds As New DataSet
Me.Image1.Visible = False
ListBox1.AutoPostBack = True
Try
da.Fill(ds)
ListBox1.DataSource = ds.Tables(0)
ListBox1.DataTextField = "FileName"
ListBox1.DataValueField = "PictureID"
ListBox1.DataBind()
Catch sqlExc As SqlClient.SqlException
Me.Label1.Text = sqlExc.ToString
Catch exc As Exception
Me.Label1.Text = exc.ToString
End Try
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Session.Item("img") = ListBox1.SelectedItem.Value
Image1.Visible = True
Image1.ImageUrl = "http://localhost/WebImage/WebForm2.aspx"
End Sub
///
\\\Webform2
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlClient.SqlConnection("Server=localhost;" & _
"DataBase=Northwind;" & "Integrated Security=SSPI")
Dim sqlstr As String = _
String.Format("SELECT Picture FROM Picture WHERE (PictureID = {0})", _
CInt(Session.Item("img")))
Dim cmd As New SqlClient.SqlCommand(sqlstr, conn)
conn.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
Dim arrImage() As Byte
arrImage = (CType(rdr.Item("Picture"), Byte()))
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.drawing.Image
origimage = System.Drawing.Image.FromStream(ms1)
Dim PThumbnail As System.drawing.Image
PThumbnail = origimage.GetThumbnailImage(100, 100, Nothing, New IntPtr)
Dim ms2 As New System.IO.MemoryStream
PThumbnail.Save(ms2, Imaging.ImageFormat.Bmp)
arrImage = ms2.GetBuffer
Response.BinaryWrite(arrImage)
rdr.Close()
conn.Close()
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top