?
Solved

Getting Error when Updating AccessDataSource via Gridview in asp.net

Posted on 2010-11-07
3
Medium Priority
?
430 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

771 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