Adding rows to gridview programmatically.

Posted on 2007-10-21
Last Modified: 2013-11-26
I am trying to populate a gridview in 2 using vb. Below is what I have. It is not returning an error, but it is not displaying the results. Any assistance would be greatly appreciated.
Label1.Text = ""
        Label1.Text = EMailLogOptionsDropDownList.SelectedValue
        Dim EMailLogDataStoredProcedure As String
        Select Case EMailLogOptionsDropDownList.SelectedValue
            Case "1"
                EMailLogDataStoredProcedure = "usr_sel_EMailLog_BrokerAdministrationEMailLogOutput"
            Case "2"
                EMailLogDataStoredProcedure = "usr_sel_EMailLog_BrokerAdministrationEMailLogOutput_BrandedPageOwnerEMailOutput"
            Case "3"
                EMailLogDataStoredProcedure = "usr_sel_EMailLog_BrokerAdministrationEMailLogOutput_ManagerEmployeeEMailOutput"
        End Select

        Dim dt As New DataTable
        dt.Columns.Add("Sender", GetType(String))
        dt.Columns.Add("Recipient", GetType(String))
        dt.Columns.Add("Copied", GetType(String))
        dt.Columns.Add("Subject", GetType(String))
        dt.Columns.Add("Sent Date", GetType(DateTime))
        Dim BrokerEmail As String = BrokerEmail
        Dim EMailLogDataConnString As String = ConfigurationManager.AppSettings("****")
        Dim EMailLogDataConn As New SqlConnection(EMailLogDataConnString)
        Dim SQLEMailLogDataCmd As New SqlCommand(EMailLogDataStoredProcedure, EMailLogDataConn)
        Dim EMailLogData As SqlDataReader
        SQLEMailLogDataCmd.CommandType = CommandType.StoredProcedure
        SQLEMailLogDataCmd.Parameters.AddWithValue("@BrokerEMailAddress", BrokerEmail) 'Request.Cookies("BrokerEMailAddress").Value)
        SQLEMailLogDataCmd.Parameters.AddWithValue("@StartDate", StartDate.Text)
        SQLEMailLogDataCmd.Parameters.AddWithValue("@EndDate", EndDate.Text)
        EMailLogData = SQLEMailLogDataCmd.ExecuteReader
        While EMailLogData.Read
            dt.Rows.Add(New Object() {EMailLogData("Sender"), EMailLogData("Recipient"), EMailLogData("Copied"), EMailLogData("Subject"), EMailLogData("SentDate")})
        End While
        GridView1.DataSource = dt

       Label1.Text = EMailLogDataStoredProcedure
        SQLEMailLogDataCmd = Nothing
        EMailLogData = Nothing
        EMailLogDataConn = Nothing
        EMailLogDataConnString = Nothing
Question by:-cr-
    LVL 22

    Expert Comment

    Ensure that
    While EMailLogData.Read
    has actually got some data, otherwise there will be nothing to add to the grid

    Author Comment

    I have executed the stored proc in SQL Server and it should be returning results.
    LVL 22

    Expert Comment

    It should yes, but you are using parameters, it is always better to test at the program level.


    Author Comment

    Yes when I am debugging I see the parameters being passed the same as I passed in sql server.
    LVL 22

    Expert Comment

    just before binding the datatable to the grid, interrogate the number of rows in dt

    LVL 16

    Accepted Solution

    Are you sure about that?  Because the following line declares a variable then initialises it with its declared value (which will be empty).  You later use it as a parameter value without setting it to anything else.  I really don't think it's doing what you think it should.

      Dim BrokerEmail As String = BrokerEmail

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now