Solved

Getting Error when Updating AccessDataSource via Gridview in asp.net

Posted on 2010-11-07
3
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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