Mega Code Archive

 
Categories / ASP.Net Tutorial / ADO Net Database
 

Iterating Through A DataSet for Access database

<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.OleDb" %> <script runat="server">     void Page_Load(object sender, EventArgs e)     {         string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["AccessConnectString"]);         string CommandText = "SELECT AuthorName, AuthorCity, AuthorContact_Email, AuthorWebsite FROM Author";         OleDbConnection myConnection = new OleDbConnection(ConnectionString);         OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);         OleDbDataAdapter myAdapter = new OleDbDataAdapter();         myAdapter.SelectCommand = myCommand;         DataSet myDataSet = new DataSet();         try {           myConnection.Open();           myAdapter.Fill(myDataSet, "Author");         } catch (Exception ex) {           throw (ex);         } finally {           myConnection.Close();        }        for (int i=0; i<=myDataSet.Tables["Author"].Rows.Count-1; i++)        {           Author p = new Author();           p.Name = myDataSet.Tables["Author"].Rows[i]["AuthorName"].ToString();           p.City = myDataSet.Tables["Author"].Rows[i]["AuthorCity"].ToString();           p.Email = myDataSet.Tables["Author"].Rows[i]["AuthorContact_Email"].ToString();           p.Website = myDataSet.Tables["Author"].Rows[i]["AuthorWebsite"].ToString();           Label1.Text += p.ToString();        }     }     public class Author     {         public string Name;         public string City;         public string Email;         public string Website;         public Author()         {}         public string ToString()         {             string description = "";             description = "Name : " + this.Name + "<br />";             description += "City : " + this.City + "<br />";             description += "Contact : <a href=mailto:" + this.Email + ">" + this.Email + "</a><br/>";             description += "Homesite : <a href='" + this.Website + "'>" + this.Website + "</a><br/><br/>";             return description;         }     } </script> <html> <body>     <asp:Label id="Label1" runat="server"></asp:Label> </body> </html> File: Web.config <configuration>     <appSettings>       <add key="AccessConnectString"            value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Books.mdb;" />     </appSettings> </configuration>