الدرس (9) تكويد صفحة الإضافة


من خلال هذا الشرح سنعمل على تكويد صفحة الإضافة وإضافة كود لعملية رفع الصورة الى الخادم،،

الكود المستخدم في الفيديو



Imports System.IO
Imports System.Data.SqlClient

'------------------------------------------------

Partial Class Admin_AddContent
Inherits System.Web.UI.Page

Dim TxTImage As New TextBox

'------------------------------------------------

Protected Sub UpLoadThisFile(ByVal upload As FileUpload)

If FUpload.HasFile Then

Dim TheFileName As String = Path.Combine(Server.MapPath("~/ImageUpload/"), Date.Now.ToString("yyyyMMddhhHHmmss") + FUpload.FileName)
upload.SaveAs(TheFileName)

TxTImage.Text = "~/ImageUpload/" + Date.Now.ToString("yyyyMMddhhHHmmss") + FUpload.FileName

InsertNew()

Else

TxTImage.Text = "~/Image/NoImage.png"

InsertNew()
End If

End Sub

'------------------------------------------------

Protected Sub InsertNew()

Try

Dim conn As New SqlConnection
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionStringComAhmad").ConnectionString

conn.Open()
Dim sql As String = " INSERT INTO TContent (FIDCategory, ContentTitle, ContentDaTi, ContentSummary, ContentImage, ContentText) VALUES (@FIDCategory, @ContentTitle, @ContentDaTi, @ContentSummary, @ContentImage, @ContentText)"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)

cmd.Parameters.AddWithValue("@FIDCategory", TxTCategory.SelectedValue)
cmd.Parameters.AddWithValue("@ContentTitle", TxTTitle.Text.Trim)
cmd.Parameters.AddWithValue("@ContentDaTi", Date.Now.ToString("yyyy/MM/dd"))
cmd.Parameters.AddWithValue("@ContentSummary", TxTSumm.Text.Trim)
cmd.Parameters.AddWithValue("@ContentImage", TxTImage.Text.Trim)
cmd.Parameters.AddWithValue("@ContentText", TxTMore.Text.Trim)

cmd.ExecuteScalar()
conn.Close()

Lbmsg.ForeColor = Drawing.Color.Blue
Lbmsg.Text = "تم إضافة المحتوى بنجاح"

Catch ex As Exception

Lbmsg.ForeColor = Drawing.Color.Red
Lbmsg.Text = "خطأ لم يتم إضافة المحتوى" + ex.ToString

End Try

End Sub

'------------------------------------------------

Protected Sub BuAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BuAdd.Click

If FUpload.HasFile Then

Dim ValidatFileTy As String() = {"bmb", "gif", "png", "jpg", "jpeg"}
Dim Ext As String = System.IO.Path.GetExtension(FUpload.PostedFile.FileName)

Dim isValidFile As Boolean = False
For i As Integer = 0 To ValidatFileTy.Length - 1
If Ext = "." & ValidatFileTy(i) Then
isValidFile = True
End If
Next

If Not isValidFile Then
Lbmsg.ForeColor = Drawing.Color.Red
Lbmsg.Text = " الملفات المسموح بها فقط " & String.Join(",", ValidatFileTy)
Exit Sub
Else

UpLoadThisFile(FUpload)

End If

Else

UpLoadThisFile(FUpload)

End If

End Sub

'------------------------------------------------

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Title = "إضافة محتوى"
End Sub
End Class