We help IT Professionals succeed at work.
Get Started

C# inserting GUID in MSSQL from one table to another

l3m0n asked
Last Modified: 2012-08-14
Hello, I need help with inserting GUID from one table to another in C# using MSSQL database, below is method that I wanted to use:

private void ImportInvoicesToDb()
            for (int i = 0; i < Converter.InvoicesList.Count; i++)
                SqlCommand Command = new SqlCommand();
                Command.Connection = DbConnection.Connection;

                Command.CommandText = @"INSERT INTO invoices(IdSeller)
                                        SELECT Id
                                        FROM sellers
                                        INSERT INTO invoices(IdBuyer)
                                        SELECT Id
                                        FROM buyers";


                Command.CommandText = @"INSERT INTO invoices(Date, Currency) VALUES ('"
                    + (Converter.InvoicesList[i] as InvoiceData).InvoiceMonth + "','"
                    + (Converter.InvoicesList[i] as InvoiceData).Currency + "')";

                int checkInsert = Command.ExecuteNonQuery();


Open in new window

It says while debugging:

Cannot insert the value NULL into column 'IdBuyer', table 'Malendowicz--Invoices.dbo.invoices'; column does not allow nulls. INSERT fails.
Cannot insert the value NULL into column 'IdSeller', table 'Malendowicz--Invoices.dbo.invoices'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The statement has been terminated.
Watch Question
Expert of the Quarter 2010
Expert of the Year 2010
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE