none
oledbexception was unhandled by user code RRS feed

  • Question

  • Mauri

    I am Tekitaa Mautaake and I doing my BA in Geo and Information System at University of the South Pacific. One of my Lab activity is to create a website that allow a user to enter Question in text box named txtQuestion, as well as Answer in the text box named txtAnswer. I managed to run a website but when I enter the Question with the Answer and click the button named btnQuestion, I received this error (refer to pictures below). Can anyone help me solve this error, much appreciate your help regarding this.

        Protected Sub btnQuestion_Click(sender As Object, e As System.EventArgs) Handles btnQuestion.Click
            lblDisplay.Text = "Question:" & txtQuestion.Text & "<br/>"
            lblDisplay.Text &= "Answer:" & txtAnswer.Text

            adsRevision.InsertParameters("Question").DefaultValue = txtQuestion.Text
            adsRevision.InsertParameters("Answer").DefaultValue = txtAnswer.Text
            adsRevision.Insert()

        End Sub

    adsRevision.Insert() this is where I got the error = (oledbexception was unhandle by user code) below that (You tried to assign the Null value to a variable that is not a Variant data type.)

    When I delete that code, I managed to run website perfectly but data are not stored in the database.

    Below is website coding.

    <%@ Page Title="" Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="Question.aspx.vb" Inherits="Default2" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
        <style type="text/css">
        .style1
        {
            margin-left: 80px;
        }
    </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
        <h1>
        <asp:Label ID="Label1" runat="server" Text="Question:" style="font-weight: 700"></asp:Label>
    &nbsp;&nbsp;
        <br class="style1" />
    &nbsp;<asp:TextBox ID="txtQuestion" runat="server" Height="32px" Width="527px"></asp:TextBox>
        &nbsp;&nbsp;&nbsp;
        <br class="style1" />
        <br class="style1" />
        <asp:Label ID="Label2" runat="server" Text="Answer:" style="font-weight: 700"></asp:Label>
    &nbsp;&nbsp;&nbsp;
        <br class="style1" />
    &nbsp;<asp:TextBox ID="txtAnswer" runat="server" Height="100px" Width="531px"></asp:TextBox>
        <br />
        <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnNew" runat="server" Text="New Question" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnClear" runat="server" Text="Clear" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnQuestion" runat="server" Text="Add Question" />
        <br />
        <br />
    &nbsp;<asp:Label ID="lblDisplay" runat="server" Text="Label"></asp:Label>
            <asp:XmlDataSource ID="xdsRevision" runat="server"
                DataFile="~/App_Data/Question.xml"></asp:XmlDataSource>
            <asp:AccessDataSource ID="adsRevision" runat="server"
                DataFile="~/App_Data/Revision.mdb"
                DeleteCommand="DELETE FROM [Questions] WHERE [ID] = ?"
                InsertCommand="INSERT INTO [Questions] ([ID], [Question], [Answer]) VALUES (?, ?, ?)"
                SelectCommand="SELECT * FROM [Questions]"
                
                UpdateCommand="UPDATE [Questions] SET [Question] = ?, [Answer] = ? WHERE [ID] = ?">
                <DeleteParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="Answer" Type="String" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Question" Type="String" />
                    <asp:Parameter Name="Answer" Type="String" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
            </asp:AccessDataSource>
        <br />
        <br />
    </h1>
    <br />
    <p>
    </p>
    </asp:Content>

    Thank in advance.
    Friday, September 27, 2013 1:58 PM

Answers

  • Hi Tekitaa,

    Welcome to visit this forum.

    I found that the insert command needs three parameters, however, there were only two parameters that were given in .cs file.

    If it is that the primary key of Question table is identity?

    If it is, the insert command should not have the primary key filed.

    If it is not, it is needed that we should specify the primary key.

    More information about to insert data into the database using AccessDataSource control, please have a look at the link below:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insert(v=vs.100).aspx

    If this does not work for you, please let me know.

    Thanks &Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Tekitaa Monday, September 30, 2013 5:16 AM
    Monday, September 30, 2013 2:28 AM
    Moderator