I have error while I'm trying to save winforms textboxes values into a connected MS access database

Mohamed Rezk 1 Reputation point
2021-10-11T20:14:44.457+00:00

I have created winform project and bound MS Access database to it, I have created dataGridView which its dataSource is from MS Acess database.
139632-image.png

I also have created 3 textboxes as to carry data inputs and created "add button" to save textboxes values into MS Access database and show these data in the dataGridView.

This is the "add button" code:

> private void button1_Click(object sender, EventArgs e)  
>     {  
>         string firstName = textBox1.Text;  
>         string secondName = textBox2.Text;  
>         int age =Convert.ToInt32(textBox3.Text);  
>         int ID = 0;  
>         ID += 1;  
>   
>         theDatabase1DataSet.Table1.Rows.Add(ID, firstName,   
>           secondName, age);  
>         textBox1.Text = "";  
>         textBox2.Text = "";  
>         textBox3.Text = "";  
>         MessageBox.Show("Inputs have been saved");  
>     }  
  
I have created winform project and bound MS Access database to it, I have created dataGridView which its dataSource is from MS Acess database.  
  
![enter image description here][2]  
  
I also have created 3 textboxes as to carry data inputs and created "add button" to save textboxes values into MS Access database and show these data in the dataGridView.  
  
This is the "add button" code:  
  
private void button1_Click(object sender, EventArgs e)  
    {  
        string firstName = textBox1.Text;  
        string secondName = textBox2.Text;  
        int age =Convert.ToInt32(textBox3.Text);  
        int ID = 0;  
        ID += 1;  
  
        theDatabase1DataSet.Table1.Rows.Add(ID, firstName,   
          secondName, age);  
        textBox1.Text = "";  
        textBox2.Text = "";  
        textBox3.Text = "";  
        MessageBox.Show("Inputs have been saved");  
    }  

When I enter text in the textboxes and click on "add button"

This error message appears " System.ArgumentException: 'Input string was not in a correct format.Couldn't store in age Column. Expected type is Int32.'"

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,248 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2021-10-11T21:58:29.45+00:00

    Seeing

    System.Data.ConstraintExccption: ‘Column 'ID' is constrained to be unique. Value T is already present.’

    Means you need to determine if a record exists with the value you are attempting to add with so check for it first like

    SELECT 1 FROM YOUR_TABLE_NAME WHERE ID = (the value to use and should be done with a parameter)

    Use ExecuteScalar() if it's null then insert the record else donot as this will be the same constraint violation you have now.

    0 comments No comments