locked
How to Store Data temporary Before insert database RRS feed

  • Question

  • User1827380138 posted

    Anyone Please share Code for Temporary Store Multiple records Data Before Insert database

    Saturday, December 14, 2019 5:55 AM

Answers

All replies

  • User1120430333 posted

    The article shows you how to use a datatable as a session object.

    https://www.brainbell.com/tutorials/ASP/Session_State_And_More_Complex_Data.html

    Saturday, December 14, 2019 1:04 PM
  • User475983607 posted

    Anyone Please share Code for Temporary Store Multiple records Data Before Insert database

    Can you be a bit more specific?  Every Web Application that displays records temporarily stores the records in the browser.  

    Are you building an MVC application? Web Forms? Razor Pages?  Can you share your code and explain why your code is not working as expected?

    Saturday, December 14, 2019 1:28 PM
  • User-719153870 posted

    Hi Dhoni,

    Dhoni

    Anyone Please share Code for Temporary Store Multiple records Data Before Insert database

    Assume you are talking about store multiple records temporary in c# and then insert them to sql server database.

    The common way you can achieve your requirement is to store your records in a datatable in session.

    Please refer to Save and retrive datatable from session variable using C# in ASP.Net.

    The code using a session datatable and ADO.NET is also easy to understand:

    private string Constr = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Country", typeof(string));
        dt.Rows.Add("Peter", "USA");
        dt.Rows.Add("John", "Franch");
        Session["Customers"] = dt;
    }
     
     
    protected void Insert(object sender, EventArgs e)
    {
        DataTable dtCustomers = Session["Customers"] as DataTable;
     
        SqlConnection con = new SqlConnection(Constr);
        con.Open();
        foreach (DataRow row in dtCustomers.Rows)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO Customers Values(@Name,@Country)", con);
            cmd.Parameters.AddWithValue("@Name", row["Name"]);
            cmd.Parameters.AddWithValue("@Country", row["Country"]);
            cmd.ExecuteNonQuery();
        }
     
        con.Close();
    }

    If i misunderstood anything or you met any problem, please feel free to tell.

    Best Regard,

    Yang Shen

    Monday, December 16, 2019 6:56 AM
  • User-1171043462 posted

    Store data in ViewState and then loop and save to database

    Store (Save) GridView data in ViewState in ASP.Net using C# and VB.Net

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 16, 2019 2:19 PM
  • User1827380138 posted

    webform

    Wednesday, December 18, 2019 3:20 PM
  • User1120430333 posted

    webform

    Of course, saving data in viewstate means you're  dragging the data around between the Web server on the server-side and the Web client on the client  browser-side on every round trip between the two, which will slowdown the responsiveness of the Web program in the user's eyes. Most developers will avoid viewstate, period no matter what. :)

    https://www.monitis.com/blog/improving-asp-net-performance-part-12-view-state-management/

    Wednesday, December 18, 2019 4:54 PM