简体   繁体   中英

I can't add a row to my database

I want to add a row from my form to the database, this is the code I used for it :

private Connexion connexion = new Connexion();
private SqlDataAdapter daAuteur;
private byte[] imgData;

     class Connexion
        {
            public DataSet ds = new DataSet();
            public SqlConnection cnx = new SqlConnection("Data Source=MAC-BOOK-AIR;Initial Catalog=Gestion_bib;Integrated Security=True");

    }

    private void AjoutAuteur_Load(object sender, EventArgs e)
            {
                InitializeOpenFileDialog();
                daAuteur = new SqlDataAdapter("select * from Auteur", connexion.cnx);
                daAuteur.Fill(connexion.ds, "Auteur");
            }

     private void AjoutAuteur_Load(object sender, EventArgs e)
            {
                InitializeOpenFileDialog();
                daAuteur = new SqlDataAdapter("select * from Auteur", connexion.cnx);
                daAuteur.Fill(connexion.ds, "Auteur");
            }

      private void ajouterNewBook_Click(object sender, EventArgs e)
            {
                DataRow row = connexion.ds.Tables["Auteur"].NewRow();
                row[1] = String.IsNullOrEmpty(nomAuteurBox.Text) ?
                    DBNull.Value :
                    nomAuteurBox.Text as Object;
                row[2] = String.IsNullOrEmpty(prenomAuteurBox.Text) ?
                    DBNull.Value :
                    prenomAuteurBox.Text as Object;
                row[3] = String.IsNullOrEmpty(nomAuteurBox.Text) ?
                    DBNull.Value :
                    dateDeNaissanceAuteurBox.Text as Object;
                row[4] = String.IsNullOrEmpty(lieuDeNaissanceAuteurBox.Text) ?
                    DBNull.Value :
                    lieuDeNaissanceAuteurBox.Text as Object;
                row[5] = String.IsNullOrEmpty(nationaliteAuteurBox.Text) ?
                    DBNull.Value :
                    nationaliteAuteurBox.Text as Object;
                if (decesCheckBox.Checked)
                {
                    row[6] = true;
                    row[7] = String.IsNullOrEmpty(dateDecesAuteurBox.Text) ?
                    DBNull.Value :
                    dateDecesAuteurBox.Text as Object;
                    row[8] = String.IsNullOrEmpty(lieuDecesAuteurBox.Text) ?
                        DBNull.Value :
                        lieuDecesAuteurBox.Text as Object;
                }
                else
                {
                    row[6] = false;
                    row[7] = DBNull.Value;
                    row[8] = DBNull.Value;
                }
                row[9] = String.IsNullOrEmpty(periodeAuteurBox.Text) ?
                    DBNull.Value :
                    periodeAuteurBox.Text as Object;
                row[10] = String.IsNullOrEmpty(resumeAuteurBox.Text) ?
                   DBNull.Value :
                   resumeAuteurBox.Text as Object;
                row[11] = String.IsNullOrEmpty(lien1Box.Text) ?
                   DBNull.Value :
                   lien1Box.Text as Object;

                imgData = new ImageConverter().ConvertTo(auteurPhoto.Image, typeof(Byte[])) as Byte[];
                row[12] = imgData == null ?
                   DBNull.Value :
                   imgData as Object;
                connexion.ds.Tables["Auteur"].Rows.Add(row);

                SqlCommandBuilder cmb = new SqlCommandBuilder(daAuteur);
                daAuteur.Update(connexion.ds, "Auteur");
            }

but it gives me this error :

String or binary data would be truncated. The statement has been terminated

Where is the problem ?

You must also ensure that the size on field in database is conform your string parameter length

You can fix size of your row content by using SubString method

You have problem about transfering of image

imgData = new ImageConverter().ConvertTo(auteurPhoto.Image, typeof(Byte[])) as Byte[];
row[12] = imgData == null ? DBNull.Value : imgData; //Adjust here

This error means that one (or more) of the columns in the database are too small to contain the text that you are attempting to insert. You need to either ensure that the strings are not too long, or increase the length of the columns in the database.

Without more information, I can't pinpoint the problem any more.

As others have said, you are running into exactly what the error is stating: the data you are trying to enter is larger than that allowed by the column.

Take a look at this sqlfiddle

Notice that it works, but if you change the insert to have 6 characters, it will fail to build the schema for the same reason that you have stated.

That truncation error means that the data that you are trying to insert is too large to fit in the columns. Have you set limits on how much data can be added to your table columns?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM