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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Garima BansalPosted Dec 14, 2022, 5:48 AM
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
Aravind GovindarajPosted Dec 13, 2022, 12:28 PM
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 BansalPosted Dec 13, 2022, 7:28 AM
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.
Sachin SinghPosted Dec 8, 2022, 6:37 PM
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 BansalPosted Dec 1, 2022, 10:35 AM
code snippet in below zip file
Garima BansalPosted Dec 1, 2022, 10:31 AM
Name/Department @Html.TextBoxFor(m => m.Name_Department)
@**@ } HomeController public class HomeController : Controller { static List