Solved

Getting Error when Updating AccessDataSource via Gridview in asp.net

Posted on 2010-11-07
3
421 Views
Last Modified: 2012-05-10
I am working in ASP.Net - Visual Studio 2008 - VB Code

Have a Gridview bound to an AccessDataSource and have configured it to allow Updating

It works correctly and updates columns as long as all grid columns (except Primary Key) are set to ReadOnly=False
Setting one or more columns to ReadOnly=True gives me the error:
You tried to assign the Null value to a variable that is not a Variant data type

My guess is that the ReadOnly fields are trying to put Null values into database fields on update.

How do I get around this problem??


Lee
0
Comment
Question by:trims30
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
Comment Utility
You will need to adjust the UpdateCommand for your AccessDataSource to exclude the read-only fields.  This AccessDataSource has three columns ID, Field1 and Field2.  Field1 is read-only, so I have removed it and it's parameters from the Update and Insert commands.
This is the original AccessDataSource created by Visual Studio (the modified working one is below):
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Database1.mdb"
DeleteCommand="DELETE FROM [Table1] WHERE [ID] = ?"
InsertCommand="INSERT INTO [Table1] ([ID], [Field1], [Field2]) VALUES (?, ?, ?)"
SelectCommand="SELECT * FROM [Table1]"
UpdateCommand="UPDATE [Table1] SET [Field1] = ?,  [Field2] = ? WHERE [ID] = ?">
  <DeleteParameters>
    <asp:Parameter Name="ID" Type="Int32" />
  </DeleteParameters>
  <InsertParameters>
    <asp:Parameter Name="ID" Type="Int32" />
    <asp:Parameter Name="Field1" Type="String" />
    <asp:Parameter Name="Field2" Type="String" />
  </InsertParameters>
  <UpdateParameters>
    <asp:Parameter Name="Field1" Type="String" />
    <asp:Parameter Name="Field2" Type="String" />
    <asp:Parameter Name="ID" Type="Int32" />
  </UpdateParameters>
</asp:AccessDataSource>

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 

			DataFile="~/App_Data/Database1.mdb" DeleteCommand="DELETE FROM [Table1] WHERE [ID] = ?" 

			InsertCommand="INSERT INTO [Table1] ([ID],  [Field2]) VALUES (?, ?)" 

			SelectCommand="SELECT * FROM [Table1]" 

			UpdateCommand="UPDATE [Table1] SET  [Field2] = ? WHERE [ID] = ?">

			<DeleteParameters>

				<asp:Parameter Name="ID" Type="Int32" />

			</DeleteParameters>

			<InsertParameters>

				<asp:Parameter Name="ID" Type="Int32" />

				<asp:Parameter Name="Field2" Type="String" />

			</InsertParameters>

			<UpdateParameters>

				<asp:Parameter Name="Field2" Type="String" />

				<asp:Parameter Name="ID" Type="Int32" />

			</UpdateParameters>

		</asp:AccessDataSource>

Open in new window

0
 

Author Comment

by:trims30
Comment Utility
Great, THANK YOU, that worked for me but caused another problem in my project.

In the RowUpdating Event I'm doing a calculation referencing one of the readonly columns and am getting Zero for the readonly item.

In code below, "LastMeter" is ReadOnly and "MeterInput" and "UsageInput" are editable.

How do I get value of "LastMeter" during Update if it's ReadOnly??

Lee

 Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

        If e.NewValues("MeterInput") > e.NewValues("LastMeter") Then
            e.NewValues("UsageInput") = e.NewValues("MeterInput") - e.NewValues("LastMeter")
        End If

    End Sub
End Class
0
 

Author Comment

by:trims30
Comment Utility
OOPS - after looking at my code found I was using e.NewValues("LastMeter") rather than e.OldValues("LastMeter")  All is working now.

Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now