学校のサイエンス フェアのデータベース プロジェクトに取り組んでいます。.aspx Web フォームからアクセス データベースにデータを挿入する必要があります。「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" %>