我正在编写一个网站注释 -在 Visual Studio 2010 中的 VB 中不是一个项目,并绑定了一个 LINQ 数据库。我构建了一个用户注册页面,提示用户输入他们的姓名、电子邮件、密码和他们需要的管理员级别。这也是添加照片的功能。这是通过标准的 asp:fileupload 控件实现的。目前它不工作。我希望浏览文件,然后单击“注册”按钮将所有用户详细信息添加到数据库中。我到目前为止的代码如下。
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
目前我还没有在数据库中声明“FileUPload1”,因为每当我尝试添加它时,我都会收到一条错误消息“无法将 System.Web.UI.WebControls.FileUpload 类型转换为 System.Data.Linq.Binary”我不知道怎么做才能避免这种情况。我正在使用的数据库表是:
用户
-UserID (int, 递增) -Name (nvarchar) -Password (nvarchar) -Email (nvarchar) -PhotoID (image)
任何建议都会很棒。谢谢