DataGrid will not display

  • Thread starter Thread starter Dave1031
  • Start date Start date
D

Dave1031

I am new to ASP.net and am building my site using Visual Web Developer 2005
Express.

I have no problem adding a "GridView" control to my page, wiring it to a
data connection and having it display in a browser. I can do all of this
with deag and drop functionality and do not have to write a line of code.

But if I need more flexibility and try to do the same thing
programmatically, the gtid will not display on my page. I get no error, juts
that nothing displays.

Listed below is the code I used. I got this code from an example in the
book ASP.net Data Web Controls. Can anyone tell me why it's not working?

I guess more fundamentally, what happene3d to the datagrid control in the
Web Developer Express? I have a "GridView" control but no "DataGrid"
control. Are they the same thing or is DataGrid nm longer supported?

Thanks
Dave

CODE:

<body>
<form id="form1" runat="server">

<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'1. Create a connection
Const strConnString As String = _
"server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn As New SqlConnection(strConnString)

'2. Create a command object for the query
Const strSQL As String = "SELECT * FROM authors"
Dim objCmd As New SqlCommand(strSQL, objConn)

'3. Create the DataAdapter
Dim objDA As New SqlDataAdapter()
objDA.SelectCommand = objCmd

'4. Populate the DataSet and close the connection
Dim objDS As New DataSet()
objDA.Fill(objDS)
objConn.Close()

'Finally, specify the DataSource and call DataBind()
dgAuthors.DataSourceID = "SqlDataSource2"
dgAuthors.DataBind()
End Sub
</script>

<asp:datagrid id="dgAuthors" runat="server" AllowPaging="True"
ShowFooter="True" />
.....
 
I am new to ASP.net and am building my site using Visual Web Developer 2005
Express.

I have no problem adding a "GridView" control to my page, wiring it to a
data connection and having it display in a browser.  I can do all of this
with deag and drop functionality and do not have to write a line of code.

But if I need more flexibility and try to do the same thing
programmatically, the gtid will not display on my page.  I get no error, juts
that nothing displays.

Listed below is the code I used.  I got this code from an example in the
book ASP.net Data Web Controls.   Can anyone tell me why it's not working?

I guess more fundamentally, what happene3d to the datagrid control in the
Web Developer Express?  I have a "GridView" control but no "DataGrid"
control.  Are they the same thing or is DataGrid nm longer supported?

Thanks
Dave

CODE:

<body>
    <form id="form1" runat="server">  

 <script runat="server">
        Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            '1. Create a connection
            Const strConnString As String = _
               "server=localhost;uid=sa;pwd=;database=pubs"
            Dim objConn As New SqlConnection(strConnString)

            '2. Create a command object for the query
            Const strSQL As String = "SELECT * FROM authors"
            Dim objCmd As New SqlCommand(strSQL, objConn)

            '3. Create the DataAdapter
            Dim objDA As New SqlDataAdapter()
            objDA.SelectCommand = objCmd

            '4. Populate the DataSet and close the connection
            Dim objDS As New DataSet()
            objDA.Fill(objDS)
            objConn.Close()

            'Finally, specify the DataSource and call DataBind()
         dgAuthors.DataSourceID = "SqlDataSource2"
            dgAuthors.DataBind()
        End Sub
 </script>

 <asp:datagrid id="dgAuthors" runat="server" AllowPaging="True"
ShowFooter="True" />
....    


Hi,
'Finally, specify the DataSource and call DataBind()
dgAuthors.DataSourceID = "SqlDataSource2"
dgAuthors.DataBind()

here you have used SqlDataSource2..is it the same as your dataset..i
think you should bind your gridview with dataset that is objDS and it
will display the gridview because your sqldatasource2 might be
empty..so it won't display anything.

try that out.
 
Thanks nil.

My apologies. When troublshooting I had switched the data source for the
datagrid with the dataview and forgot to change it back to
"dgAuthors.DataSource = objDS"

Once I did, it still would not work. No errors, just the datagrid does not
display. The GridView will display.

If I try to bind the gridview programmatically rather than as an attribute
of the asp tag as:
GridView1.DataSource = objDS
GridView1.DataBind()

....then it will not display either.

What is the difference between the datagrid and the gridview controls?

Here is the complete code:

<script runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Const strConnString As String = _
"server=localhost;uid=sa;pwd=;database=pubs"
Dim objConn As New SqlConnection(strConnString)

Const strSQL As String = "SELECT * FROM authors"
Dim objCmd As New SqlCommand(strSQL, objConn)

Dim objDA As New SqlDataAdapter()
objDA.SelectCommand = objCmd

Dim objDS As New DataSet()
objDA.Fill(objDS)
objConn.Close()

dgAuthors.DataSource = objDS
dgAuthors.DataBind()
End Sub
</script>
<body>
<form id="form1" runat="server">
<p> data grid start here</p>
<asp:datagrid id="dgAuthors" runat="server" style="position: relative"/>
<p>
data grid end here
</p>

<p>
gridview start here
</p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="au_id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True"
SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [authors]"></asp:SqlDataSource>
</p>
<p>
gridview end here
</p>
</form>
</body>
 
Thanks nil.

My apologies.  When troublshooting I had switched the data source for the
datagrid with the dataview and forgot to change it back to  
"dgAuthors.DataSource = objDS"

Once I did, it still would not work.  No errors, just the datagrid doesnot
display. The GridView will display.  

If I try to bind the gridview programmatically rather than as an attribute
of the asp tag as:
         GridView1.DataSource = objDS
         GridView1.DataBind()

...then it will not display either.

What is the difference between the datagrid and the gridview controls?

Here is the complete code:

<script runat="server">
     Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

         Const strConnString As String = _
            "server=localhost;uid=sa;pwd=;database=pubs"
         Dim objConn As New SqlConnection(strConnString)

         Const strSQL As String = "SELECT * FROM authors"
         Dim objCmd As New SqlCommand(strSQL, objConn)

         Dim objDA As New SqlDataAdapter()
         objDA.SelectCommand = objCmd

         Dim objDS As New DataSet()
         objDA.Fill(objDS)
         objConn.Close()

         dgAuthors.DataSource = objDS
         dgAuthors.DataBind()
     End Sub
 </script>
<body>
<form id="form1" runat="server">  
<p> data grid start here</p>
<asp:datagrid id="dgAuthors" runat="server" style="position: relative"/>
<p>
data grid end here
</p>            

<p>
gridview start here
</p>        
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="au_id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True"
SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [authors]"></asp:SqlDataSource>
</p>        
<p>
gridview end here
</p>
</form>
</body>


Hi,

Datagrid is the same as Gridview. Actually Datagrid was used in
asp.net 1.1. Gridview is more flexible then datagrid. i don't know
whether it is supported in asp.net 2.0 or not..but you should try..

here you are ending your tag..there are no colums and nothing..

<asp:datagrid id="dgAuthors" runat="server" style="position: relative"/
change it to

<asp:datagrid id="dgAuthors" runat="server" style="position:
relative">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True"
SortExpression="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname"
SortExpression="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname"
SortExpression="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone"
SortExpression="phone" />
</Columns>
</asp:datagrid>

try with this code..whether it works or not..let me know if you find
any problem..

Regards,
Nil
"Never expect..Never Regret"
 
Back
Top