I need some help to make a order form in C#. My development environment is:
Microsoft Visual Studio 2010 Ultimate
SQL Server Express Edition 2005
Programming Language C#
Sample Database = NorthWind (Tables Orders and OrderDetails)
I've create a form for order entry, which contain a textbox for OrderID, a combobox for Customer, DateTimePickers for OrderDate and ShippedDate and a DataGridView which contains cokumns OrderID (readonly), ProductID, UnitPrice & Quantity.
What I need is to save the data to SQL Server in respective table (Orders and OrderDetails) which my code can't seem to do. It shows an error that foreign key cannot be null... because OrderDetails table also needs OrderID which is foreign key, and I am unable to understand how can I get the OrderID, as it is auto-generated after data is inserted into database.
Please help me on this problem to save the data in database with this foreign key issue...
IF the orderid is AutoGenerated Column, afer insert get the value by calling MAX Function. Or use Stored Procedure Out Parameters (But this may completely change your code).
Look into these lines clearly
command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
**Why are using the above Line?** And you are not ppassing any value to this, so whats the use of passing Order ID?
command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
command.Parameters["@CustID"].Value = cmbCust.SelectedText;
command.Parameters["@OrdDt"].Value = dtOrdDt.Text;
sqlDataMaster.InsertCommand = command;
Thank you very much for your reply, @OrdID in Parameters added because I need some way to get the generated ID and then pass it to OrderDetails foreinKey OrderID...but i couldn't find any solution for this...
If there is a better way, can you please guide me how to do it? even with StoredProc or any other way...
Your help and guidance for a newbie is much appreciated...