Solved

Getting Error when Updating AccessDataSource via Gridview in asp.net

Posted on 2010-11-07
3
425 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
ID: 34082177
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
ID: 34083996
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
ID: 34084034
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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