[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

URGENT & EASY ASP using C# populating datagrid

Posted on 2009-07-02
3
Medium Priority
?
406 Views
Last Modified: 2013-12-17
I am having a hard time getting my datagrid to show my data. What do I need to do to make this work?

I am using .NET 2008

I removed parts of my connection string but it does connect

I would really like to put the data in an object instead so if you have an example of that that would be great as well
default.aspx file:
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="2px" 
            CellPadding="4" GridLines="Vertical">
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <Columns>
                    <asp:BoundField AccessibleHeaderText="Feed_Number" HeaderText="Feed Number" />
                    <asp:BoundField AccessibleHeaderText="Dept_Feed_Name" 
                        HeaderText="Department Feed Name" />
                    <asp:ButtonField AccessibleHeaderText="Select" Text="Select">
                    <ControlStyle Font-Underline="True" ForeColor="Blue" />
                    </asp:ButtonField>
                </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
 
default.aspx.cs file
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Data.OracleClient;
using System.Data;
using Dean_Data_Composer;
using System.Configuration;
 
namespace Dean_Data_Composer
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check active directory to see if user allowed to view this
            DeptFeedOracleConnection();
        }
 
        public void DeptFeedOracleConnection()
        {
            //string connectionString = "Data Source=SERVER1;Persist Security Info=True; User=user1; Password=@passw";
            using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConnectionString"].ConnectionString))
            {
                connection.Open();
 
                try
                {
                    DataSet ds = new DataSet();
                    OracleCommand cmd = connection.CreateCommand();
                    cmd.CommandText = "Select * from DEPT_FEED";
 
                    OracleDataAdapter oda = new OracleDataAdapter();
 
                    oda.SelectCommand = cmd;
                    oda.Fill(ds, "DEPT_FEED");
 
                    /// clear the grids data source
                    GridView1.DataSource = null;
 
                    DataTable dt = new DataTable();
 
                    dt = ds.Tables["DEPT_FEED"];
 
                    DataView dv = new DataView(dt);
 
                    dv = ds.Tables["DEPT_FEED"].DefaultView;
 
                    // Bind the data table to the data grid
                    GridView1.DataSource = dv;
                    GridView1.DataBind();
 
 
                    foreach (DataTable t in ds.Tables)
                    {
                        Response.Write("Table: " + t.TableName + " is in Dataset");
                        Response.Write("Row 0, Column 2: " + t.Rows[0][2]);
                        Response.Write("Row 1, Column 2: " + t.Rows[1][2]);
                        Response.Write("Row 2, Column 2: " + t.Rows[2][2]);
                        Response.Write("Row 3, Column 2: " + t.Rows[3][2]);
 
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("There was some error connecting to the database" + "DB Error: " + ex.ToString());
                }
                finally
                {
                    //Don't keep the connection open. 
                    connection.Close();
                }
            }
 
        }
    }
}

Open in new window

0
Comment
Question by:NewMom2Brandon
[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 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 24767838
You have BoundFields but you did not specify and DataFied ..it should be the name of the column from your table.
e.g. Feed_Number is a column from your table..
 <asp:BoundField AccessibleHeaderText="Feed_Number" HeaderText="Feed Number" DataFied= "Feed_Number" />
0
 
LVL 9

Expert Comment

by:AsishRaj
ID: 24767917
Yeap Guru Sami has identified for you

 DataFied= "Feed_Name" is missing in your gridview

That is the reason no data is shown in the gridview
0
 
LVL 3

Expert Comment

by:sandip-mishra
ID: 24769576
Hi , i have used your code and its working fine for me.. The fiffrence is in the aspx page.


aspx...

<asp:GridView runat="server" ID="GridView1"  AutoGenerateColumns="False" >
            <Columns>
                <asp:BoundField DataField="a" HeaderText="a" SortExpression="a" />
                <asp:BoundField DataField="b" HeaderText="b" SortExpression="b" />
                <asp:BoundField DataField="c" HeaderText="c" SortExpression="c" />
                <asp:BoundField DataField="no" HeaderText="no" SortExpression="no" />
                <asp:BoundField DataField="x" HeaderText="x" SortExpression="x" />
                <asp:CheckBoxField DataField="bol" HeaderText="bol" SortExpression="bol" />
            </Columns>
        </asp:GridView>

aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Dynamics-ExchangeConnectionString"].ToString());
        try
        {
            GridView1.DataSource = null;
           
            SqlCommand sqlComd = new SqlCommand();
            sqlComd.Connection = sqlConn;
            sqlComd.CommandType = CommandType.Text;
            sqlComd.CommandText = "select * from a";
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = sqlComd;
            da.Fill(ds, "DEPT_FEED");
            DataTable dt = new DataTable();
            dt = ds.Tables["DEPT_FEED"];
            DataView dv = new DataView(dt);
            dv = ds.Tables["DEPT_FEED"].DefaultView;
            GridView1.DataSource = dv;
            GridView1.DataBind();
            foreach (DataTable t in ds.Tables)
            {
                Response.Write("Table: " + t.TableName + " is in Dataset");
                Response.Write("Row 0, Column 2: " + t.Rows[0][2]);
                Response.Write("Row 1, Column 2: " + t.Rows[1][2]);
                Response.Write("Row 2, Column 2: " + t.Rows[2][2]);
                Response.Write("Row 3, Column 2: " + t.Rows[3][2]);

            }
        }
        catch
        {

        }
        finally
        {
            sqlConn.Close();

        }

    }


I have used a Sql Database and you have used Oracle Database, but it doesnt matter. I have taken a diffrent table(dummy table).

You can try the code that i have pasted in aspx...... its only the Datafield.

Regards

Sandip Mishra
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

656 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