Bonjour,

comme le titre l'indique je souhaiterais insérer des TextBox dans ma DataTable.
Voici mon code :

Code : 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
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess;
using Oracle.DataAccess.Types;
using Oracle.DataAccess.Client;
using System.Data;
using System.Collections;
 
public partial class AfficheTableauBD : System.Web.UI.Page
    {
 
        public int test;
        public TextBox TextBox1 = new TextBox();
        public TextBox TextBox2 = new TextBox();
        public TextBox TextBox3 = new TextBox();
        public TextBox TextBox4 = new TextBox();
        public TextBox TextBox5 = new TextBox();
 
        public void Page_Load(object sender, EventArgs e)
        {            
            string db1 = "Data Source=(DESCRIPTION=" + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XX)(PORT=1521)))" + "(CONNECT_DATA=(SERVICE_NAME= X)));" + "User Id=Y;Password=Z;";
 
            OracleConnection conn = new OracleConnection(db1);
            conn.Open();
 
            string sql = "select * from vat";//Permettra de déterminer les noms des colonnes
            OracleCommand cmd = new OracleCommand(sql, conn);
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
 
            // Permettra de déterminer le nombre de colonne de mon tableau
            string count = "select count(*) from user_tab_columns where table_name = 'VAT'";
            OracleCommand cmd2 = new OracleCommand(count, conn);
            cmd2.CommandType = CommandType.Text;
            OracleDataReader dr2 = cmd2.ExecuteReader();
 
            cmd2.ExecuteScalar();
            dr2.Read();
            test = dr2.GetValue(0);//J'ai une erreur ici : Impossible de convertir implicitement le type Object en int
 
 
            DataTable dt = new DataTable();
 
            for (int i = 0; i < test; i++)
            {
                DataColumn dcol = new DataColumn(dr.GetName(i), typeof(System.String));
                dt.Columns.Add(dcol);
            }
 
             dr.Read();
             DataRow drow = dt.NewRow();
 
             /*Ce que je souhaiterais faire "en gros"
            drow[dr.GetName(0)] = TextBox1;
            drow[dr.GetName(1)] = TextBox2;
            drow[dr.GetName(2)] = TextBox3;
            drow[dr.GetName(3)] = TextBox4;
            drow[dr.GetName(5)] = TextBox5;
            */
 
             dt.Rows.Add(drow);
 
            foreach (DataColumn col in dt.Columns)
            {
                //Declare the bound field and allocate memory for the bound field.
                BoundField bfield = new BoundField();
 
                //Initalize the DataField value.
                bfield.DataField = col.ColumnName;
 
                //Initialize the HeaderText field value.
                bfield.HeaderText = col.ColumnName;
                //Add the newly created bound field to the GridView.
                GrdDynamic.Columns.Add(bfield);
 
                GrdDynamic.DataSource = dt;
                GrdDynamic.DataBind();
            }
 
            conn.Close();
            conn.Dispose();
}
 
protected void Button1_Click(object sender, EventArgs e)
        {
            string db1 = "Data Source=(DESCRIPTION=" + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=COMPTA)(PORT=1521)))" + "(CONNECT_DATA=(SERVICE_NAME= CARL)));" + "User Id=S03;Password=kurk;";
 
            OracleConnection conn = new OracleConnection(db1);
 
            string insert = "insert into vat(cd_vat, txt_quotefield, vat_rate, row_id, txt_external_vat) values(:cd_vat, :txt_quotefield, :vat_rate, :row_id, 'TEST')";
            conn.Open();
 
            OracleCommand cmd = new OracleCommand(insert, conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add(":cd_vat", OracleDbType.Varchar2, 2).Value = TextBox1.Text;
            cmd.Parameters.Add(":txt_quotefield", OracleDbType.Varchar2, 60).Value = TextBox2.Text;
            cmd.Parameters.Add(":vat_rate", OracleDbType.Decimal).Value = TextBox3.Text;
            cmd.Parameters.Add(":row_id", OracleDbType.Decimal).Value = TextBox4.Text;
            cmd.Parameters.Add(":txt_external_vat", OracleDbType.Varchar2, 20).Value = TextBox5.Text;
 
            cmd.ExecuteNonQuery();
 
        }
Je souhaitrais donc savoir comment insérer les textbox dans les cases correspondant.

Mais aussi régler le problème du dr2.GetValue(0) à passer en variable.