Solved

update gridview with objectdatasource

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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