Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6563853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:53:49+00:00 2026-05-25T13:53:49+00:00

Hey I am a bit stuck at the moment, I am creating a forum,

  • 0

Hey I am a bit stuck at the moment,
I am creating a forum, I cant seem to get the “threadID” in the code behind, I need this to function as I then use it to post to the database.

The “threadID” is being dynamically generated in the first repeater, I need to way to grab that value.

at the moment I am getting this error in the browser:

“Compiler Error Message: CS0103: The name ‘threadID’ does not exist in the current context”

here’s the code:

<h1>viewThread.aspx</h1>
    <!-- THREAD TEXT STARTS --->
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" EnableViewState="False">
        <ItemTemplate>
            <h2><%# Eval ("threadTitle") %></h2>
            <!--- THIS THREAD ID NEEDS TO BE GENERATED DYNAMICALLY SO WE CAN THEN GRAB IT TO USE IN THE REPLY --->
            <asp:Label ID="threadID" runat="server" Visible="false" Text='<%# Eval ("threadID") %>' />
            <%# Eval ("threadText") %>
        </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VSTESTConnectionString %>" 
        SelectCommand="SELECT * FROM [threadCat&amp;Thread] WHERE ([threadID] = @threadID)">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="threadID" QueryStringField="t" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <!-- THREAD TEXT ENDS --->

    <!-- USER GENERATED REPLYS --->
    <h2>Replys</h2>

    <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
        <ItemTemplate>
            <%#Eval ("postText") %> <br />
           Date Posted <%#Eval ("postCreated") %>
        </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VSTESTConnectionString %>" 
        SelectCommand="SELECT * FROM [posts] WHERE ([threadID] = @threadID)">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="threadID" QueryStringField="t" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <!-- USER GENERATED REPLYS ENDS --->

    <!-- REPLY --->
    <h2>post a reply</h2>
    Userid<asp:TextBox ID="userID" runat="server" /><br />
    <asp:TextBox ID="postTxt" TextMode="MultiLine" runat="server"></asp:TextBox><br />
    <asp:Button ID="reply" runat="server" Text="Reply" onclick="reply_Click" />
    <!-- REPLY ENDS --->
</asp:Content>

<h1>viewThread.aspx.cs</h1>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class viewThread : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void reply_Click(object sender, EventArgs e)
    {
        /* threadID is not showing in intellisense */
        string thread = threadID.Text;
        string user = userID.Text;
        string reply = postTxt.Text;
        string date = DateTime.Now.ToString();
        int locked = 0;

        SqlConnection sqlCon = new SqlConnection("Server =.\\SQLEXPRESS; Database =VSTEST; Trusted_Connection =True;");
        SqlCommand sqlCom = new SqlCommand("INSERT INTO posts(threadID, postBy, postText, postCreated, postLocked) VALUES ('" + thread + "','" + user + "','" + reply + "','" + date + "','" + locked + "' )", sqlCon);
        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write("Brokies.... " + ex.ToString());
        }
        finally
        {
            sqlCon.Close();
            Response.Redirect("/forums/Default.aspx");
        }
    }
}

Have solved issue by using the viewstate, changes are below:

public partial class viewThread : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["t"] != null && Request.QueryString["t"].ToString() != "")
        {
            ViewState["threadID"] = Request.QueryString["t"].ToString();
        }
    }

    protected void reply_Click(object sender, EventArgs e)
    {
        string user = userID.Text;
        string reply = postTxt.Text;
        string date = DateTime.Now.ToString();
        int locked = 0;

        SqlConnection sqlCon = new SqlConnection("Server =.\\SQLEXPRESS; Database =VSTEST; Trusted_Connection =True;");
        SqlCommand sqlCom = new SqlCommand("INSERT INTO posts(threadID, postBy, postText, postCreated, postLocked) VALUES ('" + ViewState["threadID"].ToString() + "','" + user + "','" + reply + "','" + date + "','" + locked + "' )", sqlCon);
        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write("Brokies.... " + ex.ToString());
        }
        finally
        {
            sqlCon.Close();
            Response.Redirect("/forums/Default.aspx");
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:53:50+00:00Added an answer on May 25, 2026 at 1:53 pm

    Try setting the visibility with CSS instead of setting Visible="false".

    <asp:Label ID="threadID" runat="server" style="display:none;" Text='<%# Eval("threadID") %>' />
    

    And then try to use this code:

    Label lbl = Repeater1.Items[0].FindControl("threadID") as Label;
    if (lbl != null)
    {
        int threadID = Int32.Parse(lbl.Text);
    }
    

    Honestly, I would suggest using a ListView instead, so you can define data keys. This will be much easier that way:

    <asp:ListView ID="ListView1" runat="server" DataKeyNames="threadID">
        <ItemTemplate> 
            <h2><%# Eval ("threadTitle") %></h2> 
            <%# Eval ("threadText") %> 
        </ItemTemplate>     
    </asp:ListView>
    

    And then you can access the threadID like this in the code behind:

    int itemIndex = 0; //index of your current item
    int threadID = (int)ListView1.DataKeys[itemIndex]["threadID"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
Hey guys, I have this quick bit of code that I can't figure out
Hey guys a bit stuck here. Ill first start with showing you the code.
Hey all! This bit of code always returns 0, even though errcheck will have
Hey guys, this bit of code works in IE but not in Chrome, any
Hey guys, I've struggled with this for a bit and don't seem to find
Hey guys this is a bit of a homework puzzle I'm working on and
Hey i have been looking around and i cant find a way to get
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
Hey guys I need a tad bit of help. I need my streamwriter to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.