Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

update gridview with objectdatasource

Posted on 2011-03-23
2
Medium Priority
?
395 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 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