postgresql - Not able to get value from drop down list, c# -


In the page load I am loading a drop down list with names from the PostGrey SQL Database using SQL. Later in the code, I want to save the selected value in the drop down list in a string variable. This page will not be loaded (works fine):

  Protected Zero Page_Load (Object Sender, EventArgues E) {NPGSCock Connection Connection = New NPGSQL Connection (" Server = 127.0.0.1; port = 5432; database = ses; user id = postgrej; password = postgres;); conn.Open (); NpgsqlCommand command = new NpgsqlCommand ("name tbl_syv by name select", conn); NpgsqlDataReader dr = command.ExecuteReader (); While (Dr. Reid ()) {ddPersons.Items.Add (string) dr ["name"] + "" + ""); }}   

Here Button1_Click I want to save the selected value to string variable syv .

  Protected Zero Button 1_Click (Object Sender, EventArgs e) {string syv = ddPersons.SelectedItem.Text; String name = txtName.Text; Int studentid = 0; NPGSQL Connection conn = New NPGSQL Connection ("Server = 127.0.0.1; Port = 5432; Database = SSYS; User ID = Postgrej; Password = Postgresres;"); conn.Open (); String sql = "SELECT id fROM tbl_student WHERE name = '" + name + "'"; Npgsql Commands Command = New NPGSQL Commands (Esquilla, Conn.); NpgsqlDataReader dr = command.ExecuteReader (); While (Dr. Reid ()) {studentid = (int) dr ["id"]; }} I have tried  
  string syv = ddPersons.SelectedItem.Text; String syv = ddPersons. Selected Item.Value; String syv = ddPersons.Text; String syv = ddPersons.SelectedValue;  

It may be that the viewstoast is saving the status of your page. Why do not you include the following line in your page_load method:

  if (! IsPostBack) {NpgsqlConnection conn = new NpgsqlConnection ("server = 127.0.0.1; ports = 5432; database = SYS; User id = postgrej; password = postgres; "); conn.Open (); NpgsqlCommand command = new NpgsqlCommand ("name tbl_syv by name select", conn); NpgsqlDataReader dr = command.ExecuteReader (); While (Dr. Reid ()) {ddPersons.Items.Add (string) dr ["name"] + "" + ""); }}   

When clicking on a button on the page and prevents the viewsto from interfering in your control state, Page_load can be prevented from populating duplicate items.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -