1

Here is the situation:

I have a webform that needs to read specific fields from a database that uses the eval function in the .aspx page. I know how to assign the values in the .aspx page, but in the code behind how do I "get" the values and bind them to the .aspx page? Can I just used a datareader? Dataset? What exactly am I going to be binding or reading to the dataset or reader? Do I need to create a method?

I already have the stored procedure created to pull the data from the database. I was going to create a method with the stored procedure. I just dont which kind to create...dataset, datareader? And how do I code it in the code behind?

2 Answers 2

0

Declare public properties for your DB fields:

public int MyFieldID = 0;
public string MyStringFromDB = string.empty;

// PAGE LOAD GET DB VALUES
{
    MyFieldID = Convert.ToInt32(db["ID"]);
    MyStringFromDB = Convert.ToString(db["FIELD"]);
}

Now you can either just assign them in the aspx page like <%=MyFieldID%> or use them in the code behind.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, great! So should I get the intial data from the database using a datareader and then assign values to the fields?
Yes, you can use a data reader, data table, or any other data source. I usually return my data sets as data tables. But it's your personal choice. Just remember, DataBinder is only for when you bind data to a control (DataGrid, as an example). I was a Classic ASP developer, and this was hard for me to grasp.
0

See Walkthrough: Displaying Data Using a Stored Procedure in the GridView Web Server Control

You can only use DataBinder.Eval within the context of a data bound control such as GridView, Repeater, DataGrid, etc. See DataBinder Class:

<%@ Page Language="C#" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void  Page_Load(object sender, EventArgs e)
{
        // Create and populate an ArrayList to store the products.
        ArrayList ProductList = new ArrayList();
        ProductList.Add(new Product("Standard", 99.95));
        ProductList.Add(new Product("Deluxe", 159.95));

        // Bind the array list to Repeater
        ListRepeater.DataSource = ProductList;
        ListRepeater.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
    <tr>
        <th style="width:50; text-align:left">Model</th>
        <th style="width:100; text-align:right">Unit Price</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
        <td>
            <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
        </td>
        <!-- Format the UnitPrice as currency. ({0:c}) -->
        <td style="text-align:right">
            <%#DataBinder.GetPropertyValue(Container.DataItem,
                         "UnitPrice", "{0:c}")%>
        </td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>

4 Comments

I am not displaying in a gridview. Im using the EVAL function in the .aspx page.
Example of the field I want to load on the .aspx page:<span class="ProductDetailItemHeading">Strength:</span><br /> <span class="ProductDetailItemText"><%# DataBinder.Eval(Container.DataItem, "Strength")%></span>
That will only work if you are using a GridView, Repeater, etc. with a datasource bound to it.
@user1049984: try with <%# DataBinder.Eval(Page, "Strength")%>..not sure about it..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.