How To Get the data from SQL database to display in GridView

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaredciagar
    New Member
    • Feb 2008
    • 16

    How To Get the data from SQL database to display in GridView

    Hi Guys Can you help me please in my project, I have tblItemList in my SQL DB. I want to display all the data in my tblItemList in GridView control....
    How Can I Do That in VB Script using VB.Net...

    Thank you...

    Please Help Me...

    Regards,
    Jared
    Last edited by kenobewan; Feb 24 '08, 10:21 AM. Reason: MERGED MULTIPLE POSTS
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Check this thread out.

    Comment

    • jaredciagar
      New Member
      • Feb 2008
      • 16

      #3
      How To Get the data from MSSQL2005 database to display in GridView(VB.Net )

      Hi Guys Can you help me please in my project, I have tblItemList in my SQL DB. I want to display all the data in my tblItemList in GridView control....
      How Can I Do That in VB Script using VB.Net...

      Thank you...

      Please Help Me...

      Comment

      • jaredciagar
        New Member
        • Feb 2008
        • 16

        #4
        Hi jeffstl,

        Thanks for your reply, I appreciate it, but I got an error when I write the code below.... The Error Message: Type 'System.data.sq lclient.reader' has no constructors
        What should I do? can help me please to debug my code...I'm using MSSQL 2005 as my database and I used ASP.Net in developing the system and VB script.
        thank you....

        Code:
        ---------Name Space----------
        <%@ Import Namespace="System.Data" %>
        <%@ Import Namespace="System.Data.SqlClient" %>
        <%@ Import Namespace="system.data.sqlclient.sqldatareader" %>
        
        --------VB Script-------------------
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                  
                Dim con As New SqlConnection
                        
                con.ConnectionString = "Integrated Security=True;data source=ph-erwinm1;" & "persist security info=false;Initial Catalog=ITP"
                Dim strSql As String
                Dim SQLComm As New SqlCommand
                Dim dbread As New SqlDataReader
                              
                             
                strSql = "SELECT intITPCodeFK,intQuantity FROM tblItemlist"
                SQLComm = New SqlCommand(strSql, con)
                dbread = SQLComm.ExecuteReader()
                GridITPSumm.DataSource = dbread
                GridITPSumm.DataBind()
            
                dbread.Close()
                con.Close()    
                        
            End Sub
        
        
        
        ------------------HTML-----------------------
            <asp:DataGrid id="GridITPSumm" runat="server"
            AutoGenerateColumns="False" CellPadding="3"
            HeaderStyle-BackColor="#A4A6CF"
            HeaderStyle-ForeColor="000033"
            HeaderStyle-HorizontalAlign="Left"
            HeaderStyle-Font-Bold="True"
            BackColor="#eeeeee" Width="50%"
            HorizontalAlign="Left"
            Font-Bold="True" 
            Font-Names="Tahoma"
            Font-Size="10pt">
            
            <AlternatingItemStyle BackColor="White" />
          
            <Columns>
                <asp:BoundColumn HeaderText="ITP COde" DataField="intITPCodeFK" />
                <asp:BoundColumn HeaderText="QTY" DataField="intQuantity" />                       
            </Columns>        
        </asp:DataGrid>

        Comment

        • jeffstl
          Recognized Expert Contributor
          • Feb 2008
          • 432

          #5
          I dont know that much about namespace, or if thats actually the problem but I did notice a few things.

          You need the <script> tags around your code-behind, and you also need to open your database connection

          see below changes

          Code:
          ---------Name Space----------
          <%@ Import Namespace="System.Data" %>
          <%@ Import Namespace="System.Data.SqlClient" %>
          <%@ Import Namespace="system.data.sqlclient.sqldatareader" %>
           
          --------VB Script-------------------
          
          'ADD SCRIPT TAG
          <script  runat="server">
          
          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                    
                  Dim con As New SqlConnection
                          
                  con.ConnectionString = "Integrated Security=True;data source=ph-erwinm1;" & "persist security info=false;Initial Catalog=ITP"
                  Dim strSql As String
                  Dim SQLComm As New SqlCommand
                  Dim dbread As New SqlDataReader
                                
                   'OPEN DB CONNECTION   
                  con.Open      
                 
                 strSql = "SELECT intITPCodeFK,intQuantity FROM tblItemlist"
                  SQLComm = New SqlCommand(strSql, con)
                  dbread = SQLComm.ExecuteReader()
                  GridITPSumm.DataSource = dbread
                  GridITPSumm.DataBind()
              
                  dbread.Close()
                  con.Close()    
                          
              End Sub
           
           'CLOSE SCRIPT TAG
          </Script> 
          ------------------HTML-----------------------
              <asp:DataGrid id="GridITPSumm" runat="server"
              AutoGenerateColumns="False" CellPadding="3"
              HeaderStyle-BackColor="#A4A6CF"
              HeaderStyle-ForeColor="000033"
              HeaderStyle-HorizontalAlign="Left"
              HeaderStyle-Font-Bold="True"
              BackColor="#eeeeee" Width="50%"
              HorizontalAlign="Left"
              Font-Bold="True" 
              Font-Names="Tahoma"
              Font-Size="10pt">
              
              <AlternatingItemStyle BackColor="White" />
            
              <Columns>
                  <asp:BoundColumn HeaderText="ITP COde" DataField="intITPCodeFK" />
                  <asp:BoundColumn HeaderText="QTY" DataField="intQuantity" />                       
              </Columns>        
          </asp:DataGrid>

          Comment

          • jaredciagar
            New Member
            • Feb 2008
            • 16

            #6
            How To use datatable in displaying data in gridview from mssql db

            hi guys,
            can you help me please....

            I'm using asp.net,mssql server2005 and vb script....

            i dont know how to display data from db to gridview....
            i want to use datatable in displaying data, so that i can edit,select and can allow paging to my gridview control.

            Thank You in advance I appreciate your support...

            Comment

            • jaredciagar
              New Member
              • Feb 2008
              • 16

              #7
              Originally posted by jeffstl
              I dont know that much about namespace, or if thats actually the problem but I did notice a few things.

              You need the <script> tags around your code-behind, and you also need to open your database connection

              see below changes

              Code:
              ---------Name Space----------
              <%@ Import Namespace="System.Data" %>
              <%@ Import Namespace="System.Data.SqlClient" %>
              <%@ Import Namespace="system.data.sqlclient.sqldatareader" %>
               
              --------VB Script-------------------
              
              'ADD SCRIPT TAG
              <script  runat="server">
              
              Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                        
                      Dim con As New SqlConnection
                              
                      con.ConnectionString = "Integrated Security=True;data source=ph-erwinm1;" & "persist security info=false;Initial Catalog=ITP"
                      Dim strSql As String
                      Dim SQLComm As New SqlCommand
                      Dim dbread As New SqlDataReader
                                    
                       'OPEN DB CONNECTION   
                      con.Open      
                     
                     strSql = "SELECT intITPCodeFK,intQuantity FROM tblItemlist"
                      SQLComm = New SqlCommand(strSql, con)
                      dbread = SQLComm.ExecuteReader()
                      GridITPSumm.DataSource = dbread
                      GridITPSumm.DataBind()
                  
                      dbread.Close()
                      con.Close()    
                              
                  End Sub
               
               'CLOSE SCRIPT TAG
              </Script> 
              ------------------HTML-----------------------
                  <asp:DataGrid id="GridITPSumm" runat="server"
                  AutoGenerateColumns="False" CellPadding="3"
                  HeaderStyle-BackColor="#A4A6CF"
                  HeaderStyle-ForeColor="000033"
                  HeaderStyle-HorizontalAlign="Left"
                  HeaderStyle-Font-Bold="True"
                  BackColor="#eeeeee" Width="50%"
                  HorizontalAlign="Left"
                  Font-Bold="True" 
                  Font-Names="Tahoma"
                  Font-Size="10pt">
                  
                  <AlternatingItemStyle BackColor="White" />
                
                  <Columns>
                      <asp:BoundColumn HeaderText="ITP COde" DataField="intITPCodeFK" />
                      <asp:BoundColumn HeaderText="QTY" DataField="intQuantity" />                       
                  </Columns>        
              </asp:DataGrid>


              Hey jeffstl thank you...
              but i have a problem in binding data in gridview because of using SqlDataReader, I can't allow paging in my gridview because it is readonly, how can i allow paging in my gridview?
              thank you...
              please help

              Comment

              Working...