I'm having problem filling the dropdownlist with data from database in a windows based application.
I know how to do that in ASP.NET, but I can't find a solution in .NET 4.5 windows based.

public void Paraqit_ddListKlasa()
            DataTable dt = new DataTable();
            SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);

            string SQLstringu = "Select  KlasaViti, ID_Klasa from KLASA ";
            SqlCommand cmd = new SqlCommand(SQLstringu, sqlConn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            ddListKlasa.DataSource = dt;
            ddListKlasa.DataTextField = "KlasaViti";
            ddListKlasa.DataValueField = "ID_Klasa";
            ddListKlasa.Items.Insert(0, new ListItem("--Zgjedh--", "0"));

It is not recognizing the DataTextField and DataValueField

Recommended Answers

All 4 Replies

You are giving it a value and not referring to the database. Cycle through the database and put the values into strings and then call those strings in your code.

I created a class StringKoneksioni, in that class it is a mthod Koneksioni that returns the connection string.
Also you can see here SQLstringu is the sql query what text and value i want to fill the dropdownlist. In the ASP.NET I know in pageload i have to put the following code:

 if (!IsPostBack)

How to fix this in wondows based application?

Your class for connecting to the database should be fine.
Your query to the database should be fine as well.

What you need to do is now cycle through the row/s that contain your information and extract that information.

I don't use asp but rather I use php. The principle behind it should be the same though.
It is not going to help you if the code is put up here for you to copy. Rather try and find out about cycling through a row in the database and extracting the information from that row into a variable and then using that variable to populate your dropdown list. Your code should be using where so that when the end of the row is reached it exists the function and your dropdown list is populated.

This is what I use in php. Try and see if you can make sense of this and then try find an asp function that will do the same.

<form class="semantic" method="post" action="edit_person.php">
<legend>View a Person</legend>
$ops = '';
$sql_select = "select person_id, first_name, middle_name, last_name from `person` order by `first_name` asc ";
$retval_selectperson = mysql_query( $sql_select, $conn );
if(! $retval_selectperson ) { die('Could not select data: ' . mysql_error()); }
while($row = mysql_fetch_assoc($retval_selectperson)) 
$ops .=  "<option value='{$row['person_id']}'>{$row['first_name']} {$row['middle_name']} {$row['last_name']}</option>";
<label for="person_id">Select a Person</label>
<select name="person_id" id="person_id">
<option value="">--- Select a Person ---</option>
<?php echo $ops;?>

Hi, you are passing the value for data and text field which doesnt bind the Dropdown .
try using as follows:

 ddListKlasa.DataSource = dt;
            ddListKlasa.DataTextField = dt["your column name"];
            ddListKlasa.DataValueField = dt["col value"];

not tested .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.