I have code, which returns me a row out of a database,
con = new System.Data.SqlClient.SqlConnection();
dsl = new DataSet();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\tbl.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
con.Open();
string sql = "SELECT * From tbl_fb";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da.Fill(dsl, "fb");
DataRow dRow = dsl.Tables["fb"].Rows[0];
ViewData["a"] = dRow.ItemArray.GetValue(1).ToString();
ViewData["b"] = "afagjma";
con.Close();
Is there a way, that I could use a loop to get all rows from table. The number of rows are unknown.
I know I can do it with a loop, by using the variable (i) instead of numbers. But then I would need to use the ViewData array, which is problem for me.
Example: ViewData[“a”][i];
You should set up a class that defines what data you’re returning from your database.
For instance:
And then you would create a
List<Customer>(), and set this as the Model for your MVC Page.For instance:
And load your data into your list:
And then, on your MVC Page, set the
<%@ Page %>header to have the Inherits attribute like so:Now finally you can bind directly to the Model within the page: