?
Solved

Inserting data with AccessDataSource control

Posted on 2006-11-16
4
Medium Priority
?
979 Views
Last Modified: 2008-01-09
I am trying to insert data from a form into an Access database.

I am using ASP.NET 2.0 and an AccessDataSource control as follows

    <asp:AccessDataSource ID="contactUsAccessDataSource" runat="server" DataFile="~/App_Data/Moesc8.MDB"
        InsertCommand="INSERT INTO [tblNameAddress] ([FirstName], [LastName], [TimeStamp2], [Address1], [City], [State], [ZipCode], [Email], [WorkPhone], [Fax], [Comments]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        SelectCommand="SELECT * FROM [tblNameAddress]" >
         <InsertParameters>
            <asp:FormParameter Name="fname" FormField="firstNameTextBox" />
            <asp:FormParameter Name="lname" FormField="lastNameTextBox" />
            <asp:ControlParameter Name="time" Type="DateTime" ControlID="dateTimeLabel" />  
            <asp:FormParameter Name="address" FormField="addressTextBox" />
            <asp:FormParameter Name="city" FormField="cityTextBox" />
            <asp:FormParameter Name="state" FormField="stateDropDown" />
            <asp:FormParameter Name="zip" FormField="zipTextBox" />
            <asp:FormParameter Name="email" FormField="emailTextBox" />
            <asp:FormParameter Name="phone" FormField="phoneTextBox" />
            <asp:FormParameter Name="fax" FormField="faxTextBox" />
            <asp:FormParameter Name="comments" FormField="commentsTextBox" />
       
             
        </InsertParameters>
    </asp:AccessDataSource>

However, when I click on the submit button, I get the error message "System.Data.OleDb.OleDbException: Index or primary key cannot contain a Null value.".

This is the code for the method called when the submit button is clicked:


    protected void submitButton_Click(object sender, EventArgs e)
    {
       
        contactUsAccessDataSource.Insert();
         
    }

If anyone could help out I'd appreciate it.



0
Comment
Question by:FrankeButler
3 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 17959285
Which is your primary key? Are you passing value this field?
0
 

Author Comment

by:FrankeButler
ID: 17987438
I found the problem. I thought that ID (an autonumber field in the Access db) was my primary key, but it turned out that first name, last name, and time were my primary keys. I edited the database to make the ID my primary key and now my code works!
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18424090
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question