Click here to Skip to main content
15,917,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hidden field is not working in asp.net c#

C#
void counts()
    {

        foreach (RepeaterItem items in repeat.Items)
        {
            HiddenField comment_like = (HiddenField)items.FindControl("comment_id");
            Label user_counts = (Label)items.FindControl("user_counts");

            

            if (Request.QueryString["id"] != null)
            {
                int id;
                id = Convert.ToInt32(Request.QueryString["id"].ToString());
                con = new SqlConnection(str);
                con.Open();
                cmd = new SqlCommand("sp_command_count", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@eid", id);
                cmd.Parameters.AddWithValue("@comment_id", comment_like.Value);
                da = new SqlDataAdapter(cmd);
                dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    DataRow dr = dt.Rows[0];
                    user_counts.Text = dr["total_count"].ToString() + "of" + dr["like_count"].ToString();

                    repeat.DataSource = dt;
                    repeat.DataBind();
                    review_panel.Visible = true;


                }
                else
                {
                    review_panel.Visible = false;
                }
                con.Close();
            }
        }
    }

asp.code


ASP.NET
<asp:Label ID="user_counts" runat="server" CssClass="counts">

                                     <asp:HiddenField ID="comment_like" runat="server" Value='<%#Eval("comment_id") %>' />


stored procedure


SQL
ALTER PROCEDURE [dbo].[sp_command_count]

   (
    @eid int,
    @comment_id int
   )
AS
BEGIN
Declare  @sql int
    SET NOCOUNT ON;
      select @sql ='select *,(select c.comment_like from comments c where id=@eid and c.comment_id='+convert(varchar,@comment_id)+') as like_count,
          (select sum(comment_like+comment_dislike) from comments c where id=@eid and c.comment_id='+convert(varchar,@comment_id)+') as total_count from comments c
          where c.id=@eid and c.comment_id='+convert(varchar,@comment_id)+''
      END
Posted
Updated 1-Mar-15 19:11pm
v3
Comments
Member 10918596 2-Mar-15 0:49am    
Object reference not set to an instance of an object.
[no name] 2-Mar-15 1:10am    
Put a break point in ur code then chk which line u got the error?
Member 10918596 2-Mar-15 1:13am    
i put break point

cmd.Parameters.AddWithValue("@comment_id", comment_like.Value);..........>error

here only i got error
Member 10918596 2-Mar-15 1:14am    
how to solve?here only value does't passing. what can i do?

1 solution

Problem is here
C#
foreach (RepeaterItem items in repeat.Items)
        {
            HiddenField comment_like = (HiddenField)items.FindControl("comment_id");

Your hidden field id is comment_like but you are trying to find hidden field with id comment_id which not there.
Change this to
C#
foreach (RepeaterItem items in repeat.Items)
        {
            HiddenField comment_like = (HiddenField)items.FindControl("comment_like");


Hope, this helps :)
 
Share this answer
 
Comments
Member 10918596 2-Mar-15 1:17am    
wow ..i forgot..thank u..i solved
Suvendu Shekhar Giri 2-Mar-15 1:19am    
Welcome :)
glad that it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900