You would use a table value parameter in the stored proc to pass a virtual table of rows to insert
And use ado.nets support of a table value parameters
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/table-valued-parameters
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi
How to insert records using single stored procedure with Ado.Net in MVC c#
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; }
}
Thanks
You would use a table value parameter in the stored proc to pass a virtual table of rows to insert
And use ado.nets support of a table value parameters
https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/table-valued-parameters
Hi @Jagjit Saini ,
Using a single stored procedure means selecting, updating, inserting and deleting all SQL queries; we can put in one stored procedure and perform this CRUD operation by calling only this single stored procedure.
The steps is like this:
con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
SqlCommand cmd = new SqlCommand("Usp_InsertUpdateDelete_Customer", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@CustomerID", 0);
cmd.Parameters.AddWithValue("@Name", objcust.Name);
cmd.Parameters.AddWithValue("@Address", objcust.Address);
cmd.Parameters.AddWithValue("@Mobileno", objcust.Mobileno);
cmd.Parameters.AddWithValue("@Birthdate", objcust.Birthdate);
cmd.Parameters.AddWithValue("@EmailID", objcust.EmailID);
cmd.Parameters.AddWithValue("@Query", 1);
con.Open();
result = cmd.ExecuteScalar().ToString();
return result;
public ActionResult InsertCustomer(Customer objCustomer)
{
objCustomer.Birthdate = Convert.ToDateTime(objCustomer.Birthdate);
if (ModelState.IsValid)
{
DataAccessLayer objDB = new DataAccessLayer();
string result = objDB.InsertData(objCustomer);
//ViewData["result"] = result;
TempData["result1"] = result;
ModelState.Clear(); //return View();
return RedirectToAction("ShowAllCustomerDetails");
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
If you want to know how to create the stored procedure,you could refer to below article:
https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/create-a-stored-procedure?view=sql-server-ver15
Best regards,
Yijing Sun
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.