locked
GridView not displaying RRS feed

  • Question

  • User-305496339 posted

    Hi Friends,

    I am using the code below to populate a SqlDataAdapter, fill a DataTable, and then bid to a Gridview. For some strange reason my Gridview is not displaying. Thanks for any help.

    Dim dt As DataTable = New DataTable()

    Dim conStr As String = "Data Source=JSE;Initial Catalog=Test_UAT;Integrated Security=True"

    Dim con As SqlConnection = New SqlConnection(conStr)

    Dim com As SqlCommand = New SqlCommand("jse_BusinessInformation.getSentEmail", con)

    com.Parameters.AddWithValue("@StartDate", SqlDbType.NVarChar).Value = StartDateTxt.Text

    com.Parameters.AddWithValue("@EndDate", SqlDbType.NVarChar).Value = EndDateTxt.Text

    com.Parameters.AddWithValue("@BatchFlag", SqlDbType.NVarChar).Value = EmailFlagDdl.Text

    com.CommandType = CommandType.StoredProcedure

    Dim da As SqlDataAdapter = New SqlDataAdapter(com)

    'Try

    con.Open()

    da.Fill(dt)

    If con.State = ConnectionState.Open Then con.Close()

    GridView1.DataSource = dt

    GridView1.DataBind()

    Tuesday, October 1, 2019 4:39 PM

Answers

  • User475983607 posted

    I recommend basic troubleshooting.  I assume the result set is empty.  Set a breakpoint and verify the DataTable has rows.  

    The following code has possible errors.  First, there's no validation and we can't determine of the input contain values.  Second, there are two dates that are treated like string which is odd.

    com.Parameters.AddWithValue("@StartDate", SqlDbType.NVarChar).Value = StartDateTxt.Text
    com.Parameters.AddWithValue("@EndDate", SqlDbType.NVarChar).Value = EndDateTxt.Text
    com.arameters.AddWithValue("@BatchFlag", SqlDbType.NVarChar).Value = EmailFlagDdl.Text

    Again, taking the time to debug should help you determine where the bugs are in your logic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 1, 2019 6:57 PM
  • User475983607 posted

    Oddly enough the dataset has the right amount of columns, but no rows. On my future posts I will be more comprehensive in sharing more about the other components related to the codes i.e. the stored proc and table structure.

    That's not odd at all.  Your result set is empty.  There is nothing to data bind.  I recommend using the GridView's empty data template to display a message to the user when this situation happens.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.emptydatatemplate?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 2:30 PM

All replies

  • User475983607 posted

    I recommend basic troubleshooting.  I assume the result set is empty.  Set a breakpoint and verify the DataTable has rows.  

    The following code has possible errors.  First, there's no validation and we can't determine of the input contain values.  Second, there are two dates that are treated like string which is odd.

    com.Parameters.AddWithValue("@StartDate", SqlDbType.NVarChar).Value = StartDateTxt.Text
    com.Parameters.AddWithValue("@EndDate", SqlDbType.NVarChar).Value = EndDateTxt.Text
    com.arameters.AddWithValue("@BatchFlag", SqlDbType.NVarChar).Value = EmailFlagDdl.Text

    Again, taking the time to debug should help you determine where the bugs are in your logic.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 1, 2019 6:57 PM
  • User-305496339 posted

    Oddly enough the dataset has the right amount of columns, but no rows. On my future posts I will be more comprehensive in sharing more about the other components related to the codes i.e. the stored proc and table structure.

    Wednesday, October 2, 2019 2:12 PM
  • User475983607 posted

    Oddly enough the dataset has the right amount of columns, but no rows. On my future posts I will be more comprehensive in sharing more about the other components related to the codes i.e. the stored proc and table structure.

    That's not odd at all.  Your result set is empty.  There is nothing to data bind.  I recommend using the GridView's empty data template to display a message to the user when this situation happens.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.emptydatatemplate?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 2, 2019 2:30 PM