Solved

update gridview with objectdatasource

Posted on 2011-03-23
2
386 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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