Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Insert Data to Master/Detail table in Sqlserver 2005 from C# with Foreign key in Detail table

Insert Data to Master/Detail table in Sqlserver 2005 from C# with Foreign key in Detail table

Scheduled Pinned Locked Moved C#
csharpdatabasesql-servervisual-studiosysadmin
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahmed_one
    wrote on last edited by
    #1

    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 dataentry, which contain Textbox for OrderID, Combobox for Customer, DateTimePickers for OrderDate and ShippedDate and a DataGridView which contains Cokumns OrderID=ReadOnly, ProductID, UnitPrice & Quantity: In the form load event I've the following code:

    private void Inv2_Load(object sender, EventArgs e)
    {

        SetComb();
        connectionString = ConfigurationManager.AppSettings\["connectionString"\];
        sqlConnection = new SqlConnection(connectionString);
    
        qryOrd = "Select OrderID, CustomerID, OrderDate, ShippedDate from Orders";
        qryOrdDet = "Select OrderID, ProductID, UnitPrice, Quantity from OrderDetails";
    
        sqlConnection.Open();
        sqlDataMaster = new SqlDataAdapter(qryOrd, sqlConnection);
        sqlDataDet = new SqlDataAdapter(qryOrdDet, sqlConnection);
    
        //SET MASTER INSERT/UPDATES
        command = new SqlCommand("INSERT INTO Orders ( CustomerID, OrderDate, ShippedDate) VALUES (@CustID, @OrdDt, @ShipDt) SELECT SCOPE\_IDENTITY();");
    
        command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
        command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
        command.Parameters\["@CustID"\].Value = cmbCust.SelectedText;
    
        command.Parameters.Add("@OrdDt", SqlDbType.DateTime);
        command.Parameters\["@OrdDt"\].Value = dtOrdDt.Text;
    
        command.Parameters.Add("@ShipDt", SqlDbType.DateTime);
        command.Parameters\["@ShipDt"\].Value =dtShipDt.Text;
    
    
    
        sqlDataMaster.InsertCommand = command;
        //string id = command.ExecuteScalar().ToString();
    
        command = new SqlCommand("UPDATE Orders SET CustomerID = @CustID, OrderDate = @OrdDt, ShippedDate = @ShipDt WHERE OrderID = @OrdID");
    
        command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text; 
        command.Parameters.Add("@CustID", SqlDbType.VarChar, 15, "CustomerID").Value = cmbCust.Text;
        command.Parameters.Add("@OrdDt", SqlDbType.DateTime).Value = dtOrdDt.Text;
        command.Parameters.Add("@ShipDt", SqlDbType.DateTime).Value = dtShipDt.Text;
        sqlDataMaster.UpdateCommand = command;
    
    
        //SET DETAILS INSERT/UPDATES
        commandDet = new SqlCommand("INSERT INTO OrderDetails (ProductID, UnitPrice
    
    L 1 Reply Last reply
    0
    • A ahmed_one

      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 dataentry, which contain Textbox for OrderID, Combobox for Customer, DateTimePickers for OrderDate and ShippedDate and a DataGridView which contains Cokumns OrderID=ReadOnly, ProductID, UnitPrice & Quantity: In the form load event I've the following code:

      private void Inv2_Load(object sender, EventArgs e)
      {

          SetComb();
          connectionString = ConfigurationManager.AppSettings\["connectionString"\];
          sqlConnection = new SqlConnection(connectionString);
      
          qryOrd = "Select OrderID, CustomerID, OrderDate, ShippedDate from Orders";
          qryOrdDet = "Select OrderID, ProductID, UnitPrice, Quantity from OrderDetails";
      
          sqlConnection.Open();
          sqlDataMaster = new SqlDataAdapter(qryOrd, sqlConnection);
          sqlDataDet = new SqlDataAdapter(qryOrdDet, sqlConnection);
      
          //SET MASTER INSERT/UPDATES
          command = new SqlCommand("INSERT INTO Orders ( CustomerID, OrderDate, ShippedDate) VALUES (@CustID, @OrdDt, @ShipDt) SELECT SCOPE\_IDENTITY();");
      
          command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15);
          command.Parameters.Add("@CustID", SqlDbType.VarChar, 15);
          command.Parameters\["@CustID"\].Value = cmbCust.SelectedText;
      
          command.Parameters.Add("@OrdDt", SqlDbType.DateTime);
          command.Parameters\["@OrdDt"\].Value = dtOrdDt.Text;
      
          command.Parameters.Add("@ShipDt", SqlDbType.DateTime);
          command.Parameters\["@ShipDt"\].Value =dtShipDt.Text;
      
      
      
          sqlDataMaster.InsertCommand = command;
          //string id = command.ExecuteScalar().ToString();
      
          command = new SqlCommand("UPDATE Orders SET CustomerID = @CustID, OrderDate = @OrdDt, ShippedDate = @ShipDt WHERE OrderID = @OrdID");
      
          command.Parameters.Add("@OrdID", SqlDbType.NVarChar, 15, "OrderID").Value = txtOrdID.Text; 
          command.Parameters.Add("@CustID", SqlDbType.VarChar, 15, "CustomerID").Value = cmbCust.Text;
          command.Parameters.Add("@OrdDt", SqlDbType.DateTime).Value = dtOrdDt.Text;
          command.Parameters.Add("@ShipDt", SqlDbType.DateTime).Value = dtShipDt.Text;
          sqlDataMaster.UpdateCommand = command;
      
      
          //SET DETAILS INSERT/UPDATES
          commandDet = new SqlCommand("INSERT INTO OrderDetails (ProductID, UnitPrice
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ahmed_one wrote:

      and I am unable to understand how can I get the OrderID, as it is Auto generated after data is inserted in Database.

      You could select the last identity[^].

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      A 1 Reply Last reply
      0
      • L Lost User

        ahmed_one wrote:

        and I am unable to understand how can I get the OrderID, as it is Auto generated after data is inserted in Database.

        You could select the last identity[^].

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

        A Offline
        A Offline
        ahmed_one
        wrote on last edited by
        #3

        Thanks for reply, My problem is OrderID is not generated because of error that child table is not saved..the whole save procedure is failed because of this and compiler shows the error.

        L 1 Reply Last reply
        0
        • A ahmed_one

          Thanks for reply, My problem is OrderID is not generated because of error that child table is not saved..the whole save procedure is failed because of this and compiler shows the error.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          ahmed_one wrote:

          My problem is OrderID is not generated because of error that child table is not saved..

          Save the Order first, fetch the identity, save the child-records.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups