Ramco Ramco

Ramco Ramco

  • 471
  • 2.8k
  • 393.5k

Insert record using Ado.Net

Jul 5 2021 12:18 PM

Hi

  I have below 2 classes & want to insert record using Ado.Net

public class Invoice
    {
        public Invoice()
        {
        }

        [Key]
        public string No { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public string DocDate { get; set; }

        public virtual ICollection InvoiceDetails { get; set; }
        public string Status { get; set; }

    }

    public partial class InvoiceDetail
    {
        public string DocNo { get; set; }
        public Product Id { get; set; }
        public decimal Quantity { get; set; }
        public Nullable price { get; set; }
        public Nullable GrossAmount { get; set; }
        public Nullable NetAmount { get; set; }
    }

 

Trying like below but it is not showing Invoice Details fields.

public int Add(Invoice objInvoice)
        {
            int i;
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_Invoice", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@No", objInvoice.No);
                cmd.Parameters.AddWithValue("@No", objInvoice.DocDate);
                cmd.Parameters.AddWithValue("@No", objInvoice.Po_No);
                cmd.Parameters.AddWithValue("@No", objInvoice.PO_Date);
                cmd.Parameters.AddWithValue("@Action", "C");
                i = cmd.ExecuteNonQuery();
            }
            return i;
        }

Thanks


Answers (11)