0

I am programming a Website Note -Not A Project in VB in Visual Studio 2010 and have a LINQ database tied in. I have a user registration page built that prompts the user for their name, email, password and the admin level they will require. The is also a function to add a photo. This is implemented via a standard asp:fileupload control. Currently it is not working. I would like for the file to be browsed for, and then all user details added to the database on the click of the "register" button. The code i have so far is below.

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If IsPostBack Then
        Dim UpPath As String
        UpPath = "~/Uploads/"
        Image1.Visible = True
        Image1.ImageUrl = Session("ImagePath")

        If Not Directory.Exists(UpPath) Then
            Directory.CreateDirectory("C:\UploadedUserFiles\")
        End If

    End If
End Sub

Protected Sub btnRegister_Click(sender As Object, e As System.EventArgs) Handles btnRegister.Click


    Dim savePath = "\Uploads\"
    Dim appPath As String = Server.MapPath("~")

    If (FileUPload1.HasFile) Then
        Dim savePath2 As String = savePath & Session("currentuser") & "" & FileUPload1.FileName
        FileUPload1.SaveAs(appPath & savePath2)
        Session("ImagePath") = "." & savePath
    End If


    ' variables to store the user's registration details
    Dim username As String
    Dim email As String
    Dim password As String
    Dim retypedPassword As String

    ' variables to store the user's selected roles
    Dim productOwner As Boolean
    Dim projectManager As Boolean
    Dim scrumMaster As Boolean
    Dim developer As Boolean

    ' populate variables with the values from the web page
    username = txtUsername.Text
    email = txtEmail.Text
    password = txtPassword.Text
    retypedPassword = txtRetypePassword.Text

    ' check which user roles have been selected
    productOwner = checkProductOwner.Checked
    projectManager = checkProjectManager.Checked
    scrumMaster = checkScrumMaster.Checked
    developer = checkDeveloper.Checked


    ' boolean to check if the entered details are valid
    Dim isValid As Boolean
    isValid = True

    ' check if the values entered by the user are valid
    If _
        String.IsNullOrEmpty(username) Or _
        String.IsNullOrEmpty(email) Or _
        String.IsNullOrEmpty(password) Or _
        String.IsNullOrEmpty(retypedPassword) Or _
        password <> retypedPassword Then
        isValid = False
    End If

    ' if the values are valid, then populate the USER table with the new user and the USER ROLES table
    ' with the roles they are allowed in any project that will be created

    If isValid Then
        ' set up LINQ connection with database
        Dim db As New AgileClassesDataContext()

        ' create a user to populate a row in the USER table
        Dim user As New User With _
        {.Name = username, _
         .Password = password, _
         .Email = email}

        ' add the new user to the USER table
        db.Users.InsertOnSubmit(user)

        ' submit the changes to the database
        Try
            db.SubmitChanges()
        Catch ex As Exception
            Console.WriteLine(ex)
            db.SubmitChanges()
        End Try

Currently I have not declared 'FileUPload1' in the database because whenever i try to add it i get an error saying "Cannot converet type System.Web.UI.WebControls.FileUpload to System.Data.Linq.Binary" I don't know what to do to avoid this. The database table I am using is:

User

-UserID (int, incrementing) -Name (nvarchar) -Password (nvarchar) -Email (nvarchar) -PhotoID (image)

Any suggestions would be great. Thanks

Gavin
  • 437
  • 2
  • 8
  • 20

2 Answers2

1

I'd recommend you not to save images into the database when you building a website. You'd better save images on your hardware and push only image path to the database.

There are some explanations of best practices, so take a look:

https://stackoverflow.com/a/348373/350977

Community
  • 1
  • 1
Michael Samteladze
  • 1,310
  • 15
  • 38
  • Hi thanks for the reply. Unfortunately it is not the straight forward for me as this is a university project that is to be hosted on uni servers so I cannot guarantee a specific hardware location, whereas if the images (a small amount) are held in the database I can package it all together and host it – Gavin Nov 29 '12 at 17:34
  • If your only way to save files is database and you are using SQL Server 2008 or higher, it supports variable type "image". You can save you pictures as this type in database. – Michael Samteladze Nov 29 '12 at 18:15
  • i have the image field already declared in my database but i do not know how to actually get the image to upload into it. As mentioned, I am using LINQ. The full scope of the issue is that I have a registration page (code above). The user needs to be able to enter their: -Name, Email, Password, Admin Levels and browse for and upload a picture. I want all events to be handled by one submit button. I do not know how to tie the fileupload control to the submit button to collate all the data and enter it as one user – Gavin Nov 29 '12 at 20:31
0

If you finally decide to do it with SQL Server, so you need to:

  1. Create a field in the database that will be type of image
  2. Create a stored procedure that will receive all your parameters with this image
  3. On you application side you'll need to convert you uploaded file to System.Data.Linq.Binary type for passing it to sp and you can do it like this: new System.Data.Linq.Binary(FileUpload1.FileBytes)

After you retrieve your image from database, you'll have the same object type of System.Data.Linq.Binary. To display in on your website you must:

  1. Convert it to base64 string
  2. Push this base64 to img src

    System.Data.Linq.Binary img = "Image retrieved from your database"

    img src="data:image;base64,@Convert.ToBase64String(img.ToArray())"

Michael Samteladze
  • 1,310
  • 15
  • 38