此网络表单应提供给访问数据库。 然而,它一直在说“Field'Frartits.FirsNtame'之类的东西不能是一个零长度的字符串。”我知道代码有效,因为如果我硬编码值,它会写入数据库。如何获取页面后面代码中变量的值?
这是网页:
<%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="ScienceFair.aspx.vb" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<style type="text/css">
.auto-style1 {
text-align: center;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<div id="RuleBreaker">
<h2 class="auto-style1">Science Fair Registration</h2>
<p class="auto-style1" >
First Name:
<br />
<asp:TextBox ID="FirsNtame" runat="server" AutoCompleteType="FirstName"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="FirsNtameValidator1" runat="server"
ControlToValidate="FirsNtame"
ErrorMessage="A first name is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Last Name:
<br />
<asp:TextBox ID="LasNtame" runat="server" AutoCompleteType="LastName"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="LasNtameValidator2" runat="server"
ControlToValidate="LasNtame"
ErrorMessage="A last name is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Student Email Address:
<br />
<asp:TextBox ID="StudentEmail" runat="server" AutoCompleteType="Email"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="StudentEmailValidator3" runat="server"
ControlToValidate="StudentEmail"
ErrorMessage="A Student Email is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
<br />
School:
<br />
<asp:TextBox ID="School" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="SchoolValidator4" runat="server"
ControlToValidate="School"
ErrorMessage="Your school name is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Grade:
<br />
<asp:DropDownList id="Grade" 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 />
<asp:RequiredFieldValidator id="GradeValidator5" runat="server"
ControlToValidate="Grade"
ErrorMessage="A grade is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Teacher's Last Name (only):<br />
<asp:TextBox id="Teacher" runat="server" Width="500px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="TeacherValidator6" runat="server"
ControlToValidate="Teacher"
ErrorMessage="A teacher last name is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Teacher E-mail:<br /> <asp:TextBox ID="TeacherEmail" runat="server"
Width="500px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="TeacherEmailValidator7" runat="server"
ControlToValidate="TeacherEmail"
ErrorMessage="A teacher email is required field."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
<%-- Teacher Phone Number:<br /> <asp:TextBox id="TPhone" runat="server" Width="500px"></asp:TextBox> --%>
<%-- Might put the above in later --%>
Catagory :<br /> <asp:DropDownList id="Catagory" 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 />
<asp:RequiredFieldValidator id="CatagoryValidator8" runat="server"
ControlToValidate="Catagory"
ErrorMessage="A catagory is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Exibit Title :<br /> <asp:TextBox id="ExibitTite" runat="server" Width="500px"></asp:TextBox>
<br />
<asp:RequiredFieldValidator id="ExibitTiteValidator9" runat="server"
ControlToValidate="ExibitTite"
ErrorMessage="A title is required."
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
Does your exhibit use electricity?<br />
<%-- Possible issues here, may need to use 1 & 0 instead--%>
<asp:DropDownList id="Electricity" runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<br />
<asp:RequiredFieldValidator id="ElectricityValidator10" runat="server"
ControlToValidate="Electricity"
ErrorMessage="Does your computer use electricity?"
ForeColor="Red">
</asp:RequiredFieldValidator>
<br />
</p>
<p class="auto-style1" >
<asp:Button ID="SciFairSubmit" runat="server" Text="Submit" />
<br />
<br />
</p>
</div>
</asp:Content>
这是页面背后的aspx代码:
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub SciFairSubmit_Click(sender As Object, e As EventArgs) Handles SciFairSubmit.Click
'form data requests----------------------------------------------------------------------------C:\Users\NCG-PC\Webmastering\TamugSciFair13\WebSite1\Default1.aspx.vb
Dim strName As String = If(Request.Form("FirsNtame"), "")
Dim strLasNtame As String = If(Request.Form("LasNtame"), "")
Dim strStudentEmail As String = If(Request.Form("strStudentEmail"), "")
Dim strSchool As String = If(Request.Form("strSchool"), "")
Dim numGrade As String = If(Request.Form("numGrade"), "")
Dim strTeacher As String = If(Request.Form("strTeacher"), "")
Dim strTeacherEmail As String = If(Request.Form("strTeacherEmail"), "")
Dim strCatagory As String = If(Request.Form("strCatagory"), "")
Dim strExibitTite As String = If(Request.Form("strExibitTite"), "")
Dim strElectricity As String = If(Request.Form("strElectricity"), "")
'Open Db Connection---------------------------------------------------------------------------------------------------------
Dim strSQL As String
Dim dbconn As OleDbConnection = Nothing
dbconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Server.MapPath("\GalvestonScienceFair\App_Data\sf13.mdb"))
dbconn.Open()
'SQL actions ----------------------------------------------------------------------------------------------------------
strSQL = "INSERT INTO Exhibits (FirsNtame, LasNtame, StudentEmail, School, Grade, Teacher, TeacherEmail, Category, ExibitTite, Electricity) values (@FirsNtame, @LasNtame, @StudentEmail, @School, @Grade, @Teacher, @TeacherEmail, @Category, @ExibitTite, @Electricity)"
Dim objcmd = New OleDbCommand(strSQL, dbconn)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@FirsNtame", strName))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@LasNtame", strLasNtame))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@StudentEmail", strStudentEmail))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@School", strSchool))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Grade", numGrade))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Teacher", strTeacher))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@TeacherEmail", strTeacherEmail))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Category", strCatagory))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@ExibitTite", strExibitTite))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Electricity", strElectricity))
'Sets each parameter to "DBNULL" or "blank" if they are null/blank
'objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@FirsNtame", If(strName, DBNull.Value)))
For Each param As System.Data.OleDb.OleDbParameter In objcmd.Parameters
If param.Value Is Nothing Then
param.Value = DBNull.Value
End If
Next
objcmd.ExecuteNonQuery()
'Close DB Connection
dbconn.Close()
Response.Write("Thank you for registering!")
End Sub
End Class
答案 0 :(得分:0)
添加参数时,请执行以下操作
objcmd.InsertCommand.Parameters.AddWithValue("@ParamName", YorTextBox.Text)