-1

I'm making a Visual Studio project for adding and reading customers form a MS SQL server. The values I ad via Visual Studio cannot be seen.

Here is the SQL code:

create Table Person
(

Id int PRIMARY KEY,
FirstName varchar (15),
LastName varchar (15),
TaxNbr int,
BirthDate DATETIME,
AktivStatus varchar (15),
);

And here is the part of C# where I am adding new values for the customer:

private void Button_AddCustomer(object sender, RoutedEventArgs e)
    {


        Person customer = new Person();
        int number;
        customer.FirstName = FirstName.Text.Trim();
        customer.LastName = LastName.Text.Trim();
        if (int.TryParse(TaxNbr.Text, out number) == false) MessageBox.Show("Must add a valid tax number");
        if (number < 0 || number > 999) MessageBox.Show("Invalid Tax Number. Must be between 0 and 999");
        else customer.TaxNbr = number;

        customer.BirthDate = BirthDate.DisplayDate; 
        if (Aktiven.IsChecked == true) customer.AktivStatus = "Aktiven";
        if (Neaktiven.IsChecked == true)
        {
            customer.AktivStatus = "Neaktiven";
        }

        CustomerClass.AddCustomer(customer);
        DialogResult = true;

    }

I have connected it with SQL class:

public static class CustomerClass
{
    #region

    public static void AddCustomer(Person customer)
    {
        using (CustomerDataContext conn = new CustomerDataContext())
        {
            conn.Persons.InsertOnSubmit(customer);
            conn.SubmitChanges();

        }

    }
    #endregion
}

Please help, I am a beginner :)

DeanOC
  • 7,142
  • 6
  • 42
  • 56
  • Hey, what do you mean you can't see the values for the variables in Visual Studio? Also, what happens when you run your code and click the button? Does a new customer get added to the database? – WheretheresaWill Jan 27 '19 at 00:04
  • Yes he does, but I can see it only in the SQL table. – Kalinnnnna Jan 27 '19 at 00:12
  • When I try to add in Visual Studio I get this message: "System.Data.SqlClient.SqlException: 'Violation of PRIMARY KEY constraint 'PK__Person__3214EC070A523E0B'. Cannot insert duplicate key in object 'dbo.Person'. The duplicate key value is (0). The statement has been terminated.' " – Kalinnnnna Jan 27 '19 at 00:19
  • 1
    It looks like you are trying to add a customer with ID=0 over and over. – Slava Murygin Jan 27 '19 at 00:23
  • 2
    You should set your ID column to auto increment with IDENTITY – Brady Ward Jan 27 '19 at 00:37
  • Possible duplicate of [How To Create Table with Identity Column](https://stackoverflow.com/questions/10725705/how-to-create-table-with-identity-column) – mjwills Jan 27 '19 at 01:06
  • I added SET IDENTITY_INSERT Person on in SQL , but I now get this: " System.Data.SqlClient.SqlException: 'Cannot insert explicit value for identity column in table 'Person' when IDENTITY_INSERT is set to OFF.' " – – Kalinnnnna Jan 27 '19 at 10:39

1 Answers1

0

the problem is that each person u create will have an id of 0 , and since the ID column in the database is not marked as identity SQL server will attempt inserting the value of 0 with each person insertion , so the first time it will success and insert new person with id = 0 , but it will fail the second time because there is already a person with the id 0 you need to mark the pk as identity , this way SQL Server will handle the id value automatically and u won't even need to send it's value from ur C# code

or you can manage the ids by urself and make sure u don't inserted any duplicate values in this column which is not recommended

  • I added SET IDENTITY_INSERT Person on in SQL , but I now get this: " System.Data.SqlClient.SqlException: 'Cannot insert explicit value for identity column in table 'Person' when IDENTITY_INSERT is set to OFF.' " – Kalinnnnna Jan 27 '19 at 10:36
  • as i said , in case u set the ID column as identity SQL server will handle the insertion of this column and will create a sequence for u so that u can have ur ids in an ascending order like 1 ,2,3,4,5,6,etc the only thing u need to do is not sending the ID value from your C# code to the database – Mostafa Mohamed Ahmed Jan 27 '19 at 12:08