Link to home
Start Free TrialLog in
Avatar of -cr-
-cr-

asked on

Adding rows to gridview programmatically.

I am trying to populate a gridview in asp.net 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)
        EMailLogDataConn.Open()
        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
        GridView1.DataBind()

       Label1.Text = EMailLogDataStoredProcedure
        SQLEMailLogDataCmd.Dispose()
        SQLEMailLogDataCmd = Nothing
        EMailLogData.Close()
        EMailLogData = Nothing
        EMailLogDataConn.Close()
        EMailLogDataConn.Dispose()
        EMailLogDataConn = Nothing
        EMailLogDataConnString = Nothing
Avatar of RedKelvin
RedKelvin

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

ASKER

I have executed the stored proc in SQL Server and it should be returning results.
It should yes, but you are using parameters, it is always better to test at the program level.

Avatar of -cr-

ASKER

Yes when I am debugging I see the parameters being passed the same as I passed in sql server.
just before binding the datatable to the grid, interrogate the number of rows in dt

dt.rows.count
ASKER CERTIFIED SOLUTION
Avatar of GreymanMSC
GreymanMSC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial