Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have a GridView on a form and it's not visible in the browser even though Visible has been set to True in the Property Window. Meanwhile, I have the following code to enable data to be visible in the columns of the GridView, but nothing seems to work for me. I'd be glad if someone could help me out.
 
The showgrid procedure is supposed to be called when the form is being loaded, but non of this is working. What beats me completely is the GrideView not showing even though it is visible in the Design view.
 

 
Sub showgrid()
     Try
         ds.Clear()
         ada = New SqlDataAdapter("Select SubjectCode'Subject Code',SubjectName'Subject Name',SubjectType'Subject Type',UserId'User Id',Password from ProgramDetails.Subjects", cn)
         ada.Fill(ds, "ProgramDetails.Subjects")
         cmd = New SqlCommand("Select SubjectCode'Subject Code',SubjectName'Subject Name',SubjectType'Subject Type',UserId'User Id',Password from ProgramDetails.Subjects", cn)
         ada.SelectCommand = cmd
         ada.Fill(ds)
         GridView1.DataSource = ds.Tables(0)
     Catch ex As Exception
     End Try
End Sub
 

ASP code
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .style2
        {
        }
        .style4
        {
            width: 297px;
        }
        .style6
        {
            width: 268px;
        }
        .style3
        {
            height: 23px;
            width: 146px;
        }
        .style5
        {
            height: 23px;
            width: 297px;
        }
        .style1
        {
            height: 23px;
            width: 268px;
        }
        .style7
        {
        }
    </style>
</head>
<body>
    <form id="frmSubjects"  runat="server">
    <asp:Panel ID="Panel1" runat="server" BackColor="#FFFF99" BorderColor="#00CC00" 
        BorderStyle="Double" Height="464px">
        <div>
        </div>
        <p>
            <asp:TextBox ID="txtIdNumber" runat="server" Visible="False" 
            Width="26px"></asp:TextBox>
        </p>
        <table style="width: 66%;" align="center">
            <tr>
                <td class="style2" colspan="2">
                    Subject Code</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtSubjectCode" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                    <asp:Button ID="btnGridView" runat="server" style="text-align: center" Text="Save" 
                    Width="93px" />
                </td>
            </tr>
            <tr>
                <td class="style3" colspan="2">
                    Subject Name</td>
                <td class="style5" colspan="2">
                    <asp:TextBox ID="txtSubjectName" runat="server" Width="279px"></asp:TextBox>
                </td>
                <td class="style1" colspan="2">
                </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Subject Type</td>
                <td class="style4" colspan="2">
                    <asp:RadioButton ID="rbnCore" runat="server" Text="Core" />
                    <asp:RadioButton ID="rbnElective" runat="server" Text="Elective" />
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter User Name</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter Password</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                     </td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Button ID="btnSave" runat="server" style="text-align: center" Text="Save" 
                    Width="93px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="92px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnUpdate" runat="server" Text="Update" Width="96px" />
                </td>
                <td class="style2">
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="97px" />
                </td>
            </tr>
            <tr>
                <td class="style7">
                     </td>
                <td class="style2" colspan="2">
                     </td>
                <td class="style2" colspan="2">
                     </td>
                <td class="style2">
                     </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                    <asp:Label ID="lblMessage" runat="server" 
                    style="text-align: center" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" 
    ForeColor="#333333" GridLines="None" 
        style="text-align: center" Width="491px">
            <RowStyle BackColor="#E3EAEB" />
            <EmptyDataRowStyle BorderStyle="Solid" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
    </asp:Panel>
    <div>
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NaassConnectionString %>" 
        SelectCommand="SELECT [IDNumber], [SubjectCode], [SubjectName], [SubjectType], [UserId], [Password] FROM [Subjects]">
    </asp:SqlDataSource>
    </form>
</body>
</html>
Posted 23-Mar-13 7:54am
Akaglo419
Edited 23-Mar-13 8:39am
v3
Comments
Mickt1985 at 23-Mar-13 13:35pm
   
Can you show your aspx code please?
Tadit Dash at 23-Mar-13 14:30pm
   
You have one sql datasource defined in the aspx page and again you are using DataAdapter to bind the GridView from code behind.
Why are you doing the same thing twice ?
Akaglo at 23-Mar-13 16:39pm
   
I've removed the repeated line of code and what is happening is that the grid now appears but can't display data in columns

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try
            ds.Clear()
            ada = New SqlDataAdapter("Select SubjectCode'Subject Code',SubjectName'Subject Name',SubjectType'Subject Type',UserId'User Id',Password from ProgramDetails.Subjects", cn)
            ada.Fill(ds, "ProgramDetails.Subjects")
            cmd = New SqlCommand("Select SubjectCode'Subject Code',SubjectName'Subject Name',SubjectType'Subject Type',UserId'User Id',Password from ProgramDetails.Subjects", cn)
            ada.SelectCommand = cmd
            ada.Fill(ds)
            GridView1.DataSource = ds.Tables(0)
            GridView1.DataBind()'binds the datasource to the GridView
        Catch ex As Exception
        End Try
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100