By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

No mapping exists from object type ... to a known managed provider native type.

P: 1
Please Help! I am a System Developer at a private company and I encounter this error while I was playing at my codes. I am using Visual Studio 2010 and my database is MSSQL 2008 R2. I keep on searching on the net for the answer but I failed. Please help me with this one and Thank you in advance.

The process is so simple that is only to populate the datagridview with data then save it to the database. Then on the process of saving the data, I encounter an error message “No mapping exists from object type SLI_Payroll.DataSetLibrary+DTContactsDataTable to a known managed provider native type.”

The data of the datagridview will be transfered to a dataset datatable. The dataset is not hard code and it was added as a New Item.

Sorry but i will put the whole code of the form.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class frmContacts
  3.     '------------------------------------------------------------------------------------
  4.     Private Function sp_Contacts(ByVal DT As DataTable)
  5.  
  6.         zResultReturnBoolean = Nothing
  7.  
  8.         Try
  9.  
  10.             zCommand = SetSQLCommand("sp_tabContacts")
  11.  
  12.             CON.Open()
  13.  
  14.             With zCommand.Parameters
  15.                 .AddWithValue("@DT", DT)
  16.             End With
  17.  
  18.             zCommand.CommandType = CommandType.StoredProcedure
  19.             If zCommand.ExecuteNonQuery() = 0 Then : zResultReturnBoolean = False : Else : zResultReturnBoolean = True : End If
  20.  
  21.             CON.Close()
  22.  
  23.         Catch ex As Exception
  24.             CON.Close()
  25.             MessageBox.Show(ex.Message, "SQL Error!")
  26.             zResultReturnBoolean = False
  27.         End Try
  28.  
  29.         Return zResultReturnBoolean
  30.  
  31.     End Function
  32.     '------------------------------------------------------------------------------------
  33.     Private Sub frmContacts_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  34.  
  35.         Call TempDataTable()
  36.  
  37.     End Sub
  38.     '------------------------------------------------------------------------------------
  39.     Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click, btnSave.Click
  40.  
  41.         Select Case sender.name
  42.             Case btnClose.Name
  43.                 Me.Close()
  44.             Case btnSave.Name
  45.                 Call SaveProcedure()
  46.         End Select
  47.  
  48.     End Sub
  49.     '------------------------------------------------------------------------------------
  50.     Private Sub SaveProcedure()
  51.  
  52.         Dim DTContacts As New DataSetLibrary.DTContactsDataTable
  53.  
  54.         For Each dgRow As DataGridViewRow In dgList.Rows
  55.  
  56.             '--Solution 1---
  57.             DTContacts.AddDTContactsRow(Val(dgRow.Cells(c_ID.Index).Value),
  58.                                         dgRow.Cells(c_name.Index).Value,
  59.                                         Format(CDate(dgRow.Cells(c_bday.Index).Value), "MM/dd/yyyy"),
  60.                                         dgRow.Cells(c_address.Index).Value,
  61.                                         dgRow.Cells(c_telNo.Index).Value)
  62.             '---------------
  63.  
  64.             ''--Solution 2---
  65.             'zTempDataTable.Rows.Add(Val(dgRow.Cells(c_ID.Index).Value),
  66.             '                            dgRow.Cells(c_name.Index).Value,
  67.             '                            Format(CDate(dgRow.Cells(c_bday.Index).Value), "MM/dd/yyyy"),
  68.             '                            dgRow.Cells(c_address.Index).Value,
  69.             '                            dgRow.Cells(c_telNo.Index).Value)
  70.             ''---------------
  71.         Next
  72.         '--solution 1
  73.         If sp_Contacts(DTContacts) Then
  74.             ''--solution 2
  75.             'If sp_Contacts(zTempDataTable) Then
  76.             MessageBox.Show("Successfully Saved!")
  77.         End If
  78.  
  79.     End Sub
  80.     '------------------------------------------------------------------------------------
  81.     Private Sub DisplayRecord()
  82.  
  83.         Dim DT As New DataTable
  84.         DT = GetDataTable("SELECT * FROM tabContacts A ORDER BY A.ID")
  85.  
  86.         dgList.Rows.Clear()
  87.         For Each dRow As DataRow In DT.Rows
  88.  
  89.             dgList.Rows.Add()
  90.             With dgList.Rows(dgList.RowCount - 1)
  91.  
  92.                 .Cells(c_ID.Index).Value = Val(dRow.Item("ID").ToString)
  93.                 .Cells(c_name.Index).Value = dRow.Item("name").ToString
  94.                 .Cells(c_bday.Index).Value = Format(CDate(dRow.Item("bday").ToString), "MM/dd/yyyy")
  95.                 .Cells(c_address.Index).Value = dRow.Item("address").ToString
  96.                 .Cells(c_telNo.Index).Value = dRow.Item("telno").ToString
  97.  
  98.             End With
  99.  
  100.         Next
  101.  
  102.     End Sub
  103.     '------------------------------------------------------------------------------------
  104.     Public Sub TempDataTable()
  105.  
  106.         zTempDataTable = New DataTable
  107.  
  108.         With zTempDataTable.Columns
  109.             .Add(New DataColumn("ID", GetType(Integer)))
  110.             .Add(New DataColumn("name", GetType(String)))
  111.             .Add(New DataColumn("bday", GetType(DateTime)))
  112.             .Add(New DataColumn("address", GetType(String)))
  113.             .Add(New DataColumn("telno", GetType(String)))
  114.         End With
  115.  
  116.     End Sub
  117.  
  118. End Class
  119.  
  120.  
  121.  
This is the code of my SQL

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE TABLE [dbo].[tabContacts](
  3.     [ID] [int] NOT NULL,
  4.     [name] [varchar](50) NULL,
  5.     [bday] [date] NULL,
  6.     [address] [varchar](50) NULL,
  7.     [telno] [nchar](10) NULL,
  8.  CONSTRAINT [PK_tabContacts] PRIMARY KEY CLUSTERED 
  9. (
  10.     [ID] ASC
  11. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. '------------------------------------------------------------------------------------
  14. CREATE PROCEDURE [dbo].[sp_tabContacts] 
  15.  
  16.     @DT dbo.type_tabContacts READONLY
  17. AS
  18. BEGIN
  19.  
  20.     SET NOCOUNT ON;
  21.  
  22.     MERGE INTO tabContacts as A
  23.         USING(SELECT ID,name,bday,address,telno
  24.                             FROM @DT) AS B
  25.  
  26.             ON A.ID = B.ID
  27.  
  28.             WHEN MATCHED THEN
  29.  
  30.                     UPDATE SET 
  31.                         A.name = B.name,
  32.                         A.bday = B.bday,
  33.                         A.address = B.address,
  34.                         A.telno = B.telno
  35.  
  36.             WHEN NOT MATCHED THEN
  37.  
  38.                     INSERT
  39.                     (
  40.                         ID,name,bday,address,telno
  41.                     )
  42.                     VALUES
  43.                     (
  44.                         B.ID,B.name,B.bday,B.address,B.telno
  45.                     );        
  46.  
  47. END
  48. '------------------------------------------------------------------------------------
  49. CREATE TYPE [dbo].[type_tabContacts] AS TABLE(
  50.     [ID] [int] NOT NULL,
  51.     [name] [varchar](50) NULL,
  52.     [bday] [date] NULL,
  53.     [address] [varchar](50) NULL,
  54.     [telno] [varchar](10) NULL,
  55.     PRIMARY KEY CLUSTERED 
  56. (
  57.     [ID] ASC
  58. )WITH (IGNORE_DUP_KEY = OFF)
  59. )
  60. GO
  61.  
  62.  
  63.  
But if you will remove the comment at solution 2 then comment solution 1. The saving will be successful.

Please help
Sep 2 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more Visual Basic .NET Questions on Bytes