Tag Archive: ASP dot NET


Non-invocable member System.Data.DataSet.Tables cannot be used like a method


After filling in your dataset with your data adapter, you probably want to transfer that information to a DataTable so that you can display it on a your page’s datagrid, use it in an export to .csv function or something to that nature. However during compile time you may receive the following error:

Non-invocable member ‘System.Data.DataSet.Tables’ cannot be used like a method

One of the reasons you may be receiving this error is due to the way you have the DataTable name enclosed.  It should be in brackets instead of parenthesis.

Code Example:

DataTable dtEmployeeList;
return dtEmployeeList = dsEmpTable.Table("Employees");

Instead change the parenthesis to brackets:

DataTable dtEmployeeList;
return dtEmployeeList = dsEmpTable.Table["Employees"];

Since methods headers consist of a method name followed by parenthesis, the compiler will read the first dsEmpTable.Tabe(. . . as an attempt to invoke a method.  Once you change it to brackets it should take care of the problem.

Repeating items in an ASP.NET page using the repeater web control


This code here demonstrates the 4 things, populating an array.  Assigning the values of the array to a data source and binding the array.  Lastly displaying the results on a web page.  You can copy and paste the code to test it out.

<%@ Page Language="C#" %>

 

<!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)

    {

        ArrayList arList = new ArrayList();

        for(int k=0; k < 5; k++)

        {

            arList.Add("<br />Item " + k + " ");

        }

 

        arValList.DataSource = arList;

        arValList.DataBind();

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Repeating Items in ASP.NET</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Repeater ID="arValList" runat="server">

    <HeaderTemplate>

    <strong>This is a list of the array values.</strong>

    </HeaderTemplate>  

    <ItemTemplate>

    <%# Container.DataItem %>

    </ItemTemplate>

    </asp:Repeater>

   

    </div>

    </form>

</body>

</html>

 

 

How to bind values to a data list. Simple Example


The example below illustrates how to bind values to a data list.   You should be able to copy and paste the code and test it in any server.  

The concepts covered in this example include

  1. For loops
  2. Creating a data table to read values from
  3. Binding the data values from the data view
  4. Displaying the results in our data list
  5. Adding options to the data list to manipulate fonts and colors

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

 

<!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)

    {

        DataTable dtData = new DataTable();

        DataRow drRecords;

        dtData.Columns.Add(new DataColumn("rowValue", typeof(string)));

        for(int y=0; y < 10; y++)

        {

            drRecords = dtData.NewRow();

            drRecords[0] = "This is item " + y + "";

            dtData.Rows.Add(drRecords[0]);

        }

        DataView dvTableInfo = new DataView(dtData);

        dlFromArray.DataSource = dvTableInfo;

        dlFromArray.DataBind();

       

        

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>ASP.NET Lists</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:DataList ID="dlFromArray" runat="server"

         BorderColor="Violet" Font-Names="Arial Black" HeaderStyle-ForeColor="AliceBlue"

         HeaderStyle-BackColor="Black" AlternatingItemStyle-BackColor="Beige">

         <ItemTemplate>

         <%# DataBinder.Eval(Container.DataItem, "rowValue") %>

         </ItemTemplate>

    </asp:DataList>

    </div>

    </form>

</body>

</html>

 

 

Changing the text of the link when link is clicked


This example illustrates how to make a link change it’s text when you click on it.  This example converts the anchor tag (<a></a>) to an HTMLControl by placing the runat=”sever” value within the anchor tag.  Using the tag’s ID and some simple C# code between our script tags, we can change the text of the link when the link is clicked.

 

You can copy and paste this code to a text file and save it with a .aspx extension.  Be sure you have a server to test it on (i.e. web server or IIS on your local machine).

 

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

    void Page_Load(Object sender, EventArgs e)

    {

        linkTest.HRef = "#";  

        linkTest.InnerText = "Beginning Link Text.";

    }

 

    void linkTest_OnClick(object Source, EventArgs e)

    {

        linkTest.InnerText = "Link Text after you click on it.";

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Link Test</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        Link text to change on click. <br />

        <a id="linkTest" runat="server" onserverclick="linkTest_OnClick"></a>

    </div>

    </form>

</body>

</html>