Solved

URGENT & EASY ASP using C# populating datagrid

Posted on 2009-07-02
3
391 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video teaches users how to migrate an existing Wordpress website to a new domain.
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.

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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now