Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I am new in c# for first assignment. I had successfully load data from dropdownlist into my textfield. Also manage to insert, update and delete the records from database table. Now, I am facing this problem. Each new record insert into db table, update or delete, my dropdownlist which load from Page_Load doesn't not refresh my dropdownlist.

I do some research online, instead of using ajax. I may use another trick code which is DropDownList2.DataBind();

Anyhow,it still doesn't work to refresh my Dropdown list. Any expert can give me some advise. Did I place the line of code wrongly?

Below is my code for delete function.


What I have tried:

C#
connection.ConnectionString = 
 ConfigurationManager.ConnectionStrings["constr"].ToString();
 connection.Open();
 NpgsqlCommand cmd = new NpgsqlCommand();
 cmd.Connection = connection;
 cmd.CommandText = "Delete from student_db where id=@id";
 cmd.CommandType = CommandType.Text;
 cmd.Parameters.Add(new NpgsqlParameter("@id", 
 Convert.ToInt32(DropDownList2.SelectedItem.Value)));
 cmd.ExecuteNonQuery();
 cmd.Dispose();
 DropDownList2.DataBind();
 connection.Close();
Posted
Updated 4-Sep-19 22:50pm

1 solution

You need to call DataBind() function while saving,updating or deleting data.
like
DropDownList.DataBind();
To get more reference Have a look there
HierarchicalDataBoundControl.PerformDataBinding Method (System.Web.UI.WebControls) | Microsoft Docs[^]

BaseDataBoundControl.DataBind Method (System.Web.UI.WebControls) | Microsoft Docs[^]
 
Share this answer
 

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



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