Bonjour,

je rencontre une érreur en essayons d'inserer des données via les TVP, j'ai décidé de faire analyser mon code par le forum :
J'utilise VS2010 et SQL serveur 2008.

--Voici le code de mon TVP :
Code SQL : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
CREATE TYPE [dbo].[UT_Produit] AS TABLE(
	[Pro_design] [varchar](5) NULL,
	[Pro_Qte] [int] NULL,
	[Pro_PUA] [numeric](18, 0) NULL,
	[Pro_PUV] [numeric](18, 0) NULL
)
GO
--Et celui de la SP
Code SQL : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
create procedure [dbo].[Bon_Reception_Insert]
	@dep_design varchar(50),
	@rec_ref varchar(5),
	@rec_date varchar(10),
	@Rec_UserName Varchar(256),
	@ApplicationName Varchar(256)='/StockService',
	@lstPrd dbo.UT_Produit Readonly
as
begin
--Mon code ici
end

Le code C# utilisé est :

Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
 
namespace TVP
{
    public class Program
    {
        static void Main(string[] args)
        {
            List<DetailBonReception> dbr = new List<DetailBonReception>();
            dbr.Add(new DetailBonReception
            {
                Pro_Design = "Abattant",
                Pro_Qte = 100,
                Pro_PUA = 1020,
                Pro_PUV = 1400
            });
            dbr.Add(new DetailBonReception
            {
                Pro_Design = "Ampoule Economique",
                Pro_Qte = 100,
                Pro_PUA = 2000,
                Pro_PUV = 3500
            });
 
            int retval =new Program().Bon_Reception_Insert(new Bon_Reception
            {
                Dep_design = "Bopaka",
                Details = dbr,
                Rec_Ref = "AXX100",
                Rec_date = DateTime.Now,
                Rec_UserID = "laurent"
            });
 
            Console.ReadLine();
        }
 
        public static void CreateParametersWith_DataTable<T>(SqlCommand cmd, List<T> table)
        {
            DataTable dtProduits = new DataTable("UT_Produit");
 
            dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_design", DataType = typeof(string) });
            dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_Qte", DataType = typeof(int) });
            dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_PUA", DataType = typeof(decimal) });
            dtProduits.Columns.Add(new DataColumn { AllowDBNull = true, ColumnName = "Pro_PUV", DataType = typeof(decimal) });
 
            if (table is List<T>)
            {
 
                var t = table as List<DetailBonReception>;
                foreach (DetailBonReception dbr in t)
                {
 
                    dtProduits.Rows.Add(dbr);
                }
            }
 
            SqlParameter param = cmd.Parameters.AddWithValue("@lstPrd", dtProduits);
            param.SqlDbType = SqlDbType.Structured;
            param.TypeName = "dbo.UT_Produit";
        }
 
        int Bon_Reception_Insert(Bon_Reception br)
        {
            using (SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("Bon_Reception_Insert", db))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter prmDep_Design = new SqlParameter("@dep_design", SqlDbType.VarChar, 50);
                    prmDep_Design.Value = br.Dep_design;
                    cmd.Parameters.Add(prmDep_Design);
 
                    //Reference de la reception
                    SqlParameter prmRec_Ref = new SqlParameter("@rec_ref", SqlDbType.VarChar, 5);
                    prmRec_Ref.Value = br.Rec_Ref;
                    cmd.Parameters.Add(prmRec_Ref);
                    //Date de réception
                    SqlParameter prmRec_Date = new SqlParameter("@rec_date", SqlDbType.VarChar, 10);
                    prmRec_Date.Value = br.Rec_date.ToShortDateString();
                    cmd.Parameters.Add(prmRec_Date);
                    //Reference de la reception
                    SqlParameter prmRec_UserID = new SqlParameter("@Rec_UserName", SqlDbType.VarChar, 256);
                    prmRec_UserID.Value = br.Rec_UserID;
                    cmd.Parameters.Add(prmRec_UserID);
 
                    CreateParametersWith_DataTable<DetailBonReception>(cmd, br.Details);
 
                    try
                    {
                        db.Open();
                        int retval = cmd.ExecuteNonQuery();
                        db.Close();
                        return retval;
                    }
                    catch
                    {
                        return -1;
                    }
                    finally
                    {
                        db.Close();
                        cmd.Parameters.Clear();
                    }
                }
            }
        }
 
    }
 
    public partial class Bon_Reception
    {
        public long Rec_id { get; set; }
        public string Rec_Ref { get; set; }
        public DateTime Rec_date { get; set; }
        public string Rec_UserID { get; set; }
        public string Dep_design { get; set; }
 
    }
 
    public partial class Bon_Reception
    {
        public List<DetailBonReception> Details { get; set; }
    }
 
    public class DetailBonReception
    {
        public string Pro_Design { get; set; }
        public int Pro_Qte { get; set; }
        public decimal Pro_PUA { get; set; }
        public decimal Pro_PUV { get; set; }
    }
 
}

Aucune trace via le profiler et un message d'erreur s'affiche :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
System.Data.SqlClient.SqlException was caught
  Message=Les données de chaîne ou binaires seront tronquées.
Les données du paramètre table "@lstPrd" ne sont pas conformes au type de table du paramètre.
L'instruction a été arrêtée.
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  Class=16
  LineNumber=0
  Number=8152
Vous idées sont les bienvenues