هذا الكود خاص بحفظ البيانات في قاعدة البياناتكود:If (FileUpload1.HasFile) Then SaveFile(FileUpload1.PostedFile) Cn.ConnectionString = cnnStr Cmd.Connection = Cn Cn.Open() Dim maxx As Integer Cmd.CommandText = "select count(Id_Book) from Book" Dim cnt As Integer = Cmd.ExecuteScalar If cnt > 0 Then Cmd.CommandText = "select max(Id_Book) from Book" maxx = Cmd.ExecuteScalar + 1 Else maxx = 1 End If Cmd.CommandText = "select * from Book" Da.SelectCommand = Cmd Da.Fill(Ds, "Book") Dr = Ds.Tables("Book").NewRow Dr("Id_Book") = maxx Dr("Name_Book") = Name_book.Text Dr("Intro_Book") = IntroBook.Text Dr("Resume_book") = Resumebook.Text Dr("Pic_Book") = "imagebook/" + pathname Ds.Tables("Book").Rows.Add(Dr) Da.Update(Ds, "Book") Cn.Close() Response.Redirect("booklist.aspx") Else UploadStatusLabel.Text = "لم تحدد الملف الذي تريد تحميله" End If
وقبل ذلك ينادي على procedure التي تقوم برفع الصور
وهي كالتالي:
لقد وضعت هذا الكودكود:Sub SaveFile(ByVal file As HttpPostedFile) Dim savePath As String = Server.MapPath("../imagebook/") Dim fileName As String = FileUpload1.FileName fileName = Replace(fileName, " ", "") Dim pathToCheck As String = savePath + fileName Dim tempfileName As String = "" Dim Fex As String = IO.Path.GetExtension(fileName) If Fex <> ".gif" Or Fex <> ".GIF" Or Fex <> ".jpg" Or Fex <> ".JPG" Or Fex <> ".bmp" Or Fex <> ".BMP" Or Fex <> ".png" Or Fex <> ".PNG" Then UploadStatusLabel.Text = "نوع الملف الذي تم اختياره غير مسموح به" Exit Sub End If If (System.IO.File.Exists(pathToCheck)) Then Dim counter As Integer = 2 While (System.IO.File.Exists(pathToCheck)) tempfileName = counter.ToString() + fileName pathToCheck = savePath + tempfileName counter = counter + 1 End While fileName = tempfileName UploadStatusLabel.Text = "يوجد ملف يحمل نفس الإسم." + "<br>" + _ "تم حفظ الملف تحت اسم " + fileName End If savePath += fileName FileUpload1.SaveAs(savePath) pathname = fileName End Sub
حتى استثني باقي الملفات من غير الصوركود:If Fex <> ".gif" Or Fex <> ".GIF" Or Fex <> ".jpg" Or Fex <> ".JPG" Or Fex <> ".bmp" Or Fex <> ".BMP" Or Fex <> ".png" Or Fex <> ".PNG" Then UploadStatusLabel.Text = "نوع الملف الذي تم اختياره غير مسموح به" Exit Sub End If
المشكل انه ومع وجود Exit Sub يعود و يكمل تنفيد باقي الكود ويقوم بحفظ البيانات في قاعدة البيانات