Click here to Skip to main content
15,918,041 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
HI i am trying to bind a gridview with a command.HOwever data is there but gridview only shows the text i have given in the empty data text property of the grid view.Anyt help would b really appreciated.Here is the code:

XML
<asp:GridView ID="GridView1" runat="server" Caption="Bidding" AutoGenerateColumns="false"
                CaptionAlign="Top" HorizontalAlign="Justify" DataKeyNames="confid" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                CellPadding="4" ForeColor="#333333" GridLines="None" Width="689px"
        EmptyDataText="ABX">
                <RowStyle BackColor="#E3EAEB" />
                <Columns>
                    <asp:TemplateField HeaderText="Conference">
                        <EditItemTemplate>
                            &nbsp;
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="lbid" OnClick="LinkButton1_Click2" runat="server" Text='<%# Eval("confname") %>'
                                CommandArgument='<%# Eval("Confid") %>' CommandName="xyz"></asp:LinkButton>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="White"></HeaderStyle>
                    </asp:TemplateField>

                    <asp:BoundField DataField="tpceemailid" HeaderText="TPC Id" />
                </Columns>
                <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="Gray" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#7C6F57" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>




VB
Dim con As New SqlConnection("Data Source=113.138.123.330;Initial Catalog=test1;User ID=abc;Password=abc")
            Try


                Dim cmd As New SqlCommand
                cmd.CommandText = "select reseArcharea from  musermaster where  emailid='" + Session("userid") + "' "
                cmd.Connection = con
                cmd.CommandTimeout = 10
                If con.State <> ConnectionState.Open Then
                    con.Open()
                End If
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                If dr.Read() Then
                    rarea = dr(0).ToString()
                    'MyInterface = Implementer
                    'mycommand.Parameters.Clear()
                    'mycommand.Parameters.AddWithValue("@tablename", "[sp_stopbidding]")
                    'mycommand.Parameters.AddWithValue("@confid", confid)
                    'mytable.TableName = MyInterface.UpdateRow(mycommand)
                    'If mytable.TableName = "Saved Successfully" Then
                    '    MessageBox("Record updated sucessfully")
                    ' bindgrid()
                    dr.Close()
                End If
                Dim cmd1 As New SqlCommand
                cmd1.CommandText = "select a.confname,tpceemailid,a.confid from mconferencemaster a,emeritadmin.mptcmaster b  where bidding='1' and a.researcharea='" + rarea + "' and b.tpceemailid='" + Session("userid") + "'"
                cmd1.Connection = con
                cmd1.CommandTimeout = 10
                cmd1.CommandType = CommandType.Text
                If con.State <> ConnectionState.Open Then
                    con.Open()
                End If
                Dim dr1 As SqlDataReader = cmd1.ExecuteReader()
                If dr1.Read() Then
                    GridView1.DataSource = dr1
                    GridView1.DataBind()
               
                Else
                    GridView1.EmptyDataText = "ABC"
                End If
            Catch ex As Exception

            End Try
Posted
Updated 30-Jan-14 22:26pm
v4
Comments
thatraja 31-Jan-14 4:20am    
Include Gridview design source in your question. And debug your code(particularly check the condition If dr1.Read() Then)
Gourav Sharma from Jammu 31-Jan-14 4:27am    
i have checked the condition it does get into the if condition and the datasource dr1 has rows..still it doesnot bind the data
Member 10476757 31-Jan-14 5:50am    
check your query on sqlserver whether it is working
DipsMak 31-Jan-14 6:05am    
Are you gettig data in your reader check again as in your sql query you are using
email id = Session("userid")
Gourav Sharma from Jammu 31-Jan-14 23:56pm    
yes i have checked the reader it says it has rows....

1 solution

Replace your fallowing code

VB
Dim dr1 As SqlDataReader = cmd1.ExecuteReader()
              If dr1.Read() Then
                  GridView1.DataSource = dr1
                  GridView1.DataBind()

              Else
                  GridView1.EmptyDataText = "ABC"
              End If


WITH

VB
   Dim dr1 As  SqlDataReader =cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(dr)

GridView1.DataSource = dr1
GridView1.DataBind()


For Details refer this link
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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