By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

Can I transmit data from Database to List<>?

meomap0z1
P: 8
Firstly, I connect Database and I wanna transmit data to my public List<Product> GetProductList. Can I do this?
Expand|Select|Wrap|Line Numbers
  1. public DataTable GetBooks()
  2.         {
  3.             string SQL = "Select * From Products";
  4.             SqlConnection cnn = new SqlConnection(strConnection);
  5.             SqlCommand cmd = new SqlCommand(SQL, cnn);
  6.             SqlDataAdapter da = new SqlDataAdapter(cmd);
  7.             DataTable dtProduct = new DataTable();
  8.             try
  9.             {
  10.                 if (cnn.State == ConnectionState.Closed)
  11.                 {
  12.                     cnn.Open();
  13.                 }
  14.                 da.Fill(dtProduct);
  15.             }
  16.             catch (SqlException se)
  17.             {
  18.                 throw new Exception(se.Message);
  19.             }
  20.             finally
  21.             {
  22.                 cnn.Close();
  23.  
  24.             }
  25.             return dtProduct;
  26.         }
  27.  
Have a nice day, guys.
2 Weeks Ago #1
Share this Question
Share on Google+
1 Reply


P: 36
Hi meomap0z1,

Yes,you can pass data from database to List.

Firstly create a list and fetch data from row of dataTable and store it through Add command which store data in List.

Add Following code to your program.

Expand|Select|Wrap|Line Numbers
  1. List<Product> GetProductList =new List<Product>();
  2.     foreach (GetProductList  p in dtProduct.Rows)
  3.     {
  4.             GetProductList .Add(p);
  5.     }
and you can also use
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.