I'm developping an application using VB.Net and this is the main form of my application :
https://i.sstatic.net/a4KfQ.png
I want when I click on the button Ajouter to add all informations from controls into a table in my database.
this is the table structure :
Create table Etudiant (
num_insc varchar(20) primary key,
CIN varchar(8),
sexe varchar(1),
nom_prenom varchar(30),
date_naiss date,
adresse varchar(150),
Code_niveau varchar(5)
)
and this is the code I tried :
Dim ds As New DataSet
Dim row As DataRow
Dim builder As SqlCommandBuilder
Dim adapter As SqlDataAdapter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
code_niveau.Items.Add("BTS1")
code_niveau.Items.Add("BTS2")
adapter = New SqlDataAdapter("SELECT * FROM Etudiant", cn)
adapter.Fill(ds, "Etudiant")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
row = ds.Tables("Etudiant").NewRow
row(0) = num_insc.Text
row(1) = CIN.Text
If (sexe_f.Checked) Then
row(2) = sexe_f.Name
Else
row(2) = sexe_m.Name
End If
row(3) = nom_prenom.Text
row(4) = date_naiss.Value
row(5) = adresse.Text
row(6) = code_niveau.Text
ds.Tables("Etudiant").Rows.Add(row)
builder = New SqlCommandBuilder(adapter)
adapter.InsertCommand = builder.GetInsertCommand
adapter.Update(ds, "Etudiant")
End Sub
But when I click on the button Ajouter I get this error message :
String or binary data would be truncated.
The statement has been terminated
.