18-10-17, 06:33 PM
(18-10-17, 06:24 PM)aftfm كتب : السلام عليكم ورحمةالله وبركاته
تظهر رسالة خطأ في البرنامج عند استدعاء البيانات من قاعدة البيانات لاظهارها على مربع النص
ال TextBox
ومن ضمن هذه البيانات اداة الصورة PictureBox
هنا المشكلة في اظهار الصورة من قاعدة البيانات الي اداة الصورة
وهذا نص الكود
كود :
Using MYDataAdapter As New OleDbDataAdapter("SELECT * FROM tblCommunity " & MYFilter & "", Con)
Call MYDataAdapter.Fill(DataSet3, "tblCommunity")
If Me.BindingContext(DataSet3, "tblCommunity").Count <> 0 Then
Call MYDataAdapter.Fill(DataSet3, "CommunityTable")
For i = 0 To DataSet3.Tables(0).Rows.Count - 1
txtID.Text = DataSet3.Tables(0).Rows(i)("ID")
txtHeadComm.Text = DataSet3.Tables(0).Rows(i)("HeadComm")
txtIDHead.Text = DataSet3.Tables(0).Rows(i)("IDHead")
txtMobHead.Text = DataSet3.Tables(0).Rows(i)("MobHead")
txtNaHead.Text = DataSet3.Tables(0).Rows(i)("NaHead")
txtAddrHead.Text = DataSet3.Tables(0).Rows(i)("AddrHead")
txtNotesHead.Text = DataSet3.Tables(0).Rows(i)("NotesHead")
txtAssHead.Text = DataSet3.Tables(0).Rows(i)("AssHead")
txtIDAssHead.Text = DataSet3.Tables(0).Rows(i)("IDAssHead")
txtMobAssHe.Text = DataSet3.Tables(0).Rows(i)("MobAssHe")
txtNaAssHe.Text = DataSet3.Tables(0).Rows(i)("NaAssHe")
txtAddAssHe.Text = DataSet3.Tables(0).Rows(i)("AddAssHe")
txtNotesAssHe.Text = DataSet3.Tables(0).Rows(i)("NotesAssHe")
txtAddressComm.Text = DataSet3.Tables(0).Rows(i)("AddressComm")
txtCommCensus.Text = DataSet3.Tables(0).Rows(i)("CommCensus")
RTBMPA.Text = DataSet3.Tables(0).Rows(i)("MPA")
PBImageHead.Image = DataSet3.Tables(0).Rows(i)("ImageHead")
Next
ENF IF
END USING
أستخدم الكود الأتي لعرض البيانات و الصوره :
كود :
' show user data from the database
Public Sub showcustdata()
Try
'فحص الاتصال بقاعدة البيانات
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
Dim cmd As New SqlCommand()
cmd.Connection = SQL.conn
cmd.CommandText = "select * from customers where cust_id =@cust_id "
cmd.Parameters.AddWithValue("@cust_id", cust_id_txt.Text)
Dim dr As SqlDataReader = cmd.ExecuteReader
While dr.Read
name_txt.Text = dr.Item(1)
nickname_txt.Text = dr.Item(2)
city_cmb.SelectedItem = dr.Item(3)
phone1_txt.Text = dr.Item(4)
phone2_txt.Text = dr.Item(5)
phone3_txt.Text = dr.Item(6)
email_txt.Text = dr.Item(7)
Datepicker1.Value = dr.Item(9)
End While
show_image()
dr.Close()
SQL.conn.Close()
Catch ex As SqlException
MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
End Try
End Sub
'++++++++++++++++++++++++++++++++++++++ SHOW IMAGE ++++++++++++++++++++++++++++++++'
Private Function ImageToStream(ByVal fileName As String) As Byte()
Dim stream As New MemoryStream()
tryagain:
Try
Dim image As New Bitmap(fileName)
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
Catch ex As Exception
GoTo tryagain
End Try
Return stream.ToArray()
End Function
Private Sub show_image()
Dim dr As SqlDataReader
Dim cmd As SqlCommand
Dim stream As New MemoryStream()
Dim query As String
query = "select * from customers where cust_id=" & cust_id_txt.Text
'فحص الاتصال بقاعدة البيانات
If SQL.conn.State = ConnectionState.Open Then
SQL.conn.Close()
End If
SQL.conn.Open()
cmd = New SqlCommand(query, SQL.conn)
'dr = Cmd.ExecuteReader
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
If dr("image").ToString() <> "" Then
Dim image As [Byte]() = DirectCast(dr("image"), [Byte]())
stream.Write(image, 0, image.Length)
Dim bitmap As New Bitmap(stream)
'PictureBox1.Image = bitmap
SetPicture(bitmap, PictureBox1)
End If
End While
End Sub
Private Sub SetPicture(ByVal imgSrc As Bitmap, ByVal target As PictureBox)
Dim imgDest As Bitmap = New Bitmap(target.ClientSize.Width, target.ClientSize.Height)
Dim source_aspect, dest_aspect, k_aspect, K As Double
source_aspect = imgSrc.Width / imgSrc.Height
dest_aspect = imgDest.Width / imgSrc.Height
k_aspect = dest_aspect / source_aspect
If (k_aspect > 1) Then
K = imgSrc.Height / imgDest.Height
Else
K = imgSrc.Width / imgDest.Width
End If
Dim x_new, y_new, w_new, h_new As Integer
w_new = CInt(imgSrc.Width / K)
h_new = CInt(imgSrc.Height / K)
x_new = CInt(imgDest.Width / 2 - w_new / 2)
y_new = CInt(imgDest.Height / 2 - h_new / 2)
Dim g As Graphics = Graphics.FromImage(imgDest)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawImage(imgSrc, New Rectangle(x_new, y_new, w_new, h_new))
g.Dispose()
target.Image = imgDest
End Subيظهر هذا الخطا لانك تقوم بتخزين الطوره على شكل بايت
و انت تحاول عرضها بدون تحويل البايت الى صوره .
و انت تحاول عرضها بدون تحويل البايت الى صوره .
