?
Solved

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'au_id'

Posted on 2010-01-10
6
Medium Priority
?
2,414 Views
Last Modified: 2013-12-17
Hi guys,
I have a page that displays three datagrids with paging as per attached image of screen display. The problem comes when tying to page other datagrids such as the second.
get the following error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'au_id'. Line 61:         Control ctrl = (Control)sender;
Line 62:         IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
Line 63:         bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
Line 64:         switch (ItemType)
Line 65:         {
Source File: \App_Code\DynamicallyTemplatedGridViewHandler2.cs    Line: 63
au_id is a field name from the last datagrid, it is not paging correct datagrid.
The code I have is as below:
protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView2.PageIndex = e.NewPageIndex;
            GridView2.DataBind();
        }
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }
For clarity I have attached files as per code snippet:
App_Code DynamicallyTemplatedGridViewHandler2.cs
codebehind DefaultDisplayEdit.aspx.cs
code Page DefaultDisplayEdit.aspx
Thanks in advance, Ian.
//****************************************************
// App_Code DynamicallyTemplatedGridViewHandler2.cs
//**************************************************** 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Data.SqlClient;

public class DynamicallyTemplatedGridViewHandler2 : ITemplate
{
#region data memebers
    ListItemType ItemType;
    string FieldName;
    string InfoType;
   #endregion
    #region constructor
    public DynamicallyTemplatedGridViewHandler2(ListItemType item_type, string field_name, string info_type)
    {
        ItemType = item_type;
        FieldName = field_name;
        InfoType = info_type;
    }
    #endregion
    #region Methods
    public void InstantiateIn(System.Web.UI.Control Container)
    {
     //****************************************************
        switch (ItemType)
        {
            case ListItemType.Item:
                switch (InfoType)
                {
                    case "Command":

                        break;
                    default:
                        Label field_lbl = new Label();
                        field_lbl.ID = FieldName;
                        field_lbl.Text = String.Empty; //we will bind it later through 'OnDataBinding' event
                        field_lbl.DataBinding += new EventHandler(OnDataBinding);
                        Container.Controls.Add(field_lbl);
                        break;
                }
                break;
        }
    }
    //****************************************************
    #endregion
    #region Event Handlers
    private void OnDataBinding(object sender, EventArgs e)
    {
        object bound_value_obj = null;
        Control ctrl = (Control)sender;
        IDataItemContainer data_item_container = (IDataItemContainer)ctrl.NamingContainer;
        bound_value_obj = DataBinder.Eval(data_item_container.DataItem, FieldName);
        switch (ItemType)
        {
            case ListItemType.Item:
                Label field_ltrl = (Label)sender;
                field_ltrl.Text = bound_value_obj.ToString();
                break; 
        }
    }
    #endregion	
}
//****************************************************
// codebehind DefaultDisplayEdit.aspx.cs
//**************************************************** 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Linq;
using System.Xml.Linq;
using System.Data.Common;
using System.IO;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Web.SessionState;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.UI.MobileControls;

namespace Combine
{
    public partial class DefaultDisplayEdit : System.Web.UI.Page
    {
        public string odbName;
        public string dbTableName;
        public string sDBName = "AdminDB2";
        public string TempTable = "AdminTable";
        public string connectionString;
        public string MsgBoxDatabase = null;
        public string MsgBoxTable = null;
        public string MessageBox = null;
        public string MessageBoxProcedure = null;
        public bool bDatabaseExists = false;
        public bool bTableExists = false;
        public string strTable;
        public string strProcedure;
        public string server_tb;
        public string user_tb;
        public string pswrd_tb;
        public string database_tb;
        public string table_tb;
        public int k = 0;

        #region Data members
        public static DataTable Table = new DataTable();
        ArrayList ParameterArray = new ArrayList();
        #endregion
        #region Events Handlers
        protected void Page_PreLoad(object sender, EventArgs e)
        {
            HttpContext.Current.Session["tablePage"] = "0";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            
            HttpContext _Context = HttpContext.Current;
            LabelPage.Text = HttpContext.Current.Session["tablePage"].ToString();
            //****************************************************
            //connection = new System.Data.SqlClient.SqlConnection(Combine.conn.GetConnectionString(sDBName));
            HttpContext.Current.Session["connStringValue"] = Combine.conn.GetConnectionString2();
            //HttpContext.Current.Session["TableSelected"] = TempTable;
            //****************************************************
            SqlConnection Connection = new System.Data.SqlClient.SqlConnection(HttpContext.Current.Session["connStringValue"] + ";Initial Catalog=" + sDBName);
            Connection.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM " + TempTable, Connection);
            SqlDataReader reader = comm.ExecuteReader();
            grid.DataSource = reader;
            grid.DataBind();
            k = 1;
            CreateTemplatedGridView();
            // add templated fields to the GridView
            RoutineTemplatedField();
            k = 2;
            CreateTemplatedGridView();
            // add templated fields to the GridView
            RoutineTemplatedField1();
            k = 3;
            CreateTemplatedGridView();
            // add templated fields to the GridView
            RoutineTemplatedField2();
            //****************************************************
            //Label1.Text = "Label1:" + Session["connStringCatalog"].ToString();
            //Label2.Text = "Label2:" + Session["TableSelected"].ToString();
            //****************************************************
            if (Page.IsPostBack && (bool)Session["IsConnectionInfoSet"] == true)
            {

            }
            if (!Page.IsPostBack)
            {//msg_lbl.Text = "";
                HttpContext.Current.Session["Server"] = "";
                HttpContext.Current.Session["UserName"] = "";
                HttpContext.Current.Session["Password"] = "";
                HttpContext.Current.Session["DatabaseSelected"] = "";
                HttpContext.Current.Session["TableSelected"] = "";
                HttpContext.Current.Session["IsConnectionInfoSet"] = false;
                HttpContext.Current.Session["kvalue"] = 0;
            }
        }
        protected void grid_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int selectedRowIndex;
            selectedRowIndex = grid.SelectedIndex;
            GridViewRow row = grid.Rows[selectedRowIndex];
            k = selectedRowIndex;
            Session["kvalue"] = k;
            CreateTemplatedGridView();
            // add templated fields to the GridView
            RoutineTemplatedField();
        }
        //****************************************************
        public void TableGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }
        public void TableGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }
        protected void TableGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        { 
        }
        public void TableGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
        {
        }
        protected void TableGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            TableGridView.PageIndex = e.NewPageIndex;
            TableGridView.DataBind();
        }
        protected void TableGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        //****************************************************
        public void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }
        public void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
        public void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
        {
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        //****************************************************
        public void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
        }
        public void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
        }
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
        public void GridView2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
        {
        }
        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //CreateTemplatedGridView();
            GridView2.PageIndex = e.NewPageIndex;
            GridView2.DataBind();
        }
        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
        }
        //****************************************************
        #endregion

        #region Methods
        void PopulateDataTable()
        {
            Table = new DataTable();
            TableGridView.Columns.Clear();
            string ServerName = Session["Server"].ToString();
            string UserName = Session["UserName"].ToString();
            string Password = Session["Password"].ToString();
            string DatabaseName = Session["DatabaseSelected"].ToString();
            string TableName = Session["TableSelected"].ToString();
            SqlConnection Connection = new System.Data.SqlClient.SqlConnection("server=" + ServerName + ";uid=" + UserName + ";pwd=" + Password + ";Initial Catalog=" + DatabaseName + ";Asynchronous Processing=true");
            if (Connection.State == ConnectionState.Closed)
                Connection.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("Select * from " + TableName, Connection);
            try
            {
                adapter.Fill(Table);
            }
            catch (Exception ex)
            {
                msg_lbl.Text = ex.ToString();
                Connection.Close();
            }
        }
        void CreateTemplatedGridView()
        {
            GridViewRow row = grid.Rows[k];
            string name = row.Cells[2].Text;
            string nameT0 = row.Cells[3].Text;
            string connstr0 = "@" + row.Cells[2].Text;
            string[] str0 = connstr0.Split(';');
            server_tb = str0[0].Split('=')[1];
            user_tb = str0[1].Split('=')[1];
            pswrd_tb = str0[2].Split('=')[1];
            database_tb = str0[3].Split('=')[1];
            table_tb = nameT0;
            //****************************************************
            Session["Server"] = server_tb;
            Session["UserName"] = user_tb;
            Session["Password"] = pswrd_tb;
            Session["DatabaseSelected"] = database_tb;
            Session["TableSelected"] = table_tb;
            Session["IsConnectionInfoSet"] = true;
            Session["connStringCatalog"] = "server=" + server_tb + ";uid=" + user_tb + ";pwd=" + pswrd_tb + ";Initial Catalog=" + database_tb;
            // fill the table which is to bound to the GridView
            PopulateDataTable();
        }
        //****************************************************
        void RoutineTemplatedField()
        {
            // add templated fields to the GridView
            TemplateField BtnTmpField = new TemplateField();
            BtnTmpField.ItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Item, "...", "Command");
            BtnTmpField.HeaderTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Header, "...", "Command");
            BtnTmpField.EditItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem, "...", "Command");
            TableGridView.Columns.Add(BtnTmpField);
            for (int i = 0; i < Table.Columns.Count; i++)
            {
                TemplateField ItemTmpField = new TemplateField();
                // create HeaderTemplate
                ItemTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Header,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // create ItemTemplate
                ItemTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Item,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                //create EditItemTemplate
                ItemTmpField.EditItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // then add to the GridView
                TableGridView.Columns.Add(ItemTmpField);
            }
            // bind and display the data
            TableGridView.DataSource = Table;
            TableGridView.DataBind();
        }
        //****************************************************
        void RoutineTemplatedField1()
        {
            // add templated fields to the GridView
            TemplateField BtnTmpField = new TemplateField();
            BtnTmpField.ItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Item, "...", "Command");
            BtnTmpField.HeaderTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Header, "...", "Command");
            BtnTmpField.EditItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem, "...", "Command");
            GridView1.Columns.Add(BtnTmpField);
            for (int i = 0; i < Table.Columns.Count; i++)
            {
                TemplateField ItemTmpField = new TemplateField();
                // create HeaderTemplate
                ItemTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Header,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // create ItemTemplate
                ItemTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Item,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                //create EditItemTemplate
                ItemTmpField.EditItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // then add to the GridView
                GridView1.Columns.Add(ItemTmpField);
            }
            // bind and display the data
            GridView1.DataSource = Table;
            GridView1.DataBind();
        }
        //****************************************************
        void RoutineTemplatedField2()
        {
            // add templated fields to the GridView
            TemplateField BtnTmpField = new TemplateField();
            BtnTmpField.ItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Item, "...", "Command");
            BtnTmpField.HeaderTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.Header, "...", "Command");
            BtnTmpField.EditItemTemplate =
                new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem, "...", "Command");
            GridView2.Columns.Add(BtnTmpField);
            for (int i = 0; i < Table.Columns.Count; i++)
            {
                TemplateField ItemTmpField = new TemplateField();
                // create HeaderTemplate
                ItemTmpField.HeaderTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Header,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // create ItemTemplate
                ItemTmpField.ItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.Item,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                //create EditItemTemplate
                ItemTmpField.EditItemTemplate = new DynamicallyTemplatedGridViewHandler2(ListItemType.EditItem,
                                                              Table.Columns[i].ColumnName,
                                                              Table.Columns[i].DataType.Name);
                // then add to the GridView
                GridView1.Columns.Add(ItemTmpField);
            }
            // bind and display the data
            GridView2.DataSource = Table;
            GridView2.DataBind();
        }
        //****************************************************
        
        #endregion
        protected void GridView2_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }
}
}
<% 
    //****************************************************
    // code Page DefaultDisplayEdit.aspx
    //****************************************************  
%>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultDisplayEdit.aspx.cs" Inherits="Combine.DefaultDisplayEdit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>DefaultDisplay</title>
<link rel="stylesheet" href="outpost.css" />
<script type="text/javascript">
</script>
</head>
<body style="background-image: url(./WebFront.jpg); background-repeat:repeat-x">
<!--START PAGE SECTION-->
<!--  
--> 
<table id="table1" style="z-index:99; position:absolute; top:1px; left:1px; width:auto">
<tr>
<td>
<div style="width:650px; height:535px; font-size:smaller; overflow:auto;  border: 0px solid #000000" id="DIV1" onclick="return DIV1_onclick()">
<form id="form1" runat="server" method="post">

<table class="menu">
                        <tr>
                            <td>
                                <asp:HyperLink ID="lnkHome" Runat="server" NavigateUrl="~/Default.aspx" Text="Home" />
                            </td>
                            <td>
                                <asp:Menu ID="Menu1" Runat="server" Orientation="Horizontal" DataSourceID="srcSiteMap" />
                                <asp:SiteMapDataSource ID="srcSiteMap" Runat="server" ShowStartingNode="false" />
                            </td>
                            <td>
                            &nbsp;&nbsp;&nbsp;TablePage:<asp:Label ID="LabelPage" runat="server"></asp:Label>#
                            </td>
                        </tr>
                    </table>   
<div id="news" class="news">
<asp:GridView id="grid" runat="server" Font-Size="XX-Small"
 AutoGenerateColumns="true" OnSelectedIndexChanged="grid_SelectedIndexChanged"
 style="z-index: 100; left: 1px; position: absolute; top: 25px; font-size:smaller; visibility:hidden ">
    <Columns>
    <asp:ButtonField CommandName="Select" Text="Select" />
    </Columns>
  </asp:GridView>
</div>
<div id="tabs" class="news">
<asp:GridView ID="TableGridView"  
    OnRowEditing ="TableGridView_RowEditing" 
    OnRowCancelingEdit="TableGridView_RowCancelingEdit" 
    OnRowUpdating="TableGridView_RowUpdating"
    OnRowDeleting="TableGridView_RowDeleting"
    OnPageIndexChanging="TableGridView_PageIndexChanging"      
    runat="server"  AutoGenerateColumns="true" 
    style="z-index: 101; left: 1px; position: absolute; top: 25px; font-size:smaller" 
    AllowPaging="True" AllowSorting="True" >
</asp:GridView>
</div>
<div id="Div2" class="news">
<asp:GridView ID="GridView1"  
    OnRowEditing ="GridView1_RowEditing" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" 
    OnRowUpdating="GridView1_RowUpdating"
    OnRowDeleting="GridView1_RowDeleting"
    OnPageIndexChanging="GridView1_PageIndexChanging"      
    runat="server"  AutoGenerateColumns="true" 
    style="z-index: 102; left: 1px; position: absolute; top: 155px; font-size:smaller" 
    AllowPaging="True" AllowSorting="True" >
</asp:GridView>
</div>
<div id="Div3" class="news">
<asp:GridView ID="GridView2"  
    OnRowEditing ="GridView2_RowEditing" 
    OnRowCancelingEdit="GridView2_RowCancelingEdit" 
    OnRowUpdating="GridView2_RowUpdating"
    OnRowDeleting="GridView2_RowDeleting"
    OnPageIndexChanging="GridView2_PageIndexChanging"      
    runat="server"  AutoGenerateColumns="true" 
    style="z-index: 103; left: 1px; position: absolute; top: 425px; font-size:smaller" 
    AllowPaging="True" AllowSorting="True" >
</asp:GridView>
</div>
  <asp:Label ID="msg_lbl" runat="server" Text="" style="left: 6px; position: absolute; top: 453px" ></asp:Label>
</form>
</div>
</td>
</tr>
</table>
<!--END PAGE SECTION CODE-->
</body>
</html>

Open in new window

GridView025.jpg
0
Comment
Question by:homeshopper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
codingbeaver earned 2000 total points
ID: 26279518
1. According to your code, any control that generates postback will execute everything in Page_Load, which makes k = 3 all the time. Make sure this is the behavior you want.
2. In GridView's Paging event handler, you will need to specify its DataSource before you call DataBind. I don't see this property been set in your code.
0
 
LVL 2

Expert Comment

by:halceyon
ID: 26281521
why not add a:

if(!Page.IsPostback)

to your page load function to stop it from executing everything in page_load
0
 

Author Comment

by:homeshopper
ID: 26281948
Hi guys,
Thank you for your suggestions, what you both say is very helpfull.
I will not be able to get to look at my code till this evening.
I'll keep you posted how I get on. Thanks in advance for your help, Ian.
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 2

Expert Comment

by:halceyon
ID: 26281997
no worries, let us know how it goes.
0
 
LVL 10

Expert Comment

by:codingbeaver
ID: 26283426
@halceyon
He has a IsPostBack block in Page_Load. His code needs to be reorganized.
0
 
LVL 2

Expert Comment

by:halceyon
ID: 26283456
thanks, that's me busted skim reading ;)
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question