i am trying to insert record in database using asp.net mvc.below is the code snippet
Loading
i am trying to insert record in database using asp.net mvc.below is the code snippet
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Garima BansalRank #1,555 · 1K reputationPosted Dec 1, 2022, 5:31 AM EST
Name/Department @Html.TextBoxFor(m => m.Name_Department)
@**@ } HomeController public class HomeController : Controller { static List
Garima BansalRank #1,555 · 1K reputationPosted Dec 1, 2022, 5:35 AM EST
code snippet in below zip file
Sachin SinghRank #66 · 26.2K reputationPosted Dec 8, 2022, 1:37 PM EST
You need to save recrord to database in each tab. when user click on finalize/forward after completing the form in first tab data should be saved in DB.
If you want to save data in one go then you can store data of each tab in an array of object. and then can send that array at final stage to the DB.
Garima BansalRank #1,555 · 1K reputationPosted Dec 13, 2022, 2:28 AM EST
Sachin Singh
save record to database in each tab, each tab have its own form.
I am not doing save racord of each tab in one go/click.
Aravind GovindarajRank #430 · 5.8K reputationPosted Dec 13, 2022, 7:28 AM EST
Hi Garima,
please find the couple of approaches for your use case
1. Create a save and submit button. The submit button is the one which actually saves all the records in the main table until each tab record will save in the temp table based on the user id or any sort of unique key.
2. Create a global object, when you traverse between forms, fill in all those details in a single object and whenever you click on save, it will upsert the record ( insert or update).
Let me know if you need any more info
Garima BansalRank #1,555 · 1K reputationPosted Dec 14, 2022, 12:48 AM EST
Hello Aravind Govindaraj
right now i just save record in one table i.e BasicDetails table
Sql Table
TABLE [dbo].[BasicDetails](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name_Department] [varchar](100) NULL,
CONSTRAINT [PK_BasicDetails] PRIMARY KEY CLUSTERED
Index.cshtml
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
Name/Department
}
Content 2
Content 3
HomeController
public ActionResult Index( BasicDetail basicDetail)
{
string constr ="Data Source=DESKTOP-U788ISC\\SQLEXPRESS;Initial Catalog=PDA;Persist Security Info=True;User ID=sa;Password=sa123;MultipleActiveResultSets=True";
using (SqlConnection con = new SqlConnection(constr))
{
string query = "INSERT INTO BasicDetails(Name_Department) VALUES(@Name_Department)";
query += " SELECT SCOPE_IDENTITY()";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Name_Department", basicDetail.Name_Department);
con.Close();
return View();
}
}
This way i am trying to save record, but doesnt work