0

This button will store what I typed in my textbox into my SQL Server database:

string aOption = Request.Form["option"].ToString();
string ATitle = Request.Form["title"].ToString();
string Adesc = Request.Form["desc"].ToString();

con = new SqlConnection(constr);
con.Open();

cmd = new SqlCommand("INSERT INTO Thread (shareORask, Thread_Title, Thread_Description) VALUES (@shareORask, @Thread_Title, @Thread_Description)", con);

cmd.Parameters.AddWithValue("@shareORask", askORshare_Selected);
cmd.Parameters.AddWithValue("@Thread_Title", AS_Title);
cmd.Parameters.AddWithValue("@Thread_Description", AS_desc);

cmd.ExecuteNonQuery();

con.Close();

Next, I wanted to dynamically generate div and display database content. I did it in Page Load.

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(constr);
    con.Open();

    SqlCommand cmd = new SqlCommand("Select * from Thread", con);

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    reptater.DataSource = ds;
    reptater.DataBind();

    con.Close();
}

<asp:Repeater runat="server" ID="reptater">
    <ItemTemplate>
        <div><%#Eval("Thread_Description") %></div>
    </ItemTemplate>

It works but when I submit my input into the textbox. I'm not able to get data shown in the dynamically created. It only works when i refresh the page.

However, duplicate data will be added into my SQL Server database, how do I solve this?

2
  • You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented Dec 29, 2020 at 18:16
  • I would assume that is is a simple matter that right after you run the code stub to add the row, you then simply do a re-data bind on your repeater and your additonal data should display. In other words, you want put a check in your page load if IsPostback = False for the code that loads/binds the repeater. However, that re-bind should not be required unless your add code runs. So I would move out the re-bind/display routine to a seperate sub. That way both on-load (first time - check isPostback = false), and then also in your code stub to add - you call the load/bind routine again. Commented Dec 29, 2020 at 22:12

1 Answer 1

1

I'm assuming that the button code is in a method called something like MyButton_Click(object sender, EventArgs e).

The button click event is a "Control event" and these happen after the Page_Load event in the page life cycle. To insert into the database during the click event and render the div in one page cycle you would need to render the div in one of the later events, probably the Page_PreRender event.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to say something like this. Also depends on if the page IsPostBack. I would add a grid or manage your db call outside of page load and then call the sub in the page_load after postback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.