Solved

Getting Error when Updating AccessDataSource via Gridview in asp.net

Posted on 2010-11-07
3
423 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

16 Experts available now in Live!

Get 1:1 Help Now