Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
My problem is that, the query is correctly fetching the number of rows but when i bind the query result with the gridview, it displays double the no. of rows actually present in the resultset with the first row showing just the primary key value and the next row displaying all the field values.
for e.g. I have an employee table and I have two rows in table. But after I bind the resultset with the Gridview it shows 4 rows,the first 2 rows display only the employee ids and the next 2 rows display the actual 2 rows that it is supposed to display. My query works fine when I execute it on the Database but there is problem at application end.

My code goes something like this.

ASP.NET Page:
ASP.NET
<tr>
     <td style="width: 90px">
         <asp:GridView ID="gv" runat="server">
         </asp:GridView>
                </td>
     <td style="width: 118px">
     </td>
</tr>


Codebehind:
VB
Protected Sub PopulateGridView()
        gv.DataSource = oDb.ListEmployeeDetails()
        gv.DataBind()


 Public Function ListEmployeeDetails() As DataTable

        command.Connection = OpenConn()
        command.CommandType = Data.CommandType.Text
        command.CommandText = "Select emp_id,emp_name,address,doj,dept_id,proj_id from Employee"
        reader = command.ExecuteReader()
        If (reader.HasRows) Then
            dt.Load(reader)
        End If
        Return dt

    End Function


Public Function OpenConn() As SqlConnection
        conn = New SqlConnection(Connection())
        Try
            If (conn.State = System.Data.ConnectionState.Closed) Then
                conn.Open()
                Return conn
            End If
        Catch ex As Exception
            Return Nothing
        End Try
        Return Nothing
    End Function



 Public ReadOnly Property Connection() As String
        Get
            Dim s As String
            s = ConfigurationManager.ConnectionStrings("DbConn").ConnectionString
            Return s
        End Get
    End Property

    End Sub
Posted
Updated 5-Jun-12 6:46am
v2

you don't need to set grid in to a table , because it render a table by default!
 
Share this answer
 
Comments
pujan.gc@gmail.com 8-Jun-12 2:19am    
I am using a table within which I am placing all my web controls in order to properly align them.
use template field in gridview
 
Share this answer
 
Comments
pujan.gc@gmail.com 8-Jun-12 2:13am    
I have tried using template fields. But the result remains the same

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900