我正在为我学校的科学博览会开展一个数据库项目。我需要将.aspx webform中的一些数据插入到访问数据库中。我一直在使用“Microsoft Visual Web Developer”编写以下.aspx.vb代码。但是,当我按下“提交”按钮时,它不会按原样将数据发送到数据库。 我缺少哪些开发环境设置步骤?
我一直在关注本教程(http://www.youtube.com/watch?v=szm3BFSOVw0)。 这是aspx源代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="C#" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
}
</script>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server"></head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Science Fair Registration</title>
<form id="form1" runat="server" class="auto-style1">
<br />
<%-- Graphics--%>
<center><h1>Science Fair Registration</h1></center>
<asp:MultiView id="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View id="View1" runat="server">
<br />
First Name:<br />
<asp:TextBox id="TextBoxSFirst" runat="server" Width="500px"></asp:TextBox>
<br />
Last Name:<br />
<asp:TextBox id="TextBoxSLast" runat="server" Width="500px"></asp:TextBox>
<br />
Student Email Address:<br />
<asp:TextBox id="TextBoxSEmail" runat="server" Width="500px"></asp:TextBox>
<br />
<br />
<br />
School:<br />
<asp:TextBox id="TextBoxSchool" runat="server" Width="500px"></asp:TextBox>
<br />
Grade:<br />
<asp:DropDownList id="DropDownListGrade" runat="server" Width="86px">
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>11</asp:ListItem>
<asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
<br />
Teacher's Last Name (only):<br />
<asp:TextBox id="TextBoxTLastName" runat="server" Width="500px"></asp:TextBox>
<br />
Teacher E-mail:<br /> <asp:TextBox ID="TextBoxTEmail" runat="server"
Width="500px"></asp:TextBox>
<br />
<%-- Teacher Phone Number:<br /> <asp:TextBox id="TPhone" runat="server" Width="500px"></asp:TextBox> --%>
<%-- Might put the above in later --%>
<br />
<br />Catagory :<br /> <asp:DropDownList id="DropDownListCatagory" runat="server" Width="212px">
<asp:ListItem>Behavorial & Social Sciences</asp:ListItem>
<asp:ListItem>Biochemistry & Microbiology</asp:ListItem>
<asp:ListItem>Botany</asp:ListItem>
<asp:ListItem>Environmental Sciences</asp:ListItem>
<asp:ListItem>Medicine & Health</asp:ListItem>
<asp:ListItem>Zoology</asp:ListItem>
<asp:ListItem>Chemistry</asp:ListItem>
<asp:ListItem>Computer Science</asp:ListItem>
<asp:ListItem>Earth & Space Sciences</asp:ListItem>
<asp:ListItem>Engineering</asp:ListItem>
<asp:ListItem>Mathematics</asp:ListItem>
<asp:ListItem>Physics</asp:ListItem>
</asp:DropDownList>
<br />
Exibit Title :<br /> <asp:TextBox id="TextBoxTitle" runat="server" Width="500px"></asp:TextBox>
<br />
Does your exhibit use electricity?<br />
<%-- Possible issues here, may need to use 1 & 0 instead--%>
<asp:DropDownList id="DropDownListElectricity" runat="server">
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<asp:Button id="Button1" runat="server" Text="Submit" onclick="Button1_Click"
style="height: 26px" />
<br />
</asp:View>
</asp:MultiView>
</form>
</body>
</html>
这是aspx.vb源:
Imports System
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default1
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'form data requests---------------------------------------------------------------------------------------------------------
Dim strName As String = Request.Form("First") 'In paraenthesis may be the item name in the form
Dim strLast As String = Request.Form("Last")
Dim strStudentEmail As String = Request.Form("StudentEmail")
Dim strSchool As String = Request.Form("School")
Dim numGrade As Integer = Request.Form("Grade") '*Dropdown list
Dim strTeacher As String = Request.Form("Teacher") 'Teacher's last name
Dim strTeacherEmail As String = Request.Form("TeacherEmail")
Dim strCatagory As String = Request.Form("Catagory") '*Dropdown list
Dim strTitle As String = Request.Form("Title")
Dim boolElectricity As Boolean = Request.Form("Electricity") '*possible boolean for electricity
'Open Db Connection---------------------------------------------------------------------------------------------------------
Dim strSQL As String
Dim dbconn As OleDbConnection = Nothing
dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("sf13.mdb"))
dbconn.Open();
'SQL actions ----------------------------------------------------------------------------------------------------------
strSQL = "insert into Exhibits (First, Last, School, Teacher, Title, Grade, Category, TeacherNumber, StudentEmail, Electricity, TeacherEmail) values (?,?,?,?,?,?,?,?,?,?,?)"
objcmd = New OleDbCommand(strSQL, dbconn) 'OleDbCommand should be highlighted - missing an imports....
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@First", strName))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Last", strLast))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@School", strSchool))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Teacher", strTeacher))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Title", strTitle))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Grade", numGrade))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Category", strCatagory))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@StudentEmail", strStudentEmail))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Electricity", boolElectricity))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@TeacherEmail", strTeacherEmail))
objcmd.ExecuteNonQuery()
'Close DB Connection
dbconn.Close()
Response.Write("Thank you for registering")
End Sub
End Class
修正了它 我将语言更改为VB并添加了“代码隐藏标记”。
<%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="ScienceFair.aspx.vb" Inherits="_Default" %>
答案 0 :(得分:1)
看起来你正在将内嵌编码风格与代码隐藏混合在一起。尝试将页面语言更改为VB并指定代码隐藏类:
<%@ Page Language="VB" Inherits="_Default1" %>
此外,删除文件开头的脚本块。
进一步阅读