我正在创建一个带有“代码隐藏”的asp.net Web表单,该表单将数据发送到MS Access数据库。
提交表单后,我收到“参数?_1没有默认值”。我已经浏览了大约6个网站,但我找不到足够接近我的解决方案,我可以使用我有限的VB词汇。我对VB或C#语言不太熟悉(我只使用Java和C ++)。
我是否需要默认值?如果需要,我该如何添加?
这是.aspx.vb代码(错误源自的地方):
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 = Request.Form("FirsNtame") 'In paraenthesis may be the item name in the form
Dim strLasNtame As String = Request.Form("LasNtame")
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 LasNtame name
Dim strTeacherEmail As String = Request.Form("TeacherEmail")
Dim strCatagory As String = Request.Form("Catagory") '*Dropdown list
Dim strExibitTite As String = Request.Form("ExibitTite") 'Note : "Title" by itself is a keyword and cannot be used
Dim strElectricity As String = Request.Form("Electricity") '*possible boolean for electricity
'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))
objcmd.ExecuteNonQuery()
'Number of query values and destination fields are not the same.
'Close DB Connection
dbconn.Close()
Response.Write("Thank you for registering!")
End Sub
End Class
这是.asp页面(我很确定错误不是来自这里):
<%@ 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>
答案 0 :(得分:3)
您的查询应该是:
INSERT INTO Exhibits (FirsNtame, LasNtame, StudentEmail, School, Grade, Teacher, TeacherEmail, Category, ExibitTite, Electricity) values (@FirsNtame, @LasNtame, @StudentEmail, @School, @Grade, @Teacher, @TeacherEmail, @Category, @ExibitTite, @Electricity)
请参阅:access 2007 oledb with parameters example。问号在旧的ADO中使用,而不是在ADO.NET中使用
修改强> 参数值不可能是空的。设置为nothing的参数将被视为缺失。
可能的消息:
Dim strLasNtame As String = if(Request.Form("LasNtame"),"")
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
答案 1 :(得分:0)
我在尝试传递可能为“null”的字符串参数时遇到了这个问题,使用一些 C# 代码时,解决方案很简单:
cmd.Parameters.AddWithValue("@TEXTCOL", myString ?? (object)DBNull.Value);