Solved

update gridview with objectdatasource

Posted on 2011-03-23
2
389 Views
Last Modified: 2012-05-11
I have a gridview tied to objectdatasource. I cannot update the database table. This is my code:

 Public Sub UpdateEmail(ByVal EmployeeId As Integer, ByVal EmployeeName As String, ByVal EmailAddress As String, ByVal Active As Boolean)
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("BLS-MPOYNT_SQLClient").ConnectionString

        Dim con As New SqlConnection(connectionString)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "UPDATE EmployeeEmail " & _
           "SET " & _
           "EmployeeID = @EmployeeID,EmployeeName = @EmployeeName,EmailAddress =@EmailAddress,Active = @Active " & _
        "where EmployeeId = @EmployeeId"

        cmd.Parameters.AddWithValue("@EmployeeName", EmployeeName)
        cmd.Parameters.AddWithValue("@EmailAddress", EmailAddress)
        cmd.Parameters.AddWithValue("@Active", Active)
        cmd.Parameters.AddWithValue("@EmployeeID", EmployeeId)

        Using con

            con.Open()
            cmd.ExecuteNonQuery()

        End Using

<asp:GridView ID="GridView1"  DataKeyNames="EmployeeId" runat="server" AutoGenerateColumns="false" DataSourceID="ObjectDataSource1" AutoGenerateEditButton="true">
        <Columns>
        <asp:BoundField HeaderText="EmployeeId" DataField="EmployeeId"  />
          <asp:BoundField HeaderText="EmployeeName" DataField="EmployeeName" />
          <asp:BoundField HeaderText="EmailAddress" DataField="EmailAddress"  />
          <asp:BoundField HeaderText="Active" DataField="Active" />
        </Columns>
        </asp:GridView>
   
   
        <asp:ObjectDataSource ID="ObjectDataSource1"
            runat="server" SelectMethod="GetEmailAddresses" TypeName="Email" UpdateMethod="UpdateEmail">
            <UpdateParameters>
                <asp:Parameter Name="EmployeeId" Type="Int32" />
                <asp:Parameter Name="EmployeeName" Type="String" />
                <asp:Parameter Name="EmailAddress" Type="String" />
                <asp:Parameter Name="Active" Type="Boolean" />
            </UpdateParameters>
        </asp:ObjectDataSource>
When I click on Update I receive error msg:
System.Data.SqlClient.SqlException: Cannot update identity column 'EmployeeId'.

What am I doing wrong? Please, help.


0
Comment
Question by:msout
[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 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 35201750
EmployeeId is an Identity column so cannot be updated as it is auto-generated when the record is created. The database is just trying to maintain referential integrity.
Remove it from the set list :

cmd.CommandText = "UPDATE EmployeeEmail " & _
           "SET " & _
           "EmployeeName = @EmployeeName,EmailAddress =@EmailAddress,Active = @Active " & _
        "where EmployeeId = @EmployeeId"

If there are circumstances where you would want to change the employeeid then you will have to change the column definition so it is not an identity column.
0
 

Author Closing Comment

by:msout
ID: 35201906
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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