NickG
NickG

Reputation: 138

How do I solve this bug in Visual Studio: "Parameter ?_1 has no default value."?

I'm creating an asp.net web form with some "code behind" that sends the data to a MS Access database.

After submitting the form I receive "Parameter ?_1 has no default value." I've looked through about 6 websites, but I couldn't find a solution close enough to mine that I could use with my limited VB vocabulary. I'm not very familiar with the VB or C# language (I've only used Java & C++).

Do I need a default value and if so, how do I add one?

Here is the .aspx.vb code (where the error stems from):

   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

Here is the .asp page (I'm pretty sure the errors don't originate from here):

<%@ 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 />&nbsp;<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 />&nbsp;<asp:TextBox id="TPhone" runat="server" Width="500px"></asp:TextBox> --%>
            <%-- Might put the above in later --%>
            Catagory :<br />&nbsp;<asp:DropDownList id="Catagory" runat="server" Width="212px">
                <asp:ListItem>Behavorial &amp; Social Sciences</asp:ListItem>
                <asp:ListItem>Biochemistry &amp; Microbiology</asp:ListItem>
                <asp:ListItem>Botany</asp:ListItem>
                <asp:ListItem>Environmental Sciences</asp:ListItem>
                <asp:ListItem>Medicine &amp; Health</asp:ListItem>
                <asp:ListItem>Zoology</asp:ListItem>
                <asp:ListItem>Chemistry</asp:ListItem>
                <asp:ListItem>Computer Science</asp:ListItem>
                <asp:ListItem>Earth &amp; 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 />&nbsp;<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>

Upvotes: 3

Views: 3208

Answers (2)

nwsmith
nwsmith

Reputation: 526

I had this problem, with some C# code, when trying to pass a string parameter that could be 'null', and the solution was simply this:

  cmd.Parameters.AddWithValue("@TEXTCOL", myString ?? (object)DBNull.Value);

Upvotes: 0

IvanH
IvanH

Reputation: 5159

You query should be:

INSERT INTO Exhibits (FirsNtame, LasNtame, StudentEmail, School, Grade, Teacher, TeacherEmail, Category, ExibitTite, Electricity) values (@FirsNtame, @LasNtame, @StudentEmail, @School, @Grade, @Teacher, @TeacherEmail, @Category, @ExibitTite, @Electricity)

See:access 2007 oledb with parameters example. Question marks was used in old ADO and not in ADO.NET

Edit Parameter values cannot be nothing. Parameters set to nothing are treated as missing.

Possible soutions:

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

Upvotes: 3

Related Questions