locked
How to handle database datas in List<t> RRS feed

  • Question

  • User1827380138 posted
    How to temporary maintain datas from sql database to in List<>;  and how to insert in another table
     
     
     ID Name Qty Price
     A-1 Apple 2 25 
     A-1 Orange  50 
    A-1 Mango 2 10
     
    Query = Select * from Table where ID= 'A-1' 
    this Query take 3 rows how to maintain that datas and how to transfer that data to database
     
    how to use List<> to maintain that table data 
    Thursday, December 26, 2019 11:53 AM

All replies

  • User475983607 posted

    If you have data in a List<T> then the data is temporarily stored.  If the data is in an HTML page (or server control) it is also temporarily stored in View State.

    Can you share your code and clearly explain the problem you are trying to solve?

    Thursday, December 26, 2019 1:55 PM
  • User288213138 posted

    Hi Dhoni,

    Dhoni

    How to temporary maintain datas from sql database to in List<>;  and how to insert in another table

    According to your description, do you want to sava the datas from database to list<T>, then insert this data to another table?

    If so, you can try below code. i get the datatable to Datatable, then convernt it to list<T>.

    protected void ToList()
            {
                string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
                string query = "Select * from Test1 where ID= 'A-1'";
                DataTable dt = new DataTable();
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
                    {
                        sda.Fill(dt);
                    }
                }
                List<Customers> customersList = new List<Customers>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Customers student = new Customers();
                    student.ID = dt.Rows[i]["ID"].ToString();
                    student.Name = dt.Rows[i]["Name"].ToString();
                    student.Qty = dt.Rows[i]["Qty"].ToString();
                    customersList.Add(student);
                }
            }
            public class Customers
            {
                public string ID { get; set; }
                public string Name { get; set; }
                public string Qty { get; set; }
                public string Price { get; set; }
            }
    public void insert()
            {
    
                DataTable dt = new DataTable();  //you can store the data into DataTable
                for (int i = 0; dt.Rows.Count > i; i++)
                {
                    string id = dt.Rows[i]["ID"].ToString();
                    string name = dt.Rows[i]["Name"].ToString();
                    string Qty = dt.Rows[i]["Qty"].ToString();
                    string Price = dt.Rows[i]["Price"].ToString();
    
                    string insert = "insert into Test2 values(@ID,@Name,@Qty,@Price)";
                    string constr = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        using (SqlCommand cmd = new SqlCommand(insert))
                        {
                            cmd.Parameters.AddWithValue("@ID", id);
                            cmd.Parameters.AddWithValue("@Name", name);
                            cmd.Parameters.AddWithValue("@Qty", Qty);
                            cmd.Parameters.AddWithValue("@Price", Price);
                            cmd.Connection = con;
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
    
                        }
                    }
    
                }
            }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Friday, December 27, 2019 7:33 AM