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?
.AddWithValue()- it can lead to unexpected and surprising results...