Newbie: Datagrid is Completely Blank?!?

Discussion in 'ASP .Net' started by Altemir, May 1, 2005.

  1. Altemir

    Altemir Guest

    Just trying to connect an ASP.NET page to an SQL database. When I run
    the page, I get no errors but no recordset results are returned via the
    datagrid control -- it is completely blank.

    The only thing displayed is the "Job Dispatch List" page title which is
    executed as plain HTML.

    Code is as follows. Any ideas as to what could be going wrong?
    --------------


    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClie­nt" %>

    <HTML>

    <SCRIPT language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

    Dim MyDataset As DataSet
    Dim MyConnection As SqlConnection
    Dim MyDataAdapter As SqlDataAdapter
    Dim myDataGrid as DataGrid

    MyConnection = New SqlConnection("Data Source=apollo;Initial
    Catalog=ProductionStatus;uid=s­a;pwd=mypassword" )
    MyDataAdapter = New SqlDataAdapter("SELECT ID FROM dbo_PART",
    MyConnection)

    MyDataset = New DataSet()
    MyDataAdapter.Fill(MyDataset,"­Parts")

    MyDataGrid = New DataGrid()
    MyDataGrid.DataSource=MyDatase­t.Tables("Parts")
    MyDataGrid.DataBind()

    End Sub

    </SCRIPT>

    <TITLE>Job Dispatch List</TITLE>
    <BODY>

    <H3><font face="Arial">Job Dispatch List</font></H3>

    <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="700"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="true"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd­"
    EnableViewState="false"
    />

    </BODY>
    </HTML>
     
    Altemir, May 1, 2005
    #1
    1. Advertisements

  2. Altemir

    Norman Yuan Guest

    Try to remove

    MyDataGrid = New DataGrid()

    from the seerver side script code: You already have the DataGrid created in
    the HTML part and the variable "MyDataGrid" is originally pointed to the
    DataGrid. Once you point the variable to a new DataGrid in your code, you
    get a NEW data grid, which IS NOT the one you created in design stage (in
    HTML part). The data will be bound to the DataGrid you just created in code.
    Since the DataGrid you created in ocde is not added to the Page's control
    collection, it will not show in the page, it only stays in server's memory
    during the page loading and then be discarded. So, you page should be blank.

    Just trying to connect an ASP.NET page to an SQL database. When I run
    the page, I get no errors but no recordset results are returned via the
    datagrid control -- it is completely blank.

    The only thing displayed is the "Job Dispatch List" page title which is
    executed as plain HTML.

    Code is as follows. Any ideas as to what could be going wrong?
    --------------


    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClie­nt" %>

    <HTML>

    <SCRIPT language="VB" runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

    Dim MyDataset As DataSet
    Dim MyConnection As SqlConnection
    Dim MyDataAdapter As SqlDataAdapter
    Dim myDataGrid as DataGrid

    MyConnection = New SqlConnection("Data Source=apollo;Initial
    Catalog=ProductionStatus;uid=s­a;pwd=mypassword" )
    MyDataAdapter = New SqlDataAdapter("SELECT ID FROM dbo_PART",
    MyConnection)

    MyDataset = New DataSet()
    MyDataAdapter.Fill(MyDataset,"­Parts")

    MyDataGrid = New DataGrid()
    MyDataGrid.DataSource=MyDatase­t.Tables("Parts")
    MyDataGrid.DataBind()

    End Sub

    </SCRIPT>

    <TITLE>Job Dispatch List</TITLE>
    <BODY>

    <H3><font face="Arial">Job Dispatch List</font></H3>

    <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="700"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="true"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd­"
    EnableViewState="false"
    />

    </BODY>
    </HTML>
     
    Norman Yuan, May 1, 2005
    #2
    1. Advertisements

  3. Altemir

    Guest Guest

    Hi Altemir,

    Sincem you don't explicitly declared columns for the datagrid, you need to
    set
    AutoGenerateColumns = ture. Otherwise there is no column to show data.

    HTH

    Elton Wang
     
    Guest, May 1, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.