Solved

update gridview with objectdatasource

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now