Welcome to Dream.In.Code
Become an Expert!

Join 149,575 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,694 people online right now. Registration is fast and FREE... Join Now!




GridView not finding data?

 
Reply to this topicStart new topic

GridView not finding data?, and once agian Im confused

hyperionza
9 Oct, 2007 - 02:30 AM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
The following code works... but it doesnt...

Ok what i mean is that im populating 2 GridViews in the exactly the same manner but one of them doesnt work...
QUOTE
A field or property with the name 'COMPANY_TYPE' was not found on the selected data source.
. But when i display the data source ... there Company_Type is staring right in my face, it does this for all the fields.

so heres the definition of the GridView
CODE
<asp:GridView ID="gv_Companies" runat="server" AutoGenerateColumns="False" CellPadding="4"
        ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="gv_Companies_SelectedIndexChanged">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:CommandField SelectText="View" ShowSelectButton="True" />
            <asp:BoundField DataField="COMPANY_TYPE" HeaderText="Company Type" />
            <asp:BoundField DataField="COMPANY_NAME" HeaderText="Company Name" />
            <asp:BoundField DataField="CONTACT_PERSON" HeaderText="Contact Person" />
            <asp:BoundField DataField="OFFICE_NUMBER" HeaderText="Office #" />
            <asp:BoundField DataField="PRIMARY_EMAIL" HeaderText="Email" />
            <asp:BoundField DataField="OFFICE_FAX" HeaderText="Fax #" />
        </Columns>
        <RowStyle BackColor="#EFF3FB" />
        <EditRowStyle BackColor="#2461BF" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>


the code to fetch the data, transform it into something the GridView can use and bind the XML datasource:
CODE
protected void Page_Load(object sender, EventArgs e)
    {
        ((MasterPg)Master).InitMP();
        ((MasterPg)Master).setActiveNavBarPanel();
        string message = "<Message><Company><OwnerGUID>";
        if (RadioButtonList1.SelectedValue.ToLower().Equals("own"))
            message += ((MasterPg)Master).UserGUID;
        else
            message += "*";
        message += "</OwnerGUID>" +
                "<CompanyGUID/>" +
                "<COMPANY_TYPE/>" +
                "<COMPANY_NAME/>" +
                "<CONTACT_PERSON/>" +
                "<OFFICE_NUMBER/>" +
                "<PRIMARY_EMAIL/>" +
                "<OFFICE_FAX/>" +
            "</Company></Message>";
        ((MasterPg)Master).PageData = ((MasterPg)Master).getService().Search(message);
       // ((MasterPg)Master).MessageBox(StaticOperations.XmlDataStringToXmlAttributeString(((MasterPg)Master).PageData));
        BindGridView();
        gv_Companies.Visible = true;
        StaticOperations.CreateDBEvent(((MasterPg)Master).UserGUID, "Personal Companies Page View");
    }

    private void BindGridView()
    {
        XmlDataSource XDS = new XmlDataSource();
        XDS.Data = StaticOperations.XmlDataStringToXmlAttributeString(((MasterPg)Master).PageData);
        gv_Companies.DataSource = XDS;
        gv_Companies.DataBind();
    }


where PageData is just a string residing on the MasterPage.

the XML returned from getService().Search(string) is exactly as sent just with data in all the fields.

oh and
CODE
public static string XmlDataStringToXmlAttributeString(string XmlString)
    {
        string tempa = "<Message>";
        XmlDocument XDoc = new XmlDocument();
        XDoc.InnerXml = XmlString;
        if (!XDoc.HasChildNodes)
        {
            throw new Exception("Malformed Xml String");
        }
        XmlElement XRootElem = XDoc.DocumentElement;
        XmlNodeList XBaseNodeList = XRootElem.ChildNodes;
        foreach (XmlNode XBaseNode in XBaseNodeList)
        {
            tempa += "<"+XBaseNode.Name;
            foreach (XmlNode XDataNode in XBaseNode.ChildNodes)
            {
                tempa += " " + XDataNode.Name + "=\"" + XDataNode.InnerText + "\"";
            }
            tempa += "/>";
        }
        tempa += "</Message>";
        return tempa;
    }


the other operation that it uses to transform the XML.

Any one got ideas?

one last thing, in XML, does it matter what order the attributes are in? thats about the only thing i can think of that could possibly be going wrong because <Company_Type/> is the third attribute in the XML but the first required field in the GridView.

This post has been edited by hyperionza: 9 Oct, 2007 - 02:42 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: GridView Not Finding Data?
9 Oct, 2007 - 04:36 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
My first question is where is this data coming from? Is it coming from a database, I see the use of a CreateDBEvent method but no definition for it. If the data is indeed coming from a database why are you converting it to XML?

Second, I see where you're building the <Message> tag and values, but not where you're building the <Company> tag and values.
User is offlineProfile CardPM
+Quote Post

hyperionza
RE: GridView Not Finding Data?
9 Oct, 2007 - 11:42 AM
Post #3

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 30


My Contributions
ok the XML is passed to a webservice, which builds a query from it, searches the DB and returns a xml string with completed fields, this works fine that wasnt the problem. I got it sorted eventually (the code follows just now). why im using xml?... it was easier when i started, now however im regretting that but its my first venture with web services as well (im doing alot of those with this project)

<Message> is the document wrapper, in case I send more than 1 query at any one time through the webservice, the <Company> is the table name and the next level is the fields i want as either the "WHERE" part of the sql Query or what to to select.

and the CreateDBEvent has absolutely nothing to do with the problem, just creates an entry in the Event table, its for usage stats

anyway the solution to the problem

CODE
public static void BindGridView(ref GridView gv, string source)
    {
            string data = StaticOperations.XmlDataStringToXmlAttributeString(source);
            if (data.Length > 20)
            {
                DataSet set = new DataSet();
                set.ReadXml(new MemoryStream(new System.Text.ASCIIEncoding().GetBytes(data.ToCharArray())), XmlReadMode.InferSchema);
                gv.DataSource = set;
                gv.DataBind();
            }
    }


i made the code generic coz i use it all over the place, but for whatever reason the XMLDataSource wasnt binding to the xml it was given all the time, sometimes it would sometimes it wouldnt. Ive had no problems with the above code. The data.Length > 20 is just to protect against an empty result from the database.

Thanks to Antagonist for that solution... mate of mine from varsity.

I think the next question is why does the following code only work sometimes

CODE
XmlDataSource XDS = new XmlDataSource();
        XDS.Data = StaticOperations.XmlDataStringToXmlAttributeString(source);
        gv.DataSource = XDS;
        gv.DataBind();


its what the BindGridView origionally contained.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month