DataGrid not showing data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmm32
    New Member
    • May 2007
    • 9

    DataGrid not showing data

    I have a DataGrid which displays data with the aid of a procedure.

    I have tested the procedure inside the database and it is working fine.
    I have another procedure which adds another row to the table, which is working fine inside the programming, and in the database.

    However, I still cannot figure out what am I doing wrong with the DataGrid configurations OR Database OR DataAdapter OR SqlSelectComman d ... I really don't know yet and I have spent a lot of time in this tiny issue.

    Below is additional information. Any help is extremely appreciated.

    **On page Load a different page calls the method RefreshDataCrit eria, which it supposes to refresh the data in the DataGrid***

    I have other pages that do the same thing:
    when the user clicks on a certain link, in the main page, the link makes visible the control ( .ascx ) and then calls the function to refresh the data of the DataGrid. The other pages are working fine, except for this one.

    ----------------------------------------------------------
    Code inside the HTML part:
    [HTML]
    <asp:datagrid id=dataGridCrit eria Width="466px" runat="server" Font-Size="XX-Small" DataKeyField="C ourseCriteriaID " DataMember="pro cWebSelectCours eCriteriaForSyl labus" DataSource="<%# DtsCourseCriter ias31 %>" BorderColor="#3 366CC" BorderWidth="1p x" BackColor="Whit e" CellPadding="4" BorderStyle="No ne" AutoGenerateCol umns="False" PageSize="5" Font-Names="sans-serif" AllowPaging="Tr ue" Height="120px">
    <SelectedItemSt yle Font-Bold="True" ForeColor="#CCF F99" BackColor="#009 999"></SelectedItemSty le>
    <ItemStyle Font-Size="XX-Small" Font-Names="Arial" Font-Bold="True" ForeColor="#003 399" BackColor="Whit e"></ItemStyle>
    <HeaderStyle Font-Size="XX-Small" Font-Names="Arial" Font-Bold="True" ForeColor="#CCC CFF" BackColor="#003 399"></HeaderStyle>
    <FooterStyle ForeColor="#003 399" BackColor="#99C CCC"></FooterStyle>
    <Columns>
    <asp:BoundColum n DataField="Prof OrgCriteriaID" HeaderText="Pro fOrgCriteriaID" ></asp:BoundColumn >
    <asp:BoundColum n DataField="Prof OrgCriteriaCode " HeaderText="Pro fOrgCriteriaCod e"></asp:BoundColumn >
    <asp:BoundColum n DataField="Prof OrgCriteriaDesc r" HeaderText="Pro fOrgCriteriaDes cr"></asp:BoundColumn >
    <asp:BoundColum n DataField="Cour seCriteriaID" HeaderText="Cou rseCriteriaID"> </asp:BoundColumn >
    <asp:ButtonColu mn Text="Delete" CommandName="De lete">
    <HeaderStyle Width="50px"></HeaderStyle>
    </asp:ButtonColum n>
    </Columns>
    <PagerStyle NextPageText="N ext Page" PrevPageText="P revious Page" HorizontalAlign ="Center" ForeColor="#003 399"
    BackColor="#99C CCC"></PagerStyle>
    </asp:datagrid>[/HTML]
    -----------------------------------------------------------------------

    Procedure:

    Code:
    CREATE PROCEDURE dbo.procWebSelectCourseCriteriaForSyllabus
    
    @CourseCode varchar(20)
    
    AS
    
    SELECT     ProfOrgCriteria.ProfOrgCriteriaID, ProfOrgCriteria.ProfOrgCriteriaCode,ProfOrgCriteria.ProfOrgCriteriaDescr,CourseCriteria.CourseCriteriaID
    FROM         CourseCriteria INNER JOIN
                          ProfOrgCriteria ON CourseCriteria.ProfOrgCriteriaID = ProfOrgCriteria.ProfOrgCriteriaID
    WHERE     (CourseCriteria.CourseCode = @CourseCode)
    GO
    ---------------------------------------------------------

    DataSet:

    [HTML]<?xml version="1.0" standalone="yes "?>
    <xs:schema id="dtsCourseCr iterias3" targetNamespace ="http://www.tempuri.org/dtsCourseCriter ias3.xsd" xmlns:mstns="ht tp://www.tempuri.org/dtsCourseCriter ias3.xsd" xmlns="http://www.tempuri.org/dtsCourseCriter ias3.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata" attributeFormDe fault="qualifie d" elementFormDefa ult="qualified" >
    <xs:element name="dtsCourse Criterias3" msdata:IsDataSe t="true">
    <xs:complexType >
    <xs:choice maxOccurs="unbo unded">
    <xs:element name="procWebSe lectCourseCrite riaForSyllabus" >
    <xs:complexType >
    <xs:sequence>
    <xs:element name="ProfOrgCr iteriaID" msdata:ReadOnly ="true" msdata:AutoIncr ement="true" type="xs:int" />
    <xs:element name="ProfOrgCr iteriaCode" type="xs:string " />
    <xs:element name="ProfOrgCr iteriaDescr" type="xs:string " />
    <xs:element name="CourseCri teriaID" msdata:ReadOnly ="true" msdata:AutoIncr ement="true" type="xs:int" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="Table">
    <xs:complexType >
    <xs:sequence>
    <xs:element name="ProfOrgCr iteriaID" msdata:ReadOnly ="true" msdata:AutoIncr ement="true" type="xs:int" />
    <xs:element name="ProfOrgCr iteriaCode" type="xs:string " />
    <xs:element name="ProfOrgCr iteriaDescr" type="xs:string " />
    <xs:element name="CourseCri teriaID" msdata:ReadOnly ="true" msdata:AutoIncr ement="true" type="xs:int" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    <xs:unique name="dtsCourse Criterias3Key1" msdata:PrimaryK ey="true">
    <xs:selector xpath=".//mstns:procWebSe lectCourseCrite riaForSyllabus" />
    <xs:field xpath="mstns:Pr ofOrgCriteriaID " />
    <xs:field xpath="mstns:Co urseCriteriaID" />
    </xs:unique>
    <xs:unique name="Constrain t1" msdata:PrimaryK ey="true">
    <xs:selector xpath=".//mstns:Table" />
    <xs:field xpath="mstns:Pr ofOrgCriteriaID " />
    <xs:field xpath="mstns:Co urseCriteriaID" />
    </xs:unique>
    </xs:element>
    </xs:schema>[/HTML]

    -----------------------------------------

    VB.NET Code:


    Code:
    Public Class CourseCriteriaGrid
        Inherits System.Web.UI.UserControl
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
            Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
            Me.sqlSelectCourseCriteria = New System.Data.SqlClient.SqlCommand
            Me.SqlCommandAddCourseCriteria = New System.Data.SqlClient.SqlCommand
            Me.DtsCourseCriterias31 = New UniversalSyllabus.dtsCourseCriterias3
            Me.SqlDataAdapterCourseCriteria = New System.Data.SqlClient.SqlDataAdapter
            Me.sqlCommandDeleteCourseCriteria = New System.Data.SqlClient.SqlCommand
            CType(Me.DtsCourseCriterias31, System.ComponentModel.ISupportInitialize).BeginInit()
            '
            'SqlConnection1
            '
            Me.SqlConnection1.ConnectionString = CType(configurationAppSettings.GetValue("SqlConnection1.ConnectionString", GetType(System.String)), String)
            '
            'sqlSelectCourseCriteria
            '
            Me.sqlSelectCourseCriteria.CommandText = "[procWebSelectCourseCriteriaForSyllabus]"
            Me.sqlSelectCourseCriteria.CommandType = System.Data.CommandType.StoredProcedure
            Me.sqlSelectCourseCriteria.Connection = Me.SqlConnection1
            Me.sqlSelectCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
            Me.sqlSelectCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CourseCode", System.Data.SqlDbType.VarChar, 20))
            '
            'SqlCommandAddCourseCriteria
            '
            Me.SqlCommandAddCourseCriteria.CommandText = "[procWebInsertCourseCriteria]"
            Me.SqlCommandAddCourseCriteria.CommandType = System.Data.CommandType.StoredProcedure
            Me.SqlCommandAddCourseCriteria.Connection = Me.SqlConnection1
            Me.SqlCommandAddCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
            Me.SqlCommandAddCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CourseCode", System.Data.SqlDbType.VarChar, 20))
            Me.SqlCommandAddCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@ProfOrgCriteriaID", System.Data.SqlDbType.Int, 4))
            '
            'DtsCourseCriterias31
            '
            Me.DtsCourseCriterias31.DataSetName = "dtsCourseCriterias3"
            Me.DtsCourseCriterias31.EnforceConstraints = False
            Me.DtsCourseCriterias31.Locale = New System.Globalization.CultureInfo("en-US")
            '
            'SqlDataAdapterCourseCriteria
            '
            Me.SqlDataAdapterCourseCriteria.SelectCommand = Me.sqlSelectCourseCriteria
            '
            'sqlCommandDeleteCourseCriteria
            '
            Me.sqlCommandDeleteCourseCriteria.CommandText = "[procWebDeleteCourseCriteria]"
            Me.sqlCommandDeleteCourseCriteria.CommandType = System.Data.CommandType.StoredProcedure
            Me.sqlCommandDeleteCourseCriteria.Connection = Me.SqlConnection1
            Me.sqlCommandDeleteCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
            Me.sqlCommandDeleteCourseCriteria.Parameters.Add(New System.Data.SqlClient.SqlParameter("@CourseCriteriaID", System.Data.SqlDbType.Int, 4))
            CType(Me.DtsCourseCriterias31, System.ComponentModel.ISupportInitialize).EndInit()
    
        End Sub
    
        Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
        '  Protected WithEvents DtsCourseCriterias1 As UniversalSyllabus.dtsCourseCriterias3
        Protected WithEvents sqlSelectCourseCriteria As System.Data.SqlClient.SqlCommand
        Protected WithEvents SqlCommandAddCourseCriteria As System.Data.SqlClient.SqlCommand
        Protected WithEvents DtsCourseCriterias31 As UniversalSyllabus.dtsCourseCriterias3
        Protected WithEvents SqlDataAdapterCourseCriteria As System.Data.SqlClient.SqlDataAdapter
        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents dataGridCriteria As System.Web.UI.WebControls.DataGrid
        Protected WithEvents buttonAddCriteria As System.Web.UI.WebControls.Button
        Protected WithEvents sqlCommandDeleteCourseCriteria As System.Data.SqlClient.SqlCommand
    
        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
        End Sub
    
        Public Sub RefreshDataCriteria()
            Me.DtsCourseCriterias31.Clear()
            Me.sqlSelectCourseCriteria.Parameters("@CourseCode").Value = Session("CourseCode")
            Me.SqlDataAdapterCourseCriteria.Fill(Me.DtsCourseCriterias31)
            Me.dataGridCriteria.DataBind()
    
        End Sub
    
        Public Event addCriteria()
    
    
        Private Sub buttonAddCriteria_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAddCriteria.Click
            RaiseEvent addCriteria()
    
        End Sub
    
        Private Sub dataGridCriteria_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
            'Enable javascript to alert about the delete
            If e.Item.ItemType <> ListItemType.Header And _
            e.Item.ItemType <> ListItemType.Footer And e.Item.ItemType <> ListItemType.Pager Then
                Dim deleteButton2 As LinkButton = e.Item.Cells(2).Controls(0)
                deleteButton2.Attributes("onclick") = "javascript:return " & _
                "confirm('Are you sure you want to delete Course Criteria: " & _
                DataBinder.Eval(e.Item.DataItem, "CriteriaDescription") & " ?')"
            End If
    
        End Sub
    
    
        Private Sub dataGridCriteria_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
            Me.dataGridCriteria.CurrentPageIndex = e.NewPageIndex
            RefreshDataCriteria()
        End Sub
    
    
        Private Sub dataGridCriteria_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
            Select Case (CType(e.CommandSource, LinkButton)).CommandName
                Case "Delete"
                    Me.sqlCommandDeleteCourseCriteria.Parameters("@CourseCriteriaID").Value = Me.dataGridCriteria.DataKeys(e.Item.ItemIndex)
                    Me.SqlConnection1.Open()
                    Me.sqlCommandDeleteCourseCriteria.ExecuteNonQuery()
                    Me.SqlConnection1.Close()
    
                    Me.dataGridCriteria.EditItemIndex = -1
    
                    If Me.dataGridCriteria.CurrentPageIndex >= 1 And Me.dataGridCriteria.Items.Count = 1 Then
                        Me.dataGridCriteria.CurrentPageIndex = Me.dataGridCriteria.CurrentPageIndex - 1
    
                    End If
    
                    RefreshDataCriteria()
            End Select
        End Sub
    
    
    End Class
    -------------------------------------

    Thank you!,
    NMM
    Last edited by nmm32; May 20 '07, 04:09 AM. Reason: I forgot something!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Sounds like you have a postback problem.

    Comment

    • nmm32
      New Member
      • May 2007
      • 9

      #3
      I have added the code..
      Code:
      Private Sub Page_Load
      (ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles MyBase.Load
      
              If Not IsPostBack Then
                  RefreshDataCriteria()
              End If
          End Sub

      But still does not work.
      I even have tried it without the "if not ispostback".

      Comment

      • Abdul Haque
        New Member
        • May 2007
        • 17

        #4
        try debugging the value which is being send to SP. It appear that datatype is mismatching as a result the where condition is not showing a result , which in turn is null.

        Comment

        • nmm32
          New Member
          • May 2007
          • 9

          #5
          Originally posted by Abdul Haque
          try debugging the value which is being send to SP. It appear that datatype is mismatching as a result the where condition is not showing a result , which in turn is null.
          Thanks. I tried that, but it is passing the correct data.
          Ok, I solved the problem, but not with the DataGrid. I chose a different control (ListBox) and arranged the programming so that the ListBox, with two buttons, can do the same thing that the DataGrid was supposed to do. Not a "pretty" box, but well, they must get along with it now.

          ....I have not yet figured out what happened with the DataGrid. Someone told me to call the method Me.DataBind in Page Load(). .. got to check that out.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Anytime you change the .DataSource property, you must also call the .DataBind() function to complete the change.
            I only use a few lines of code for it.
            Code:
            DataTable dt=globalDBFunctions.GetMyData();
            Mygridview.DataSource=dt;
            Mygridview.DataBind();

            Comment

            Working...