etant debutant en Csharp j'ai testé une applik de l'aide de .Net
elle consiste a affiché ds une datagrid des donnes d'une DB sql et a pouvoir les modifiers et les sauvgarder.
il n'y a pas de prob lors de la compilation mais à l'éxecution.
quand je clic sur le button 2 (qui sert a la sauvegarde) j'obtient un msg :an unhandled exeception ........
j'ai bien chercher sur le forum et j'ai trouver un msg de GODZIDANE qui avait un prob +ou- similaire. Helas je bloque toujours.
le prob doit se trouver a la fin de mon code dans
**********************************************
private void button2_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Update(dataSet11);
MessageBox.Show("Database updated!");
}
**********************************************
voiçi tout mon code
**********************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace AdressOgraphe2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private AdressOgraphe2.DataSet1 dataSet11;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataSet11 = new AdressOgraphe2.DataSet1();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "TableAdressOgraphe", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("Head", "Head"),
new System.Data.Common.DataColumnMapping("Div/dpt", "Div/dpt"),
new System.Data.Common.DataColumnMapping("Title", "Title"),
new System.Data.Common.DataColumnMapping("Last Name", "Last Name"),
new System.Data.Common.DataColumnMapping("First Name", "First Name"),
new System.Data.Common.DataColumnMapping("Position", "Position"),
new System.Data.Common.DataColumnMapping("Street", "Street"),
new System.Data.Common.DataColumnMapping("PO Box", "PO Box"),
new System.Data.Common.DataColumnMapping("Zip Code", "Zip Code"),
new System.Data.Common.DataColumnMapping("City", "City"),
new System.Data.Common.DataColumnMapping("State", "State"),
new System.Data.Common.DataColumnMapping("Country", "Country"),
new System.Data.Common.DataColumnMapping("Language", "Language"),
new System.Data.Common.DataColumnMapping("tel office", "tel office"),
new System.Data.Common.DataColumnMapping("tel direct", "tel direct"),
new System.Data.Common.DataColumnMapping("tel home", "tel home"),
new System.Data.Common.DataColumnMapping("tel mobile", "tel mobile"),
new System.Data.Common.DataColumnMapping("E-mail", "E-mail"),
new System.Data.Common.DataColumnMapping("fax office", "fax office"),
new System.Data.Common.DataColumnMapping("fax direct", "fax direct"),
new System.Data.Common.DataColumnMapping("fax home", "fax home"),
new System.Data.Common.DataColumnMapping("fax mobile", "fax mobile"),
new System.Data.Common.DataColumnMapping("Type1", "Type1"),
new System.Data.Common.DataColumnMapping("Type2", "Type2"),
new System.Data.Common.DataColumnMapping("Type3", "Type3"),
new System.Data.Common.DataColumnMapping("Marchés1", "Marchés1"),
new System.Data.Common.DataColumnMapping("Marchés2", "Marchés2"),
new System.Data.Common.DataColumnMapping("Marchés3", "Marchés3"),
new System.Data.Common.DataColumnMapping("Marchés4", "Marchés4"),
new System.Data.Common.DataColumnMapping("Marchés5", "Marchés5"),
new System.Data.Common.DataColumnMapping("Produits1", "Produits1"),
new System.Data.Common.DataColumnMapping("Produits2", "Produits2"),
new System.Data.Common.DataColumnMapping("Produits3", "Produits3"),
new System.Data.Common.DataColumnMapping("Produits4", "Produits4"),
new System.Data.Common.DataColumnMapping("Produits5", "Produits5")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = @"SELECT Head, [Div/dpt], Title, [Last Name], [First Name], Position, Street, [PO Box], [Zip Code], City, State, Country, Language, [tel office], [tel direct], [tel home], [tel mobile], [E-mail], [fax office], [fax direct], [fax home], [fax mobile], Type1, Type2, Type3, Marchés1, Marchés2, Marchés3, Marchés4, Marchés5, Produits1, Produits2, Produits3, Produits4, Produits5 FROM TableAdressOgraphe";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = @"INSERT INTO TableAdressOgraphe(Head, [Div/dpt], Title, [Last Name], [First Name], Position, Street, [PO Box], [Zip Code], City, State, Country, Language, [tel office], [tel direct], [tel home], [tel mobile], [E-mail], [fax office], [fax direct], [fax home], [fax mobile], Type1, Type2, Type3, Marchés1, Marchés2, Marchés3, Marchés4, Marchés5, Produits1, Produits2, Produits3, Produits4, Produits5) VALUES (@Head, @Param1, @Title, @Param2, @Param3, @Position, @Street, @Param4, @Param5, @City, @State, @Country, @Language, @Param6, @Param7, @Param8, @Param9, @Param10, @Param11, @Param12, @Param13, @Param14, @Type1, @Type2, @Type3, @Marchés1, @Marchés2, @Marchés3, @Marchés4, @Marchés5, @Produits1, @Produits2, @Produits3, @Produits4, @Produits5); SELECT Head, [Div/dpt], Title, [Last Name], [First Name], Position, Street, [PO Box], [Zip Code], City, State, Country, Language, [tel office], [tel direct], [tel home], [tel mobile], [E-mail], [fax office], [fax direct], [fax home], [fax mobile], Type1, Type2, Type3, Marchés1, Marchés2, Marchés3, Marchés4, Marchés5, Produits1, Produits2, Produits3, Produits4, Produits5 FROM TableAdressOgraphe";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Head", System.Data.SqlDbType.VarChar, 50, "Head"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.VarChar, 50, "Div/dpt"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Title", System.Data.SqlDbType.VarChar, 50, "Title"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.VarChar, 50, "Last Name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 50, "First Name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Position", System.Data.SqlDbType.VarChar, 50, "Position"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Street", System.Data.SqlDbType.VarChar, 50, "Street"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.Int, 4, "PO Box"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param5", System.Data.SqlDbType.Int, 4, "Zip Code"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.VarChar, 50, "City"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@State", System.Data.SqlDbType.VarChar, 50, "State"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.VarChar, 50, "Country"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Language", System.Data.SqlDbType.VarChar, 50, "Language"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param6", System.Data.SqlDbType.Int, 4, "tel office"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param7", System.Data.SqlDbType.Int, 4, "tel direct"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param8", System.Data.SqlDbType.Int, 4, "tel home"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param9", System.Data.SqlDbType.Int, 4, "tel mobile"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param10", System.Data.SqlDbType.VarChar, 50, "E-mail"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param11", System.Data.SqlDbType.Int, 4, "fax office"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param12", System.Data.SqlDbType.Int, 4, "fax direct"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param13", System.Data.SqlDbType.Int, 4, "fax home"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param14", System.Data.SqlDbType.Int, 4, "fax mobile"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Type1", System.Data.SqlDbType.VarChar, 50, "Type1"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Type2", System.Data.SqlDbType.VarChar, 50, "Type2"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Type3", System.Data.SqlDbType.VarChar, 50, "Type3"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Marchés1", System.Data.SqlDbType.VarChar, 50, "Marchés1"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Marchés2", System.Data.SqlDbType.VarChar, 50, "Marchés2"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Marchés3", System.Data.SqlDbType.VarChar, 50, "Marchés3"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Marchés4", System.Data.SqlDbType.VarChar, 50, "Marchés4"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Marchés5", System.Data.SqlDbType.VarChar, 50, "Marchés5"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Produits1", System.Data.SqlDbType.VarChar, 50, "Produits1"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Produits2", System.Data.SqlDbType.VarChar, 50, "Produits2"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Produits3", System.Data.SqlDbType.VarChar, 50, "Produits3"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Produits4", System.Data.SqlDbType.VarChar, 50, "Produits4"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Produits5", System.Data.SqlDbType.VarChar, 50, "Produits5"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=DURNEZ;initial catalog=adressOgraphe;integrated security=SSPI;persist" +
" security info=False;user id=sa;workstation id=DURNEZ;packet size=4096";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("fr-BE");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
//
// dataGrid1
//
this.dataGrid1.DataMember = "TableAdressOgraphe";
this.dataGrid1.DataSource = this.dataSet11;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 40);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(264, 184);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "Load";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(104, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(152, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Save Changes in Database";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
dataSet11.Clear();
sqlDataAdapter1.Fill(dataSet11);
}
private void button2_Click(object sender, System.EventArgs e)
{
sqlDataAdapter1.Update(dataSet11);
MessageBox.Show("Database updated!");
}
}
}
********************************************
Merci d'avance pour votre aide
