Results 1 to 9 of 9

Thread: add data from a DB to a table

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271

    add data from a DB to a table

    hi

    I know I can add data from a database to a datagrid, but I want to take the data from the data base,

    and the depending on the value in the field (Rank) I want to add an image to the corrosponding row.

    I want the Rank Column to be invisible to...

    how do I do this?

    the values in rank will be from 1 - 5
    and images from 1.jpg - 5.jpg

    thanks
    §tudz

    Studzworld.com - Portfolio

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    The easiest way is to not use auto column creation, instead, add each column seperately, then bind each one to the data source column it matches with. Then, for the Rank column, you will create a Template column. In that column template, you will place a image control. For the href of the image, you will put a databinder.eval statement.

    If you still need help, I will set up an example.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thanks for that

    If you could make an example I'd be grateful.
    §tudz

    Studzworld.com - Portfolio

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Code:
    <asp:DataGrid id="DataGrid1" runat="server" Width="304px" AutoGenerateColumns="False">
    	<Columns>
    		<asp:TemplateColumn HeaderText="Rank Image">
    			<ItemTemplate>
    				<asp:Image id=Image1 runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Rank", "image{0}.jpg") %>'>
    				</asp:Image>
    			</ItemTemplate>
    		</asp:TemplateColumn>
    		<asp:BoundColumn DataField="Rank" HeaderText="Rank"></asp:BoundColumn>
    		<asp:BoundColumn DataField="Name" HeaderText="Item Name"></asp:BoundColumn>
    		<asp:BoundColumn DataField="Price" HeaderText="Price"></asp:BoundColumn>
    	</Columns>
    </asp:DataGrid>
    Basically I made a datagrid that has 4 columns. The first column is the template column that will hold your image. Notice the image control in the itemtemplate. Since I have a Rank column which holds the numbers, I took the Rank column and created a url for the image when the data is being bound. My data table I binded to has these columns:
    Rank, Name, Price

    Good luck.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thanks but I get this error:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property with the name Rank.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: DataBinder.Eval: 'System.Data.Common.DbDataRecord' does not contain a property with the name Rank.

    Source Error:


    Line 36: <asp:TemplateColumn HeaderText="Rank Image">
    Line 37: <ItemTemplate>
    Line 38: <asp:Image id="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Rank", "image{0}.jpg") %>'></asp:Image>
    Line 39: </ItemTemplate>
    Line 40: </asp:TemplateColumn>
    §tudz

    Studzworld.com - Portfolio

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    This line of code:
    Code:
    <asp:Image id="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Rank", "image{0}.jpg") %>'></asp:Image>
    is what does the work for you. You need to replace the word 'Rank' with the Column name that holds the rank value.

    Also, you will need to change the 'image{0}.jpg' to match up to the structure of your images. You can chang it to something like:
    "Images\{0}.jpg"

    The {0} just puts in the column that it is matching against. So if the column you are pulling the number from is called ItemRank in the datasource, and your images are named like 1.jpg, 2.jpg and they are in the Images folder of the site, you would set up something like this:
    Code:
    <asp:Image id="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ItemRank", "Images\{0}.jpg") %>'></asp:Image>

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thanks that works great, aftetr a bit of tweaking:

    Here is the code, but how do I make the coloumn Rank (the ones with the rank values in) invisible?

    Code:
    <%@ Page Language="VB" Debug="true" %>
    <%@ import Namespace="System.Data" %>
    <%@ import Namespace="System.Data.OleDb" %>
    <script runat="server">
    
        Sub Page_Load(Sender As Object, E As EventArgs)
        
        
                ' TODO: Update the ConnectionString and CommandText values for your application
                Dim  MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & _
                            server.mappath("puzzlepirates.mdb"))
                Dim CommandText As String = "Select Name, Title, Rank from alchemist_guild"
        
                Dim myCommand As New OleDBCommand(CommandText, myConnection)
        
                myConnection.Open()
        
                DGAlchemist.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
                DGAlchemist.DataBind()
        
        
        
        
        End Sub
    
    </script>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
        <title>The Patricians Web</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="tpw.css" type="text/css" rel="stylesheet" />
    </head>
    <body text="#666666" vlink="#333333" alink="#333333" link="#333333" bgcolor="#ffffff" leftmargin="5" topmargin="5" marginwidth="0" marginheight="0">
        <form runat="server">
            <p>
                <asp:DataGrid id="DGAlchemist" runat="server" BackColor="LemonChiffon" Width="301px" BorderStyle="None" HorizontalAlign="Left" BorderWidth="0px">
                    <Columns>
                        <asp:TemplateColumn HeaderText="Rank">
                            <ItemTemplate>
                                <asp:Image id="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Rank", "images\image{0}.gif") %>'></asp:Image>
                            </ItemTemplate>
                        </asp:TemplateColumn>
                    </Columns>
                </asp:DataGrid>
            </p>
        </form>
    </body>
    </html>
    §tudz

    Studzworld.com - Portfolio

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You are autogenerating columns. Not good if you want to customize. Take this exact datagrid code, and replace yours with it. It should work.

    Code:
                <asp:DataGrid id="DGAlchemist" runat="server" BackColor="LemonChiffon" Width="301px" BorderStyle="None" HorizontalAlign="Left" BorderWidth="0px" AutoGenerateColumns="False">
                    <Columns>
                        <asp:TemplateColumn HeaderText="Rank">
                            <ItemTemplate>
                                <asp:Image id="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Rank", "images\image{0}.gif") %>'></asp:Image>
                            </ItemTemplate>
                        </asp:TemplateColumn>
    	<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
    	<asp:BoundColumn DataField="Title" HeaderText="Title"></asp:BoundColumn>
                    </Columns>
                </asp:DataGrid>

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    UK
    Posts
    271
    thank you again,
    that helped.

    Thanks
    §tudz

    Studzworld.com - Portfolio

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width