locked
Data not displaying on Repeater form RRS feed

  • Question

  • User1216627406 posted

    Greetings again,

    I am having difficulty figuring out why my code is not displaying results in Repeater form.

    I have a stored procedure that pulls records from the database.

    Then I have C# code that invokes that stored procedure.

    I also have fields that I have initially to add dynamic rows to the repeater control.

    Finally, I have forms that is supposed to display the values from DB.

    So far, no records is displaying.

    Does anyone have any ideas why I am not able to display the results of my SP on Repeater control?

    I have attempted to debug but the debugger just confirms what I already know which is that the values for the form fields are null.

    Your assistance is greatly appreciated.

        //Stored procedure
        ALTER PROCEDURE [dbo].[uspGetMarineRecs] @pin int
        AS
          SET NOCOUNT ON;
        begin
        select MarineRegNo, VesselRegNo, TaxPyrRetdVal,VesselTaxPyrRetdVal
          FROM MarineInfo
          WHERE pid = @pin
    end
      
    //Markup
    <ItemTemplate>
        <tr>
          <td>
           <asp:TextBox ID="txtboatregNum" runat="server"  Text='<%# Eval("MarineRegNo") %>' placeholder="Enter registration #..." Style="width: 450px;" class="form-control"></asp:TextBox>
          </td>
          <td>
           <asp:TextBox ID="txtPayerret" runat="server" placeholder="Enter returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
          </td>
        </tr>
        <tr>
          <td>
           <asp:TextBox ID="cgaurdNumber" runat="server" placeholder="Enter Coast Guard #..." Style="width: 450px;" class="form-control"></asp:TextBox>
          </td>
          <td>
           <asp:TextBox ID="cguardreturnedval" runat="server" placeholder="Enter returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
          </td>
        </tr>
    </ItemTemplate>
      
    //C#
        private void getMarineRecs(int pin)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("uspGetMarineRecs", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("@pin", pin);
            cmd.Parameters.Add(p1);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dtPreviousRecords = new DataTable();
            sda.Fill(dtPreviousRecords);
            Repeater1.DataSource = dtPreviousRecords;
            Repeater1.DataBind();
            //conn.Open();
        }
     
    DataTable dt = new DataTable();
      
        DataRow dr = null;
      
        dt.Columns.Add(new DataColumn("ID", typeof(string)));
      
        dt.Columns.Add(new DataColumn("MarineRegNo", typeof(string)));
      
        dt.Columns.Add(new DataColumn("TaxPyrRetdVal", typeof(string)));
      
        dt.Columns.Add(new DataColumn("VesselRegNo", typeof(string)));
      
        dt.Columns.Add(new DataColumn("VesselTaxPyrRetdVal", typeof(string)));
      
       
      
        dr = dt.NewRow();
      
        dr["ID"] = 1;
      
        dr["MarineRegNo"] = string.Empty;
      
        dr["TaxPyrRetdVal"] = string.Empty;
      
        dr["VesselRegNo"] = string.Empty;
      
        dr["VesselTaxPyrRetdVal"] = string.Empty;
      
        dt.Rows.Add(dr);
      
        ViewState["CurrTable"] = dt;
      
        Repeater1.DataSource = dt;
      
        Repeater1.DataBind();
    
    //setInitialRow
        private void SetInitialRow()
        {
            myMultiView.ActiveViewIndex = 0;
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("MarineRegNo", typeof(string)));
            dt.Columns.Add(new DataColumn("TaxPyrRetdVal", typeof(string)));
            dt.Columns.Add(new DataColumn("VesselRegNo", typeof(string)));
            dt.Columns.Add(new DataColumn("VesselTaxPyrRetdVal", typeof(string)));
    
            dr = dt.NewRow();
            dr["ID"] = 1;
            dr["MarineRegNo"] = string.Empty;
            dr["TaxPyrRetdVal"] = string.Empty;
            dr["VesselRegNo"] = string.Empty;
            dr["VesselTaxPyrRetdVal"] = string.Empty;
            dt.Rows.Add(dr);
            ViewState["CurrTable"] = dt;
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }

    Monday, September 11, 2017 3:21 AM

All replies

  • User-707554951 posted

    Hi simflex,

    It seems that there is some problem in your store procedure.

    You could refer to the working code as below:

    In ASPX:

    <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:TextBox ID="txtboatregNum" runat="server" Text='<%# Eval("MarineRegNo") %>' placeholder="Enter registration #..." Style="width: 450px;" class="form-control"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPayerret" runat="server" placeholder="Enter returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:TextBox ID="cgaurdNumber" runat="server" placeholder="Enter Coast Guard #..." Style="width: 450px;" class="form-control"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="cguardreturnedval" runat="server" placeholder="Enter returned value as of Jan. 1..." Style="width: 400px;" class="form-control txtPayerret"></asp:TextBox>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </form>

    In code behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                SetInitialRow();
                getMarineRecs(11);
            }
            private void getMarineRecs(int pin)
            {
                string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(connStr);
                conn.Open();
                //string sqlStr = @"select MarineRegNo, VesselRegNo, TaxPyrRetdVal,VesselTaxPyrRetdVal FROM MarineInfo WHERE pid = "+ @pin;
                //SqlCommand cmd = new SqlCommand(sqlStr, conn);
                SqlCommand cmd = new SqlCommand("uspGetMarineRecs", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter p1 = new SqlParameter("@pin", pin);
               cmd.Parameters.Add(p1);
                DataTable dtPreviousRecords = new DataTable();
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                sda.Fill(dtPreviousRecords);
                Repeater1.DataSource = dtPreviousRecords;
                Repeater1.DataBind();
            }
            private void SetInitialRow()
            {
                DataTable dt = new DataTable();
                DataRow dr = null;
                dt.Columns.Add(new DataColumn("ID", typeof(string)));
                dt.Columns.Add(new DataColumn("MarineRegNo", typeof(string)));
                dt.Columns.Add(new DataColumn("TaxPyrRetdVal", typeof(string)));
                dt.Columns.Add(new DataColumn("VesselRegNo", typeof(string)));
                dt.Columns.Add(new DataColumn("VesselTaxPyrRetdVal", typeof(string)));
                dr = dt.NewRow();
                dr["ID"] = 1;
                dr["MarineRegNo"] = string.Empty;
                dr["TaxPyrRetdVal"] = string.Empty;
                dr["VesselRegNo"] = string.Empty;
                dr["VesselTaxPyrRetdVal"] = string.Empty;
                dt.Rows.Add(dr);
                ViewState["CurrTable"] = dt;
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }

    In Store Procedure:

    CREATE PROCEDURE [dbo].[uspGetMarineRecs] @pin int  
     AS   
        SET NOCOUNT ON;  
        select MarineRegNo, VesselRegNo, TaxPyrRetdVal,VesselTaxPyrRetdVal
          FROM MarineInfo
          WHERE pid = @pin
    GO 

    Database like below:

    Output as below:

    Best regards

    Cathy

    Tuesday, September 12, 2017 6:23 AM
  • User1216627406 posted

    Hi Cathy,

    Thanks for your response.

    I am about to test your solution now but in the meantime, I need two clarifications from you.

    1,  You said there is something wrong with my stored procedure. Is the one on your solution to working copy? In other words, should I use that one?

    2, The other clarification is did you make any changes to my C# and markup other than the SP?

    I am asking to see if I could copy your C# and markup and replace mine.

    UPDATE: I tried it with the stored procedure. It didn't work.

    I tried it with the embedded sql statement, it didn't work either.

    So, I am completely bushed.

    Tuesday, September 12, 2017 2:23 PM
  • User-707554951 posted

    Hi simflex,

    1,  You said there is something wrong with my stored procedure. Is the one on your solution to working copy? In other words, should I use that one?

    1.The sql statement you provided is alter the uspGetMarineRece SP. Because I do not have that procedure, so I create the uspGetMarineRecs SP and then select data by using this SP.

    You could use the sql statement in my previous reply. However, you need to make sure there is no a SP whose name is same with 'uspGetMarineRecs' in your database

    Otherwise, you could failed to create SP.

    2, The other clarification is did you make any changes to my C# and markup other than the SP?

    I am asking to see if I could copy your C# and markup and replace mine.

    2.I called the SetInitialRow and getMarineRecs method in Page_Load. And I give a pin parameter into getMarineRecs method.

    Please check whether your database have data.

    You could refer to the embedded sql statement as below:

    string sqlStr = @"select MarineRegNo, VesselRegNo, TaxPyrRetdVal,VesselTaxPyrRetdVal FROM MarineInfo WHERE pid = "+ @pin;
    SqlCommand cmd = new SqlCommand(sqlStr, conn);

    Best regards

    Cathy

    Wednesday, September 13, 2017 7:42 AM
  • User1216627406 posted

    OK, I finally figured it out.

    There is actually nothing wrong with my SP.

    A couple of IF statements did the trick.

    Here is what worked:

        private void getMarineRecs(int pin)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("uspGetMarineRecs", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("@pin", pin);
            cmd.Parameters.Add(p1);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dtPreviousRecords = new DataTable();
            sda.Fill(dtPreviousRecords);
            if (dtPreviousRecords.Rows.Count > 0)
            {
                Repeater1.DataSource = dtPreviousRecords;
                Repeater1.DataBind();
                ViewState["CurrTable"] = dtPreviousRecords;
            }
        }
        
        private void SetInitialRow()
        {
            myMultiView.ActiveViewIndex = 0;
            DataTable dt;
            if (ViewState["CurrTable"] == null)
            {
                dt = new DataTable();
                DataRow dr = null;
                dt.Columns.Add(new DataColumn("ID", typeof(string)));
                dt.Columns.Add(new DataColumn("MarineRegNo", typeof(string)));
                dt.Columns.Add(new DataColumn("TaxPyrRetdVal", typeof(string)));
                dt.Columns.Add(new DataColumn("VesselRegNo", typeof(string)));
                dt.Columns.Add(new DataColumn("VesselTaxPyrRetdVal", typeof(string)));
    
                dr = dt.NewRow();
                dr["ID"] = 1;
                dr["MarineRegNo"] = string.Empty;
                dr["TaxPyrRetdVal"] = string.Empty;
                dr["VesselRegNo"] = string.Empty;
                dr["VesselTaxPyrRetdVal"] = string.Empty;
                dt.Rows.Add(dr);
            }
            else
            {
                dt = (DataTable)ViewState["CurrTable"];
            }
            ViewState["CurrTable"] = dt;
            if (dt.Rows.Count > 0)
            {
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
        } 

    Wednesday, September 13, 2017 2:46 PM