ASP.NET Create HTML page on the fly based on dB table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnarewec
    New Member
    • Nov 2007
    • 22

    ASP.NET Create HTML page on the fly based on dB table

    Hi all
    I am newbie to ASP.NET am facing this problem.

    I search the web but I can't seem to find an example of code where I can generate an html file on the fly.

    I am reading through a table tblData and perform some calculations, if the calculations fails then I insert the record to a seperate table tblErrors.

    At the end of the caculation, I want to read the tblErrors and output its contents [fields] to the HTML page for the users to see.

    I would appreciate if somebody provide me a step by step on how to get achieve this. I am doing some reading by maybe I am not understanding them properyl.

    Please assist.

    Thanks so much
    Last edited by jhardman; Feb 23 '08, 04:31 AM. Reason: moved to .Net forum. ASP forum is for "classic" ASP
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Hi.
    I recently started learning ASP.NET as well.

    You are correct, there is no more "response.write " type of output with .NET. Everything with ASP.NET is about objects\control s.

    The good news is that if you can get the data you want to display through an SQL query you can display it as you describe in the datagrid control fairly easily.

    Assuming you have your database connection up and your sql executing on a page load, or button click event, you can do something like this.

    Code:
    	'PLACE THIS CODE IN A SUB PAGE_LOAD, OR BUTTON CLICK
    	dbconn.Open()
    
    
    	sql="SELECT * FROM TblErrors"
    	dbcomm=New OleDbCommand(sql,dbconn)
    	dbread=dbcomm.ExecuteReader()
    	MyErrorGrid.DataSource=dbread
    	MyErrorGrid.DataBind()
        
    	dbread.close
        dbconn.close
        
        'PLACE THIS CODE IN YOUR HTML DESIGN
    <asp:DataGrid id="MyErrorGrid" runat="server"
        AutoGenerateColumns="False" CellPadding="3"
        HeaderStyle-BackColor="#A4A6CF"
        HeaderStyle-ForeColor="000033"
        HeaderStyle-HorizontalAlign="Left"
        HeaderStyle-Font-Bold="True"
        BackColor="#eeeeee" Width="50%"
        HorizontalAlign="Left"
        Font-Bold="True" Font-Name="Tahoma"
        Font-Size="10pt">
    	
        <AlternatingItemStyle BackColor="White" />
      
        <Columns>
        	<asp:BoundColumn HeaderText="Error Data1" DataField="data_field1" />
            <asp:BoundColumn HeaderText="Error Data2" DataField="data_field2" />                       
        </Columns>        
    </asp:DataGrid>

    Comment

    • jeffstl
      Recognized Expert Contributor
      • Feb 2008
      • 432

      #3
      Guess I should point out too, that before the

      dbconn.open you should have something that sets up your connection string..I use access currently so it looks like this. Where default_DB_Path was setup previously as my path.

      Also know that there are other neat ways to use connection strings in .NET such as in the web.config file.

      Code:
      dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & default_DB_Path & ";")

      Comment

      • mnarewec
        New Member
        • Nov 2007
        • 22

        #4
        Thanks jeffstl
        Much appreciated. It works and truly helps

        Comment

        • pozze
          New Member
          • Feb 2008
          • 8

          #5
          Is it hard to modify this exmple to retrieve images sucessfuly stored in the db and display them in the datagrid as well?

          Comment

          • jeffstl
            Recognized Expert Contributor
            • Feb 2008
            • 432

            #6
            Unless you are storing the full path I think what you would want to do is load the path and the value from the database into a asp.net literal control once you have the data.

            Where DBReader is a set of data from a read.
            Like this:

            Code:
            'place in code behind (sub_load, etc)
            myimage.Text = "<img src=../databases/images/" & DBReader("imagecolumn") & " height=80 width=80 align=center>"
            
            
            'place in HTML design
            <asp:Literal id="myimage" runat="server" />

            Comment

            • pozze
              New Member
              • Feb 2008
              • 8

              #7
              thanks for the reply, but i have figured it out myself :)

              Originally posted by jeffstl
              Unless you are storing the full path I think what you would want to do is load the path and the value from the database into a asp.net literal control once you have the data.

              Where DBReader is a set of data from a read.
              Like this:

              Code:
              'place in code behind (sub_load, etc)
              myimage.Text = "<img src=../databases/images/" & DBReader("imagecolumn") & " height=80 width=80 align=center>"
              
              
              'place in HTML design
              <asp:Literal id="myimage" runat="server" />

              Comment

              Working...