Solved

URGENT & EASY ASP using C# populating datagrid

Posted on 2009-07-02
3
393 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
3 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

803 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