Link to home
Start Free TrialLog in
Avatar of NewMom2Brandon
NewMom2Brandon

asked on

URGENT & EASY ASP using C# populating datagrid

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

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of sandip-mishra
sandip-mishra

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